]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subject, Subscription } from 'rxjs' |
be27ef3b | 2 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
1263fc4e | 3 | import { ActivatedRoute } from '@angular/router' |
67ed6552 C |
4 | import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, User } from '@app/core' |
5 | import { HooksService } from '@app/core/plugins/hooks.service' | |
6 | import { Syndication, VideoDetails } from '@app/shared/shared-main' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8 | import { VideoCommentThreadTree } from './video-comment-thread-tree.model' | |
4635f59d C |
9 | import { VideoComment } from './video-comment.model' |
10 | import { VideoCommentService } from './video-comment.service' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-video-comments', | |
14 | templateUrl: './video-comments.component.html', | |
15 | styleUrls: ['./video-comments.component.scss'] | |
16 | }) | |
1263fc4e | 17 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { |
2f5d2ec5 | 18 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef |
47564bbe | 19 | @Input() video: VideoDetails |
4635f59d C |
20 | @Input() user: User |
21 | ||
b29bf61d RK |
22 | @Output() timestampClicked = new EventEmitter<number>() |
23 | ||
4635f59d | 24 | comments: VideoComment[] = [] |
5b8072ee | 25 | highlightedThread: VideoComment |
67ed6552 | 26 | sort = '-createdAt' |
4635f59d C |
27 | componentPagination: ComponentPagination = { |
28 | currentPage: 1, | |
7416fbf3 | 29 | itemsPerPage: 10, |
4635f59d C |
30 | totalItems: null |
31 | } | |
32 | inReplyToCommentId: number | |
33 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | |
34 | threadLoading: { [ id: number ]: boolean } = {} | |
1263fc4e | 35 | |
c199c427 | 36 | syndicationItems: Syndication[] = [] |
53877968 | 37 | |
ad453580 C |
38 | onDataSubject = new Subject<any[]>() |
39 | ||
1263fc4e | 40 | private sub: Subscription |
4635f59d C |
41 | |
42 | constructor ( | |
43 | private authService: AuthService, | |
f8b2c1b4 | 44 | private notifier: Notifier, |
4cb6d457 | 45 | private confirmService: ConfirmService, |
d5b53822 | 46 | private videoCommentService: VideoCommentService, |
b1d40cff | 47 | private activatedRoute: ActivatedRoute, |
93cae479 C |
48 | private i18n: I18n, |
49 | private hooks: HooksService | |
4635f59d C |
50 | ) {} |
51 | ||
1263fc4e C |
52 | ngOnInit () { |
53 | // Find highlighted comment in params | |
54 | this.sub = this.activatedRoute.params.subscribe( | |
55 | params => { | |
5b8072ee C |
56 | if (params['threadId']) { |
57 | const highlightedThreadId = +params['threadId'] | |
58 | this.processHighlightedThread(highlightedThreadId) | |
1263fc4e C |
59 | } |
60 | } | |
61 | ) | |
62 | } | |
63 | ||
339632b4 C |
64 | ngOnChanges (changes: SimpleChanges) { |
65 | if (changes['video']) { | |
1263fc4e | 66 | this.resetVideo() |
47564bbe | 67 | } |
4635f59d C |
68 | } |
69 | ||
1263fc4e C |
70 | ngOnDestroy () { |
71 | if (this.sub) this.sub.unsubscribe() | |
72 | } | |
73 | ||
5b8072ee | 74 | viewReplies (commentId: number, highlightThread = false) { |
1263fc4e | 75 | this.threadLoading[commentId] = true |
4635f59d | 76 | |
93cae479 C |
77 | const params = { |
78 | videoId: this.video.id, | |
79 | threadId: commentId | |
80 | } | |
81 | ||
82 | const obs = this.hooks.wrapObsFun( | |
83 | this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService), | |
84 | params, | |
85 | 'video-watch', | |
86 | 'filter:api.video-watch.video-thread-replies.list.params', | |
87 | 'filter:api.video-watch.video-thread-replies.list.result' | |
88 | ) | |
89 | ||
90 | obs.subscribe( | |
4635f59d | 91 | res => { |
1263fc4e C |
92 | this.threadComments[commentId] = res |
93 | this.threadLoading[commentId] = false | |
cf387740 | 94 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) |
1263fc4e | 95 | |
bf079b7b C |
96 | if (highlightThread) { |
97 | this.highlightedThread = new VideoComment(res.comment) | |
98 | ||
99 | // Scroll to the highlighted thread | |
43483d12 | 100 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) |
bf079b7b | 101 | } |
4635f59d C |
102 | }, |
103 | ||
f8b2c1b4 | 104 | err => this.notifier.error(err.message) |
4635f59d C |
105 | ) |
106 | } | |
107 | ||
93cae479 C |
108 | loadMoreThreads () { |
109 | const params = { | |
110 | videoId: this.video.id, | |
111 | componentPagination: this.componentPagination, | |
112 | sort: this.sort | |
113 | } | |
7416fbf3 | 114 | |
93cae479 C |
115 | const obs = this.hooks.wrapObsFun( |
116 | this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService), | |
117 | params, | |
118 | 'video-watch', | |
119 | 'filter:api.video-watch.video-threads.list.params', | |
120 | 'filter:api.video-watch.video-threads.list.result' | |
121 | ) | |
122 | ||
123 | obs.subscribe( | |
124 | res => { | |
e8f902c0 C |
125 | this.comments = this.comments.concat(res.data) |
126 | this.componentPagination.totalItems = res.total | |
ad453580 C |
127 | |
128 | this.onDataSubject.next(res.data) | |
0840ca76 | 129 | this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }) |
93cae479 C |
130 | }, |
131 | ||
132 | err => this.notifier.error(err.message) | |
133 | ) | |
7416fbf3 C |
134 | } |
135 | ||
4635f59d C |
136 | onCommentThreadCreated (comment: VideoComment) { |
137 | this.comments.unshift(comment) | |
138 | } | |
139 | ||
140 | onWantedToReply (comment: VideoComment) { | |
141 | this.inReplyToCommentId = comment.id | |
142 | } | |
143 | ||
144 | onResetReply () { | |
145 | this.inReplyToCommentId = undefined | |
146 | } | |
147 | ||
4cb6d457 | 148 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 149 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 150 | } |
b29bf61d | 151 | |
67ed6552 | 152 | handleSortChange (sort: string) { |
c1125bca RK |
153 | if (this.sort === sort) return |
154 | ||
155 | this.sort = sort | |
156 | this.resetVideo() | |
157 | } | |
158 | ||
b29bf61d RK |
159 | handleTimestampClicked (timestamp: number) { |
160 | this.timestampClicked.emit(timestamp) | |
161 | } | |
4cb6d457 | 162 | |
1f30a185 | 163 | async onWantedToDelete (commentToDelete: VideoComment) { |
4cb6d457 | 164 | let message = 'Do you really want to delete this comment?' |
7e73f071 | 165 | |
e00aa353 | 166 | if (commentToDelete.isLocal || this.video.isLocal) { |
41a94d07 | 167 | message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.') |
7e73f071 C |
168 | } else { |
169 | message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.') | |
170 | } | |
171 | ||
b1d40cff | 172 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) |
1f30a185 C |
173 | if (res === false) return |
174 | ||
175 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
176 | .subscribe( | |
177 | () => { | |
e00aa353 C |
178 | if (this.highlightedThread?.id === commentToDelete.id) { |
179 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
180 | ||
181 | this.highlightedThread = undefined | |
182 | } | |
183 | ||
69222afa JM |
184 | // Mark the comment as deleted |
185 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
186 | }, |
187 | ||
f8b2c1b4 | 188 | err => this.notifier.error(err.message) |
1f30a185 | 189 | ) |
4cb6d457 C |
190 | } |
191 | ||
4635f59d C |
192 | isUserLoggedIn () { |
193 | return this.authService.isLoggedIn() | |
194 | } | |
7416fbf3 | 195 | |
0503e975 | 196 | onNearOfBottom () { |
2f1548fd | 197 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 198 | this.componentPagination.currentPage++ |
93cae479 | 199 | this.loadMoreThreads() |
7416fbf3 C |
200 | } |
201 | } | |
202 | ||
69222afa JM |
203 | private softDeleteComment (comment: VideoComment) { |
204 | comment.isDeleted = true | |
205 | comment.deletedAt = new Date() | |
206 | comment.text = '' | |
207 | comment.account = null | |
4cb6d457 | 208 | } |
339632b4 | 209 | |
1263fc4e | 210 | private resetVideo () { |
339632b4 C |
211 | if (this.video.commentsEnabled === true) { |
212 | // Reset all our fields | |
5b8072ee | 213 | this.highlightedThread = null |
339632b4 C |
214 | this.comments = [] |
215 | this.threadComments = {} | |
216 | this.threadLoading = {} | |
217 | this.inReplyToCommentId = undefined | |
0cd4344f C |
218 | this.componentPagination.currentPage = 1 |
219 | this.componentPagination.totalItems = null | |
339632b4 | 220 | |
53877968 | 221 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
93cae479 | 222 | this.loadMoreThreads() |
339632b4 C |
223 | } |
224 | } | |
1263fc4e | 225 | |
5b8072ee C |
226 | private processHighlightedThread (highlightedThreadId: number) { |
227 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 228 | |
5b8072ee C |
229 | const highlightThread = true |
230 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 231 | } |
4635f59d | 232 | } |