]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subject, Subscription } from 'rxjs' |
be27ef3b | 2 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
1263fc4e | 3 | import { ActivatedRoute } from '@angular/router' |
67ed6552 C |
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' | |
cfde28ba | 7 | import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment' |
4635f59d C |
8 | |
9 | @Component({ | |
10 | selector: 'my-video-comments', | |
11 | templateUrl: './video-comments.component.html', | |
9df52d66 | 12 | styleUrls: [ './video-comments.component.scss' ] |
4635f59d | 13 | }) |
1263fc4e | 14 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { |
2f5d2ec5 | 15 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef |
47564bbe | 16 | @Input() video: VideoDetails |
4635f59d C |
17 | @Input() user: User |
18 | ||
b29bf61d RK |
19 | @Output() timestampClicked = new EventEmitter<number>() |
20 | ||
4635f59d | 21 | comments: VideoComment[] = [] |
5b8072ee | 22 | highlightedThread: VideoComment |
9d6b9d10 | 23 | |
67ed6552 | 24 | sort = '-createdAt' |
9d6b9d10 | 25 | |
4635f59d C |
26 | componentPagination: ComponentPagination = { |
27 | currentPage: 1, | |
7416fbf3 | 28 | itemsPerPage: 10, |
4635f59d C |
29 | totalItems: null |
30 | } | |
9d6b9d10 C |
31 | totalNotDeletedComments: number |
32 | ||
4635f59d | 33 | inReplyToCommentId: number |
f63c03fb | 34 | commentReplyRedraftValue: string |
35 | commentThreadRedraftValue: string | |
9d6b9d10 | 36 | |
4635f59d C |
37 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} |
38 | threadLoading: { [ id: number ]: boolean } = {} | |
1263fc4e | 39 | |
c199c427 | 40 | syndicationItems: Syndication[] = [] |
53877968 | 41 | |
ad453580 C |
42 | onDataSubject = new Subject<any[]>() |
43 | ||
1263fc4e | 44 | private sub: Subscription |
4635f59d C |
45 | |
46 | constructor ( | |
47 | private authService: AuthService, | |
f8b2c1b4 | 48 | private notifier: Notifier, |
4cb6d457 | 49 | private confirmService: ConfirmService, |
d5b53822 | 50 | private videoCommentService: VideoCommentService, |
b1d40cff | 51 | private activatedRoute: ActivatedRoute, |
93cae479 | 52 | private hooks: HooksService |
4635f59d C |
53 | ) {} |
54 | ||
1263fc4e C |
55 | ngOnInit () { |
56 | // Find highlighted comment in params | |
57 | this.sub = this.activatedRoute.params.subscribe( | |
58 | params => { | |
5b8072ee C |
59 | if (params['threadId']) { |
60 | const highlightedThreadId = +params['threadId'] | |
61 | this.processHighlightedThread(highlightedThreadId) | |
1263fc4e C |
62 | } |
63 | } | |
64 | ) | |
65 | } | |
66 | ||
339632b4 C |
67 | ngOnChanges (changes: SimpleChanges) { |
68 | if (changes['video']) { | |
1263fc4e | 69 | this.resetVideo() |
47564bbe | 70 | } |
4635f59d C |
71 | } |
72 | ||
1263fc4e C |
73 | ngOnDestroy () { |
74 | if (this.sub) this.sub.unsubscribe() | |
75 | } | |
76 | ||
5b8072ee | 77 | viewReplies (commentId: number, highlightThread = false) { |
1263fc4e | 78 | this.threadLoading[commentId] = true |
4635f59d | 79 | |
93cae479 | 80 | const params = { |
7c07259a | 81 | videoId: this.video.uuid, |
93cae479 C |
82 | threadId: commentId |
83 | } | |
84 | ||
85 | const obs = this.hooks.wrapObsFun( | |
86 | this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService), | |
87 | params, | |
88 | 'video-watch', | |
89 | 'filter:api.video-watch.video-thread-replies.list.params', | |
90 | 'filter:api.video-watch.video-thread-replies.list.result' | |
91 | ) | |
92 | ||
1378c0d3 C |
93 | obs.subscribe({ |
94 | next: res => { | |
95 | this.threadComments[commentId] = res | |
96 | this.threadLoading[commentId] = false | |
97 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) | |
1263fc4e | 98 | |
1378c0d3 C |
99 | if (highlightThread) { |
100 | this.highlightedThread = new VideoComment(res.comment) | |
bf079b7b | 101 | |
1378c0d3 C |
102 | // Scroll to the highlighted thread |
103 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) | |
104 | } | |
105 | }, | |
4635f59d | 106 | |
1378c0d3 C |
107 | error: err => this.notifier.error(err.message) |
108 | }) | |
4635f59d C |
109 | } |
110 | ||
93cae479 C |
111 | loadMoreThreads () { |
112 | const params = { | |
7c07259a | 113 | videoId: this.video.uuid, |
93cae479 C |
114 | componentPagination: this.componentPagination, |
115 | sort: this.sort | |
116 | } | |
7416fbf3 | 117 | |
93cae479 C |
118 | const obs = this.hooks.wrapObsFun( |
119 | this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService), | |
120 | params, | |
121 | 'video-watch', | |
122 | 'filter:api.video-watch.video-threads.list.params', | |
123 | 'filter:api.video-watch.video-threads.list.result' | |
124 | ) | |
125 | ||
1378c0d3 C |
126 | obs.subscribe({ |
127 | next: res => { | |
e8f902c0 | 128 | this.comments = this.comments.concat(res.data) |
9d6b9d10 C |
129 | this.componentPagination.totalItems = res.total |
130 | this.totalNotDeletedComments = res.totalNotDeletedComments | |
ad453580 C |
131 | |
132 | this.onDataSubject.next(res.data) | |
d65cd165 C |
133 | |
134 | setTimeout(() => this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })) | |
93cae479 C |
135 | }, |
136 | ||
1378c0d3 C |
137 | error: err => this.notifier.error(err.message) |
138 | }) | |
7416fbf3 C |
139 | } |
140 | ||
4635f59d C |
141 | onCommentThreadCreated (comment: VideoComment) { |
142 | this.comments.unshift(comment) | |
fdd12965 | 143 | this.commentThreadRedraftValue = undefined |
4635f59d C |
144 | } |
145 | ||
146 | onWantedToReply (comment: VideoComment) { | |
147 | this.inReplyToCommentId = comment.id | |
148 | } | |
149 | ||
150 | onResetReply () { | |
151 | this.inReplyToCommentId = undefined | |
fdd12965 | 152 | this.commentReplyRedraftValue = undefined |
4635f59d C |
153 | } |
154 | ||
4cb6d457 | 155 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 156 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 157 | } |
b29bf61d | 158 | |
67ed6552 | 159 | handleSortChange (sort: string) { |
c1125bca RK |
160 | if (this.sort === sort) return |
161 | ||
162 | this.sort = sort | |
163 | this.resetVideo() | |
164 | } | |
165 | ||
b29bf61d RK |
166 | handleTimestampClicked (timestamp: number) { |
167 | this.timestampClicked.emit(timestamp) | |
168 | } | |
4cb6d457 | 169 | |
93e903ac C |
170 | async onWantedToDelete ( |
171 | commentToDelete: VideoComment, | |
172 | title = $localize`Delete`, | |
173 | message = $localize`Do you really want to delete this comment?` | |
174 | ): Promise<boolean> { | |
e00aa353 | 175 | if (commentToDelete.isLocal || this.video.isLocal) { |
66357162 | 176 | message += $localize` The deletion will be sent to remote instances so they can reflect the change.` |
7e73f071 | 177 | } else { |
66357162 | 178 | message += $localize` It is a remote comment, so the deletion will only be effective on your instance.` |
7e73f071 C |
179 | } |
180 | ||
ddb0303f | 181 | const res = await this.confirmService.confirm(message, title) |
182 | if (res === false) return false | |
1f30a185 C |
183 | |
184 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
1378c0d3 C |
185 | .subscribe({ |
186 | next: () => { | |
e00aa353 C |
187 | if (this.highlightedThread?.id === commentToDelete.id) { |
188 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
189 | ||
190 | this.highlightedThread = undefined | |
191 | } | |
192 | ||
69222afa JM |
193 | // Mark the comment as deleted |
194 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
195 | }, |
196 | ||
1378c0d3 C |
197 | error: err => this.notifier.error(err.message) |
198 | }) | |
f63c03fb | 199 | |
200 | return true | |
201 | } | |
202 | ||
ddb0303f | 203 | async onWantedToRedraft (commentToRedraft: VideoComment) { |
9df52d66 C |
204 | const confirm = await this.onWantedToDelete( |
205 | commentToRedraft, | |
206 | $localize`Delete and re-draft`, | |
207 | $localize`Do you really want to delete and re-draft this comment?` | |
208 | ) | |
f63c03fb | 209 | |
210 | if (confirm) { | |
211 | this.inReplyToCommentId = commentToRedraft.inReplyToCommentId | |
212 | ||
110d463f | 213 | // Restore line feed for editing |
6a9498e3 K |
214 | const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n') |
215 | ||
f63c03fb | 216 | if (commentToRedraft.threadId === commentToRedraft.id) { |
6a9498e3 | 217 | this.commentThreadRedraftValue = commentToRedraftText |
f63c03fb | 218 | } else { |
6a9498e3 | 219 | this.commentReplyRedraftValue = commentToRedraftText |
f63c03fb | 220 | } |
221 | ||
222 | } | |
4cb6d457 C |
223 | } |
224 | ||
4635f59d C |
225 | isUserLoggedIn () { |
226 | return this.authService.isLoggedIn() | |
227 | } | |
7416fbf3 | 228 | |
0503e975 | 229 | onNearOfBottom () { |
2f1548fd | 230 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 231 | this.componentPagination.currentPage++ |
93cae479 | 232 | this.loadMoreThreads() |
7416fbf3 C |
233 | } |
234 | } | |
235 | ||
69222afa JM |
236 | private softDeleteComment (comment: VideoComment) { |
237 | comment.isDeleted = true | |
238 | comment.deletedAt = new Date() | |
239 | comment.text = '' | |
240 | comment.account = null | |
4cb6d457 | 241 | } |
339632b4 | 242 | |
1263fc4e | 243 | private resetVideo () { |
339632b4 C |
244 | if (this.video.commentsEnabled === true) { |
245 | // Reset all our fields | |
5b8072ee | 246 | this.highlightedThread = null |
339632b4 C |
247 | this.comments = [] |
248 | this.threadComments = {} | |
249 | this.threadLoading = {} | |
250 | this.inReplyToCommentId = undefined | |
0cd4344f C |
251 | this.componentPagination.currentPage = 1 |
252 | this.componentPagination.totalItems = null | |
9d6b9d10 | 253 | this.totalNotDeletedComments = null |
339632b4 | 254 | |
d4a8e7a6 | 255 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video) |
93cae479 | 256 | this.loadMoreThreads() |
339632b4 C |
257 | } |
258 | } | |
1263fc4e | 259 | |
5b8072ee C |
260 | private processHighlightedThread (highlightedThreadId: number) { |
261 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 262 | |
5b8072ee C |
263 | const highlightThread = true |
264 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 265 | } |
4635f59d | 266 | } |