]>
Commit | Line | Data |
---|---|---|
1 | import { Subject, Subscription } from 'rxjs' | |
2 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' | |
3 | import { ActivatedRoute } from '@angular/router' | |
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' | |
7 | import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment' | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-video-comments', | |
11 | templateUrl: './video-comments.component.html', | |
12 | styleUrls: ['./video-comments.component.scss'] | |
13 | }) | |
14 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { | |
15 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef | |
16 | @Input() video: VideoDetails | |
17 | @Input() user: User | |
18 | ||
19 | @Output() timestampClicked = new EventEmitter<number>() | |
20 | ||
21 | comments: VideoComment[] = [] | |
22 | highlightedThread: VideoComment | |
23 | sort = '-createdAt' | |
24 | componentPagination: ComponentPagination = { | |
25 | currentPage: 1, | |
26 | itemsPerPage: 10, | |
27 | totalItems: null | |
28 | } | |
29 | inReplyToCommentId: number | |
30 | commentReplyRedraftValue: string | |
31 | commentThreadRedraftValue: string | |
32 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | |
33 | threadLoading: { [ id: number ]: boolean } = {} | |
34 | ||
35 | syndicationItems: Syndication[] = [] | |
36 | ||
37 | onDataSubject = new Subject<any[]>() | |
38 | ||
39 | private sub: Subscription | |
40 | ||
41 | constructor ( | |
42 | private authService: AuthService, | |
43 | private notifier: Notifier, | |
44 | private confirmService: ConfirmService, | |
45 | private videoCommentService: VideoCommentService, | |
46 | private activatedRoute: ActivatedRoute, | |
47 | private hooks: HooksService | |
48 | ) {} | |
49 | ||
50 | ngOnInit () { | |
51 | // Find highlighted comment in params | |
52 | this.sub = this.activatedRoute.params.subscribe( | |
53 | params => { | |
54 | if (params['threadId']) { | |
55 | const highlightedThreadId = +params['threadId'] | |
56 | this.processHighlightedThread(highlightedThreadId) | |
57 | } | |
58 | } | |
59 | ) | |
60 | } | |
61 | ||
62 | ngOnChanges (changes: SimpleChanges) { | |
63 | if (changes['video']) { | |
64 | this.resetVideo() | |
65 | } | |
66 | } | |
67 | ||
68 | ngOnDestroy () { | |
69 | if (this.sub) this.sub.unsubscribe() | |
70 | } | |
71 | ||
72 | viewReplies (commentId: number, highlightThread = false) { | |
73 | this.threadLoading[commentId] = true | |
74 | ||
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( | |
89 | res => { | |
90 | this.threadComments[commentId] = res | |
91 | this.threadLoading[commentId] = false | |
92 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) | |
93 | ||
94 | if (highlightThread) { | |
95 | this.highlightedThread = new VideoComment(res.comment) | |
96 | ||
97 | // Scroll to the highlighted thread | |
98 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) | |
99 | } | |
100 | }, | |
101 | ||
102 | err => this.notifier.error(err.message) | |
103 | ) | |
104 | } | |
105 | ||
106 | loadMoreThreads () { | |
107 | const params = { | |
108 | videoId: this.video.id, | |
109 | componentPagination: this.componentPagination, | |
110 | sort: this.sort | |
111 | } | |
112 | ||
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 => { | |
123 | this.comments = this.comments.concat(res.data) | |
124 | this.componentPagination.totalItems = res.total | |
125 | ||
126 | this.onDataSubject.next(res.data) | |
127 | this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }) | |
128 | }, | |
129 | ||
130 | err => this.notifier.error(err.message) | |
131 | ) | |
132 | } | |
133 | ||
134 | onCommentThreadCreated (comment: VideoComment) { | |
135 | this.comments.unshift(comment) | |
136 | this.commentThreadRedraftValue = undefined | |
137 | } | |
138 | ||
139 | onWantedToReply (comment: VideoComment) { | |
140 | this.inReplyToCommentId = comment.id | |
141 | } | |
142 | ||
143 | onResetReply () { | |
144 | this.inReplyToCommentId = undefined | |
145 | this.commentReplyRedraftValue = undefined | |
146 | } | |
147 | ||
148 | onThreadCreated (commentTree: VideoCommentThreadTree) { | |
149 | this.viewReplies(commentTree.comment.id) | |
150 | } | |
151 | ||
152 | handleSortChange (sort: string) { | |
153 | if (this.sort === sort) return | |
154 | ||
155 | this.sort = sort | |
156 | this.resetVideo() | |
157 | } | |
158 | ||
159 | handleTimestampClicked (timestamp: number) { | |
160 | this.timestampClicked.emit(timestamp) | |
161 | } | |
162 | ||
163 | async onWantedToDelete ( | |
164 | commentToDelete: VideoComment, | |
165 | title = $localize`Delete`, | |
166 | message = $localize`Do you really want to delete this comment?` | |
167 | ): Promise<boolean> { | |
168 | if (commentToDelete.isLocal || this.video.isLocal) { | |
169 | message += $localize` The deletion will be sent to remote instances so they can reflect the change.` | |
170 | } else { | |
171 | message += $localize` It is a remote comment, so the deletion will only be effective on your instance.` | |
172 | } | |
173 | ||
174 | const res = await this.confirmService.confirm(message, title) | |
175 | if (res === false) return false | |
176 | ||
177 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
178 | .subscribe( | |
179 | () => { | |
180 | if (this.highlightedThread?.id === commentToDelete.id) { | |
181 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
182 | ||
183 | this.highlightedThread = undefined | |
184 | } | |
185 | ||
186 | // Mark the comment as deleted | |
187 | this.softDeleteComment(commentToDelete) | |
188 | }, | |
189 | ||
190 | err => this.notifier.error(err.message) | |
191 | ) | |
192 | ||
193 | return true | |
194 | } | |
195 | ||
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?`) | |
198 | ||
199 | if (confirm) { | |
200 | this.inReplyToCommentId = commentToRedraft.inReplyToCommentId | |
201 | ||
202 | if (commentToRedraft.threadId === commentToRedraft.id) { | |
203 | this.commentThreadRedraftValue = commentToRedraft.text | |
204 | } else { | |
205 | this.commentReplyRedraftValue = commentToRedraft.text | |
206 | } | |
207 | ||
208 | } | |
209 | } | |
210 | ||
211 | isUserLoggedIn () { | |
212 | return this.authService.isLoggedIn() | |
213 | } | |
214 | ||
215 | onNearOfBottom () { | |
216 | if (hasMoreItems(this.componentPagination)) { | |
217 | this.componentPagination.currentPage++ | |
218 | this.loadMoreThreads() | |
219 | } | |
220 | } | |
221 | ||
222 | private softDeleteComment (comment: VideoComment) { | |
223 | comment.isDeleted = true | |
224 | comment.deletedAt = new Date() | |
225 | comment.text = '' | |
226 | comment.account = null | |
227 | } | |
228 | ||
229 | private resetVideo () { | |
230 | if (this.video.commentsEnabled === true) { | |
231 | // Reset all our fields | |
232 | this.highlightedThread = null | |
233 | this.comments = [] | |
234 | this.threadComments = {} | |
235 | this.threadLoading = {} | |
236 | this.inReplyToCommentId = undefined | |
237 | this.componentPagination.currentPage = 1 | |
238 | this.componentPagination.totalItems = null | |
239 | ||
240 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) | |
241 | this.loadMoreThreads() | |
242 | } | |
243 | } | |
244 | ||
245 | private processHighlightedThread (highlightedThreadId: number) { | |
246 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
247 | ||
248 | const highlightThread = true | |
249 | this.viewReplies(highlightedThreadId, highlightThread) | |
250 | } | |
251 | } |