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