]>
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) | |
f63c03fb | 136 | delete this.commentThreadRedraftValue |
4635f59d C |
137 | } |
138 | ||
139 | onWantedToReply (comment: VideoComment) { | |
140 | this.inReplyToCommentId = comment.id | |
141 | } | |
142 | ||
143 | onResetReply () { | |
144 | this.inReplyToCommentId = undefined | |
f63c03fb | 145 | delete this.commentReplyRedraftValue |
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 | |
ddb0303f | 163 | async onWantedToDelete(commentToDelete: VideoComment, title = $localize`Delete`, message = $localize`Do you really want to delete this comment?`): Promise<boolean> { |
e00aa353 | 164 | if (commentToDelete.isLocal || this.video.isLocal) { |
66357162 | 165 | message += $localize` The deletion will be sent to remote instances so they can reflect the change.` |
7e73f071 | 166 | } else { |
66357162 | 167 | message += $localize` It is a remote comment, so the deletion will only be effective on your instance.` |
7e73f071 C |
168 | } |
169 | ||
ddb0303f | 170 | const res = await this.confirmService.confirm(message, title) |
171 | if (res === false) return false | |
1f30a185 C |
172 | |
173 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
174 | .subscribe( | |
175 | () => { | |
e00aa353 C |
176 | if (this.highlightedThread?.id === commentToDelete.id) { |
177 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
178 | ||
179 | this.highlightedThread = undefined | |
180 | } | |
181 | ||
69222afa JM |
182 | // Mark the comment as deleted |
183 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
184 | }, |
185 | ||
f8b2c1b4 | 186 | err => this.notifier.error(err.message) |
1f30a185 | 187 | ) |
f63c03fb | 188 | |
189 | return true | |
190 | } | |
191 | ||
ddb0303f | 192 | async onWantedToRedraft (commentToRedraft: VideoComment) { |
193 | const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`) | |
f63c03fb | 194 | |
195 | if (confirm) { | |
196 | this.inReplyToCommentId = commentToRedraft.inReplyToCommentId | |
197 | ||
198 | if (commentToRedraft.threadId === commentToRedraft.id) { | |
199 | this.commentThreadRedraftValue = commentToRedraft.text | |
200 | } else { | |
201 | this.commentReplyRedraftValue = commentToRedraft.text | |
202 | } | |
203 | ||
204 | } | |
4cb6d457 C |
205 | } |
206 | ||
4635f59d C |
207 | isUserLoggedIn () { |
208 | return this.authService.isLoggedIn() | |
209 | } | |
7416fbf3 | 210 | |
0503e975 | 211 | onNearOfBottom () { |
2f1548fd | 212 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 213 | this.componentPagination.currentPage++ |
93cae479 | 214 | this.loadMoreThreads() |
7416fbf3 C |
215 | } |
216 | } | |
217 | ||
69222afa JM |
218 | private softDeleteComment (comment: VideoComment) { |
219 | comment.isDeleted = true | |
220 | comment.deletedAt = new Date() | |
221 | comment.text = '' | |
222 | comment.account = null | |
4cb6d457 | 223 | } |
339632b4 | 224 | |
1263fc4e | 225 | private resetVideo () { |
339632b4 C |
226 | if (this.video.commentsEnabled === true) { |
227 | // Reset all our fields | |
5b8072ee | 228 | this.highlightedThread = null |
339632b4 C |
229 | this.comments = [] |
230 | this.threadComments = {} | |
231 | this.threadLoading = {} | |
232 | this.inReplyToCommentId = undefined | |
0cd4344f C |
233 | this.componentPagination.currentPage = 1 |
234 | this.componentPagination.totalItems = null | |
339632b4 | 235 | |
53877968 | 236 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
93cae479 | 237 | this.loadMoreThreads() |
339632b4 C |
238 | } |
239 | } | |
1263fc4e | 240 | |
5b8072ee C |
241 | private processHighlightedThread (highlightedThreadId: number) { |
242 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 243 | |
5b8072ee C |
244 | const highlightThread = true |
245 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 246 | } |
4635f59d | 247 | } |