]>
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' | |
cfde28ba | 7 | import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment' |
19b163d0 | 8 | import { ThisReceiver } from '@angular/compiler' |
4635f59d C |
9 | |
10 | @Component({ | |
11 | selector: 'my-video-comments', | |
12 | templateUrl: './video-comments.component.html', | |
13 | styleUrls: ['./video-comments.component.scss'] | |
14 | }) | |
1263fc4e | 15 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { |
2f5d2ec5 | 16 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef |
47564bbe | 17 | @Input() video: VideoDetails |
4635f59d C |
18 | @Input() user: User |
19 | ||
b29bf61d RK |
20 | @Output() timestampClicked = new EventEmitter<number>() |
21 | ||
4635f59d | 22 | comments: VideoComment[] = [] |
5b8072ee | 23 | highlightedThread: VideoComment |
67ed6552 | 24 | sort = '-createdAt' |
4635f59d C |
25 | componentPagination: ComponentPagination = { |
26 | currentPage: 1, | |
7416fbf3 | 27 | itemsPerPage: 10, |
4635f59d C |
28 | totalItems: null |
29 | } | |
30 | inReplyToCommentId: number | |
f63c03fb | 31 | commentReplyRedraftValue: string |
32 | commentThreadRedraftValue: string | |
4635f59d C |
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 | 48 | private hooks: HooksService |
4635f59d C |
49 | ) {} |
50 | ||
1263fc4e C |
51 | ngOnInit () { |
52 | // Find highlighted comment in params | |
53 | this.sub = this.activatedRoute.params.subscribe( | |
54 | params => { | |
5b8072ee C |
55 | if (params['threadId']) { |
56 | const highlightedThreadId = +params['threadId'] | |
57 | this.processHighlightedThread(highlightedThreadId) | |
1263fc4e C |
58 | } |
59 | } | |
60 | ) | |
61 | } | |
62 | ||
339632b4 C |
63 | ngOnChanges (changes: SimpleChanges) { |
64 | if (changes['video']) { | |
1263fc4e | 65 | this.resetVideo() |
47564bbe | 66 | } |
4635f59d C |
67 | } |
68 | ||
1263fc4e C |
69 | ngOnDestroy () { |
70 | if (this.sub) this.sub.unsubscribe() | |
71 | } | |
72 | ||
5b8072ee | 73 | viewReplies (commentId: number, highlightThread = false) { |
1263fc4e | 74 | this.threadLoading[commentId] = true |
4635f59d | 75 | |
93cae479 C |
76 | const params = { |
77 | videoId: this.video.id, | |
78 | threadId: commentId | |
79 | } | |
80 | ||
81 | const obs = this.hooks.wrapObsFun( | |
82 | this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService), | |
83 | params, | |
84 | 'video-watch', | |
85 | 'filter:api.video-watch.video-thread-replies.list.params', | |
86 | 'filter:api.video-watch.video-thread-replies.list.result' | |
87 | ) | |
88 | ||
89 | obs.subscribe( | |
4635f59d | 90 | res => { |
1263fc4e C |
91 | this.threadComments[commentId] = res |
92 | this.threadLoading[commentId] = false | |
cf387740 | 93 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) |
1263fc4e | 94 | |
bf079b7b C |
95 | if (highlightThread) { |
96 | this.highlightedThread = new VideoComment(res.comment) | |
97 | ||
98 | // Scroll to the highlighted thread | |
43483d12 | 99 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) |
bf079b7b | 100 | } |
4635f59d C |
101 | }, |
102 | ||
f8b2c1b4 | 103 | err => this.notifier.error(err.message) |
4635f59d C |
104 | ) |
105 | } | |
106 | ||
93cae479 C |
107 | loadMoreThreads () { |
108 | const params = { | |
109 | videoId: this.video.id, | |
110 | componentPagination: this.componentPagination, | |
111 | sort: this.sort | |
112 | } | |
7416fbf3 | 113 | |
93cae479 C |
114 | const obs = this.hooks.wrapObsFun( |
115 | this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService), | |
116 | params, | |
117 | 'video-watch', | |
118 | 'filter:api.video-watch.video-threads.list.params', | |
119 | 'filter:api.video-watch.video-threads.list.result' | |
120 | ) | |
121 | ||
122 | obs.subscribe( | |
123 | res => { | |
e8f902c0 | 124 | this.comments = this.comments.concat(res.data) |
19b163d0 C |
125 | // Client does not display removed comments |
126 | this.componentPagination.totalItems = res.total - this.comments.filter(c => c.isDeleted).length | |
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) | |
fdd12965 | 138 | this.commentThreadRedraftValue = undefined |
4635f59d C |
139 | } |
140 | ||
141 | onWantedToReply (comment: VideoComment) { | |
142 | this.inReplyToCommentId = comment.id | |
143 | } | |
144 | ||
145 | onResetReply () { | |
146 | this.inReplyToCommentId = undefined | |
fdd12965 | 147 | this.commentReplyRedraftValue = undefined |
4635f59d C |
148 | } |
149 | ||
4cb6d457 | 150 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 151 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 152 | } |
b29bf61d | 153 | |
67ed6552 | 154 | handleSortChange (sort: string) { |
c1125bca RK |
155 | if (this.sort === sort) return |
156 | ||
157 | this.sort = sort | |
158 | this.resetVideo() | |
159 | } | |
160 | ||
b29bf61d RK |
161 | handleTimestampClicked (timestamp: number) { |
162 | this.timestampClicked.emit(timestamp) | |
163 | } | |
4cb6d457 | 164 | |
93e903ac C |
165 | async onWantedToDelete ( |
166 | commentToDelete: VideoComment, | |
167 | title = $localize`Delete`, | |
168 | message = $localize`Do you really want to delete this comment?` | |
169 | ): Promise<boolean> { | |
e00aa353 | 170 | if (commentToDelete.isLocal || this.video.isLocal) { |
66357162 | 171 | message += $localize` The deletion will be sent to remote instances so they can reflect the change.` |
7e73f071 | 172 | } else { |
66357162 | 173 | message += $localize` It is a remote comment, so the deletion will only be effective on your instance.` |
7e73f071 C |
174 | } |
175 | ||
ddb0303f | 176 | const res = await this.confirmService.confirm(message, title) |
177 | if (res === false) return false | |
1f30a185 C |
178 | |
179 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
180 | .subscribe( | |
181 | () => { | |
e00aa353 C |
182 | if (this.highlightedThread?.id === commentToDelete.id) { |
183 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
184 | ||
185 | this.highlightedThread = undefined | |
186 | } | |
187 | ||
69222afa JM |
188 | // Mark the comment as deleted |
189 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
190 | }, |
191 | ||
f8b2c1b4 | 192 | err => this.notifier.error(err.message) |
1f30a185 | 193 | ) |
f63c03fb | 194 | |
195 | return true | |
196 | } | |
197 | ||
ddb0303f | 198 | async onWantedToRedraft (commentToRedraft: VideoComment) { |
199 | const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`) | |
f63c03fb | 200 | |
201 | if (confirm) { | |
202 | this.inReplyToCommentId = commentToRedraft.inReplyToCommentId | |
203 | ||
110d463f | 204 | // Restore line feed for editing |
6a9498e3 K |
205 | const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n') |
206 | ||
f63c03fb | 207 | if (commentToRedraft.threadId === commentToRedraft.id) { |
6a9498e3 | 208 | this.commentThreadRedraftValue = commentToRedraftText |
f63c03fb | 209 | } else { |
6a9498e3 | 210 | this.commentReplyRedraftValue = commentToRedraftText |
f63c03fb | 211 | } |
212 | ||
213 | } | |
4cb6d457 C |
214 | } |
215 | ||
4635f59d C |
216 | isUserLoggedIn () { |
217 | return this.authService.isLoggedIn() | |
218 | } | |
7416fbf3 | 219 | |
0503e975 | 220 | onNearOfBottom () { |
2f1548fd | 221 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 222 | this.componentPagination.currentPage++ |
93cae479 | 223 | this.loadMoreThreads() |
7416fbf3 C |
224 | } |
225 | } | |
226 | ||
69222afa JM |
227 | private softDeleteComment (comment: VideoComment) { |
228 | comment.isDeleted = true | |
229 | comment.deletedAt = new Date() | |
230 | comment.text = '' | |
231 | comment.account = null | |
4cb6d457 | 232 | } |
339632b4 | 233 | |
1263fc4e | 234 | private resetVideo () { |
339632b4 C |
235 | if (this.video.commentsEnabled === true) { |
236 | // Reset all our fields | |
5b8072ee | 237 | this.highlightedThread = null |
339632b4 C |
238 | this.comments = [] |
239 | this.threadComments = {} | |
240 | this.threadLoading = {} | |
241 | this.inReplyToCommentId = undefined | |
0cd4344f C |
242 | this.componentPagination.currentPage = 1 |
243 | this.componentPagination.totalItems = null | |
339632b4 | 244 | |
53877968 | 245 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
93cae479 | 246 | this.loadMoreThreads() |
339632b4 C |
247 | } |
248 | } | |
1263fc4e | 249 | |
5b8072ee C |
250 | private processHighlightedThread (highlightedThreadId: number) { |
251 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 252 | |
5b8072ee C |
253 | const highlightThread = true |
254 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 255 | } |
4635f59d | 256 | } |