]>
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', | |
12 | styleUrls: ['./video-comments.component.scss'] | |
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 |
67ed6552 | 23 | sort = '-createdAt' |
4635f59d C |
24 | componentPagination: ComponentPagination = { |
25 | currentPage: 1, | |
7416fbf3 | 26 | itemsPerPage: 10, |
4635f59d C |
27 | totalItems: null |
28 | } | |
29 | inReplyToCommentId: number | |
30 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | |
31 | threadLoading: { [ id: number ]: boolean } = {} | |
1263fc4e | 32 | |
c199c427 | 33 | syndicationItems: Syndication[] = [] |
53877968 | 34 | |
ad453580 C |
35 | onDataSubject = new Subject<any[]>() |
36 | ||
1263fc4e | 37 | private sub: Subscription |
4635f59d C |
38 | |
39 | constructor ( | |
40 | private authService: AuthService, | |
f8b2c1b4 | 41 | private notifier: Notifier, |
4cb6d457 | 42 | private confirmService: ConfirmService, |
d5b53822 | 43 | private videoCommentService: VideoCommentService, |
b1d40cff | 44 | private activatedRoute: ActivatedRoute, |
93cae479 | 45 | private hooks: HooksService |
4635f59d C |
46 | ) {} |
47 | ||
1263fc4e C |
48 | ngOnInit () { |
49 | // Find highlighted comment in params | |
50 | this.sub = this.activatedRoute.params.subscribe( | |
51 | params => { | |
5b8072ee C |
52 | if (params['threadId']) { |
53 | const highlightedThreadId = +params['threadId'] | |
54 | this.processHighlightedThread(highlightedThreadId) | |
1263fc4e C |
55 | } |
56 | } | |
57 | ) | |
58 | } | |
59 | ||
339632b4 C |
60 | ngOnChanges (changes: SimpleChanges) { |
61 | if (changes['video']) { | |
1263fc4e | 62 | this.resetVideo() |
47564bbe | 63 | } |
4635f59d C |
64 | } |
65 | ||
1263fc4e C |
66 | ngOnDestroy () { |
67 | if (this.sub) this.sub.unsubscribe() | |
68 | } | |
69 | ||
5b8072ee | 70 | viewReplies (commentId: number, highlightThread = false) { |
1263fc4e | 71 | this.threadLoading[commentId] = true |
4635f59d | 72 | |
93cae479 C |
73 | const params = { |
74 | videoId: this.video.id, | |
75 | threadId: commentId | |
76 | } | |
77 | ||
78 | const obs = this.hooks.wrapObsFun( | |
79 | this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService), | |
80 | params, | |
81 | 'video-watch', | |
82 | 'filter:api.video-watch.video-thread-replies.list.params', | |
83 | 'filter:api.video-watch.video-thread-replies.list.result' | |
84 | ) | |
85 | ||
86 | obs.subscribe( | |
4635f59d | 87 | res => { |
1263fc4e C |
88 | this.threadComments[commentId] = res |
89 | this.threadLoading[commentId] = false | |
cf387740 | 90 | this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res }) |
1263fc4e | 91 | |
bf079b7b C |
92 | if (highlightThread) { |
93 | this.highlightedThread = new VideoComment(res.comment) | |
94 | ||
95 | // Scroll to the highlighted thread | |
43483d12 | 96 | setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0) |
bf079b7b | 97 | } |
4635f59d C |
98 | }, |
99 | ||
f8b2c1b4 | 100 | err => this.notifier.error(err.message) |
4635f59d C |
101 | ) |
102 | } | |
103 | ||
93cae479 C |
104 | loadMoreThreads () { |
105 | const params = { | |
106 | videoId: this.video.id, | |
107 | componentPagination: this.componentPagination, | |
108 | sort: this.sort | |
109 | } | |
7416fbf3 | 110 | |
93cae479 C |
111 | const obs = this.hooks.wrapObsFun( |
112 | this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService), | |
113 | params, | |
114 | 'video-watch', | |
115 | 'filter:api.video-watch.video-threads.list.params', | |
116 | 'filter:api.video-watch.video-threads.list.result' | |
117 | ) | |
118 | ||
119 | obs.subscribe( | |
120 | res => { | |
e8f902c0 C |
121 | this.comments = this.comments.concat(res.data) |
122 | this.componentPagination.totalItems = res.total | |
ad453580 C |
123 | |
124 | this.onDataSubject.next(res.data) | |
0840ca76 | 125 | this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }) |
93cae479 C |
126 | }, |
127 | ||
128 | err => this.notifier.error(err.message) | |
129 | ) | |
7416fbf3 C |
130 | } |
131 | ||
4635f59d C |
132 | onCommentThreadCreated (comment: VideoComment) { |
133 | this.comments.unshift(comment) | |
134 | } | |
135 | ||
136 | onWantedToReply (comment: VideoComment) { | |
137 | this.inReplyToCommentId = comment.id | |
138 | } | |
139 | ||
140 | onResetReply () { | |
141 | this.inReplyToCommentId = undefined | |
142 | } | |
143 | ||
4cb6d457 | 144 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
1263fc4e | 145 | this.viewReplies(commentTree.comment.id) |
4cb6d457 | 146 | } |
b29bf61d | 147 | |
67ed6552 | 148 | handleSortChange (sort: string) { |
c1125bca RK |
149 | if (this.sort === sort) return |
150 | ||
151 | this.sort = sort | |
152 | this.resetVideo() | |
153 | } | |
154 | ||
b29bf61d RK |
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 | |
e00aa353 | 162 | if (commentToDelete.isLocal || this.video.isLocal) { |
66357162 | 163 | message += $localize` The deletion will be sent to remote instances so they can reflect the change.` |
7e73f071 | 164 | } else { |
66357162 | 165 | message += $localize` It is a remote comment, so the deletion will only be effective on your instance.` |
7e73f071 C |
166 | } |
167 | ||
66357162 | 168 | const res = await this.confirmService.confirm(message, $localize`Delete`) |
1f30a185 C |
169 | if (res === false) return |
170 | ||
171 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
172 | .subscribe( | |
173 | () => { | |
e00aa353 C |
174 | if (this.highlightedThread?.id === commentToDelete.id) { |
175 | commentToDelete = this.comments.find(c => c.id === commentToDelete.id) | |
176 | ||
177 | this.highlightedThread = undefined | |
178 | } | |
179 | ||
69222afa JM |
180 | // Mark the comment as deleted |
181 | this.softDeleteComment(commentToDelete) | |
1f30a185 C |
182 | }, |
183 | ||
f8b2c1b4 | 184 | err => this.notifier.error(err.message) |
1f30a185 | 185 | ) |
4cb6d457 C |
186 | } |
187 | ||
4635f59d C |
188 | isUserLoggedIn () { |
189 | return this.authService.isLoggedIn() | |
190 | } | |
7416fbf3 | 191 | |
0503e975 | 192 | onNearOfBottom () { |
2f1548fd | 193 | if (hasMoreItems(this.componentPagination)) { |
34102d19 | 194 | this.componentPagination.currentPage++ |
93cae479 | 195 | this.loadMoreThreads() |
7416fbf3 C |
196 | } |
197 | } | |
198 | ||
69222afa JM |
199 | private softDeleteComment (comment: VideoComment) { |
200 | comment.isDeleted = true | |
201 | comment.deletedAt = new Date() | |
202 | comment.text = '' | |
203 | comment.account = null | |
4cb6d457 | 204 | } |
339632b4 | 205 | |
1263fc4e | 206 | private resetVideo () { |
339632b4 C |
207 | if (this.video.commentsEnabled === true) { |
208 | // Reset all our fields | |
5b8072ee | 209 | this.highlightedThread = null |
339632b4 C |
210 | this.comments = [] |
211 | this.threadComments = {} | |
212 | this.threadLoading = {} | |
213 | this.inReplyToCommentId = undefined | |
0cd4344f C |
214 | this.componentPagination.currentPage = 1 |
215 | this.componentPagination.totalItems = null | |
339632b4 | 216 | |
53877968 | 217 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) |
93cae479 | 218 | this.loadMoreThreads() |
339632b4 C |
219 | } |
220 | } | |
1263fc4e | 221 | |
5b8072ee C |
222 | private processHighlightedThread (highlightedThreadId: number) { |
223 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
1263fc4e | 224 | |
5b8072ee C |
225 | const highlightThread = true |
226 | this.viewReplies(highlightedThreadId, highlightThread) | |
1263fc4e | 227 | } |
4635f59d | 228 | } |