]>
Commit | Line | Data |
---|---|---|
b29bf61d | 1 | import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild, Output, EventEmitter } 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' |
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 { |
f36da21e | 23 | @ViewChild('commentHighlightBlock', { static: false }) 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 |
7b87d2d5 | 31 | sort: VideoSortField = '-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 | |
99 | ||
bf079b7b C |
100 | if (highlightThread) { |
101 | this.highlightedThread = new VideoComment(res.comment) | |
102 | ||
103 | // Scroll to the highlighted thread | |
43483d12 | 104 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) |
bf079b7b | 105 | } |
4635f59d C |
106 | }, |
107 | ||
f8b2c1b4 | 108 | err => this.notifier.error(err.message) |
4635f59d C |
109 | ) |
110 | } | |
111 | ||
93cae479 C |
112 | loadMoreThreads () { |
113 | const params = { | |
114 | videoId: this.video.id, | |
115 | componentPagination: this.componentPagination, | |
116 | sort: this.sort | |
117 | } | |
7416fbf3 | 118 | |
93cae479 C |
119 | const obs = this.hooks.wrapObsFun( |
120 | this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService), | |
121 | params, | |
122 | 'video-watch', | |
123 | 'filter:api.video-watch.video-threads.list.params', | |
124 | 'filter:api.video-watch.video-threads.list.result' | |
125 | ) | |
126 | ||
127 | obs.subscribe( | |
128 | res => { | |
e8f902c0 C |
129 | this.comments = this.comments.concat(res.data) |
130 | this.componentPagination.totalItems = res.total | |
ad453580 C |
131 | |
132 | this.onDataSubject.next(res.data) | |
93cae479 C |
133 | }, |
134 | ||
135 | err => this.notifier.error(err.message) | |
136 | ) | |
7416fbf3 C |
137 | } |
138 | ||
4635f59d C |
139 | onCommentThreadCreated (comment: VideoComment) { |
140 | this.comments.unshift(comment) | |
141 | } | |
142 | ||
143 | onWantedToReply (comment: VideoComment) { | |
144 | this.inReplyToCommentId = comment.id | |
145 | } | |
146 | ||
147 | onResetReply () { | |
148 | this.inReplyToCommentId = undefined | |
149 | } | |
150 | ||
4cb6d457 | 151 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 152 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 153 | } |
b29bf61d RK |
154 | |
155 | handleTimestampClicked (timestamp: number) { | |
156 | this.timestampClicked.emit(timestamp) | |
157 | } | |
4cb6d457 | 158 | |
1f30a185 | 159 | async onWantedToDelete (commentToDelete: VideoComment) { |
4cb6d457 | 160 | let message = 'Do you really want to delete this comment?' |
7e73f071 | 161 | |
7e73f071 | 162 | if (commentToDelete.isLocal) { |
32d7f2b7 | 163 | message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.') |
7e73f071 C |
164 | } else { |
165 | message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.') | |
166 | } | |
167 | ||
b1d40cff | 168 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) |
1f30a185 C |
169 | if (res === false) return |
170 | ||
171 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
172 | .subscribe( | |
173 | () => { | |
69222afa JM |
174 | // Mark the comment as deleted |
175 | this.softDeleteComment(commentToDelete) | |
73dc4da0 C |
176 | |
177 | if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined | |
1f30a185 C |
178 | }, |
179 | ||
f8b2c1b4 | 180 | err => this.notifier.error(err.message) |
1f30a185 | 181 | ) |
4cb6d457 C |
182 | } |
183 | ||
4635f59d C |
184 | isUserLoggedIn () { |
185 | return this.authService.isLoggedIn() | |
186 | } | |
7416fbf3 C |
187 | |
188 | onNearOfBottom () { | |
189 | this.componentPagination.currentPage++ | |
190 | ||
2f1548fd | 191 | if (hasMoreItems(this.componentPagination)) { |
93cae479 | 192 | this.loadMoreThreads() |
7416fbf3 C |
193 | } |
194 | } | |
195 | ||
69222afa JM |
196 | private softDeleteComment (comment: VideoComment) { |
197 | comment.isDeleted = true | |
198 | comment.deletedAt = new Date() | |
199 | comment.text = '' | |
200 | comment.account = null | |
4cb6d457 | 201 | } |
339632b4 | 202 | |
1263fc4e | 203 | private resetVideo () { |
339632b4 C |
204 | if (this.video.commentsEnabled === true) { |
205 | // Reset all our fields | |
5b8072ee | 206 | this.highlightedThread = null |
339632b4 C |
207 | this.comments = [] |
208 | this.threadComments = {} | |
209 | this.threadLoading = {} | |
210 | this.inReplyToCommentId = undefined | |
0cd4344f C |
211 | this.componentPagination.currentPage = 1 |
212 | this.componentPagination.totalItems = null | |
339632b4 | 213 | |
53877968 C |
214 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
215 | ||
93cae479 | 216 | this.loadMoreThreads() |
339632b4 C |
217 | } |
218 | } | |
1263fc4e | 219 | |
5b8072ee C |
220 | private processHighlightedThread (highlightedThreadId: number) { |
221 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 222 | |
5b8072ee C |
223 | const highlightThread = true |
224 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 225 | } |
4635f59d | 226 | } |