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