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