]>
Commit | Line | Data |
---|---|---|
be27ef3b | 1 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
1263fc4e | 2 | import { ActivatedRoute } from '@angular/router' |
f8b2c1b4 | 3 | import { ConfirmService, Notifier } from '@app/core' |
ad453580 | 4 | import { Subject, Subscription } from 'rxjs' |
4635f59d | 5 | import { AuthService } from '../../../core/auth' |
2f1548fd | 6 | import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model' |
4635f59d | 7 | import { User } from '../../../shared/users' |
c1125bca | 8 | import { CommentSortField } from '../../../shared/video/sort-field.type' |
47564bbe | 9 | import { VideoDetails } from '../../../shared/video/video-details.model' |
4635f59d C |
10 | import { VideoComment } from './video-comment.model' |
11 | import { VideoCommentService } from './video-comment.service' | |
b1d40cff | 12 | import { I18n } from '@ngx-translate/i18n-polyfill' |
c199c427 | 13 | import { Syndication } from '@app/shared/video/syndication.model' |
93cae479 | 14 | import { HooksService } from '@app/core/plugins/hooks.service' |
be27ef3b | 15 | import { VideoCommentThreadTree } from '@app/videos/+video-watch/comment/video-comment-thread-tree.model' |
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 { |
2f5d2ec5 | 23 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef |
47564bbe | 24 | @Input() video: VideoDetails |
4635f59d C |
25 | @Input() user: User |
26 | ||
b29bf61d RK |
27 | @Output() timestampClicked = new EventEmitter<number>() |
28 | ||
4635f59d | 29 | comments: VideoComment[] = [] |
5b8072ee | 30 | highlightedThread: VideoComment |
c1125bca | 31 | sort: CommentSortField = '-createdAt' |
4635f59d C |
32 | componentPagination: ComponentPagination = { |
33 | currentPage: 1, | |
7416fbf3 | 34 | itemsPerPage: 10, |
4635f59d C |
35 | totalItems: null |
36 | } | |
37 | inReplyToCommentId: number | |
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 C |
53 | private i18n: I18n, |
54 | private hooks: HooksService | |
4635f59d C |
55 | ) {} |
56 | ||
1263fc4e C |
57 | ngOnInit () { |
58 | // Find highlighted comment in params | |
59 | this.sub = this.activatedRoute.params.subscribe( | |
60 | params => { | |
5b8072ee C |
61 | if (params['threadId']) { |
62 | const highlightedThreadId = +params['threadId'] | |
63 | this.processHighlightedThread(highlightedThreadId) | |
1263fc4e C |
64 | } |
65 | } | |
66 | ) | |
67 | } | |
68 | ||
339632b4 C |
69 | ngOnChanges (changes: SimpleChanges) { |
70 | if (changes['video']) { | |
1263fc4e | 71 | this.resetVideo() |
47564bbe | 72 | } |
4635f59d C |
73 | } |
74 | ||
1263fc4e C |
75 | ngOnDestroy () { |
76 | if (this.sub) this.sub.unsubscribe() | |
77 | } | |
78 | ||
5b8072ee | 79 | viewReplies (commentId: number, highlightThread = false) { |
1263fc4e | 80 | this.threadLoading[commentId] = true |
4635f59d | 81 | |
93cae479 C |
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( | |
4635f59d | 96 | res => { |
1263fc4e C |
97 | this.threadComments[commentId] = res |
98 | this.threadLoading[commentId] = false | |
cf387740 | 99 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) |
1263fc4e | 100 | |
bf079b7b C |
101 | if (highlightThread) { |
102 | this.highlightedThread = new VideoComment(res.comment) | |
103 | ||
104 | // Scroll to the highlighted thread | |
43483d12 | 105 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) |
bf079b7b | 106 | } |
4635f59d C |
107 | }, |
108 | ||
f8b2c1b4 | 109 | err => this.notifier.error(err.message) |
4635f59d C |
110 | ) |
111 | } | |
112 | ||
93cae479 C |
113 | loadMoreThreads () { |
114 | const params = { | |
115 | videoId: this.video.id, | |
116 | componentPagination: this.componentPagination, | |
117 | sort: this.sort | |
118 | } | |
7416fbf3 | 119 | |
93cae479 C |
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 => { | |
e8f902c0 C |
130 | this.comments = this.comments.concat(res.data) |
131 | this.componentPagination.totalItems = res.total | |
ad453580 C |
132 | |
133 | this.onDataSubject.next(res.data) | |
0840ca76 | 134 | this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }) |
93cae479 C |
135 | }, |
136 | ||
137 | err => this.notifier.error(err.message) | |
138 | ) | |
7416fbf3 C |
139 | } |
140 | ||
4635f59d C |
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 | ||
4cb6d457 | 153 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 154 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 155 | } |
b29bf61d | 156 | |
c1125bca RK |
157 | handleSortChange (sort: CommentSortField) { |
158 | if (this.sort === sort) return | |
159 | ||
160 | this.sort = sort | |
161 | this.resetVideo() | |
162 | } | |
163 | ||
b29bf61d RK |
164 | handleTimestampClicked (timestamp: number) { |
165 | this.timestampClicked.emit(timestamp) | |
166 | } | |
4cb6d457 | 167 | |
1f30a185 | 168 | async onWantedToDelete (commentToDelete: VideoComment) { |
4cb6d457 | 169 | let message = 'Do you really want to delete this comment?' |
7e73f071 | 170 | |
e00aa353 | 171 | if (commentToDelete.isLocal || this.video.isLocal) { |
41a94d07 | 172 | message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.') |
7e73f071 C |
173 | } else { |
174 | message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.') | |
175 | } | |
176 | ||
b1d40cff | 177 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) |
1f30a185 C |
178 | if (res === false) return |
179 | ||
180 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
181 | .subscribe( | |
182 | () => { | |
e00aa353 C |
183 | if (this.highlightedThread?.id === commentToDelete.id) { |
184 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
185 | ||
186 | this.highlightedThread = undefined | |
187 | } | |
188 | ||
69222afa JM |
189 | // Mark the comment as deleted |
190 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
191 | }, |
192 | ||
f8b2c1b4 | 193 | err => this.notifier.error(err.message) |
1f30a185 | 194 | ) |
4cb6d457 C |
195 | } |
196 | ||
4635f59d C |
197 | isUserLoggedIn () { |
198 | return this.authService.isLoggedIn() | |
199 | } | |
7416fbf3 | 200 | |
0503e975 | 201 | onNearOfBottom () { |
2f1548fd | 202 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 203 | this.componentPagination.currentPage++ |
93cae479 | 204 | this.loadMoreThreads() |
7416fbf3 C |
205 | } |
206 | } | |
207 | ||
69222afa JM |
208 | private softDeleteComment (comment: VideoComment) { |
209 | comment.isDeleted = true | |
210 | comment.deletedAt = new Date() | |
211 | comment.text = '' | |
212 | comment.account = null | |
4cb6d457 | 213 | } |
339632b4 | 214 | |
1263fc4e | 215 | private resetVideo () { |
339632b4 C |
216 | if (this.video.commentsEnabled === true) { |
217 | // Reset all our fields | |
5b8072ee | 218 | this.highlightedThread = null |
339632b4 C |
219 | this.comments = [] |
220 | this.threadComments = {} | |
221 | this.threadLoading = {} | |
222 | this.inReplyToCommentId = undefined | |
0cd4344f C |
223 | this.componentPagination.currentPage = 1 |
224 | this.componentPagination.totalItems = null | |
339632b4 | 225 | |
53877968 | 226 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
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 | } |