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