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