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