]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core' | |
2 | import { ActivatedRoute } from '@angular/router' | |
3 | import { ConfirmService, Notifier } from '@app/core' | |
4 | import { Subscription } from 'rxjs' | |
5 | import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' | |
6 | import { AuthService } from '../../../core/auth' | |
7 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' | |
8 | import { User } from '../../../shared/users' | |
9 | import { VideoSortField } from '../../../shared/video/sort-field.type' | |
10 | import { VideoDetails } from '../../../shared/video/video-details.model' | |
11 | import { VideoComment } from './video-comment.model' | |
12 | import { VideoCommentService } from './video-comment.service' | |
13 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
14 | import { Syndication } from '@app/shared/video/syndication.model' | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-video-comments', | |
18 | templateUrl: './video-comments.component.html', | |
19 | styleUrls: ['./video-comments.component.scss'] | |
20 | }) | |
21 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { | |
22 | @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef | |
23 | @Input() video: VideoDetails | |
24 | @Input() user: User | |
25 | ||
26 | comments: VideoComment[] = [] | |
27 | highlightedThread: VideoComment | |
28 | sort: VideoSortField = '-createdAt' | |
29 | componentPagination: ComponentPagination = { | |
30 | currentPage: 1, | |
31 | itemsPerPage: 10, | |
32 | totalItems: null | |
33 | } | |
34 | inReplyToCommentId: number | |
35 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | |
36 | threadLoading: { [ id: number ]: boolean } = {} | |
37 | ||
38 | syndicationItems: Syndication[] = [] | |
39 | ||
40 | private sub: Subscription | |
41 | ||
42 | constructor ( | |
43 | private authService: AuthService, | |
44 | private notifier: Notifier, | |
45 | private confirmService: ConfirmService, | |
46 | private videoCommentService: VideoCommentService, | |
47 | private activatedRoute: ActivatedRoute, | |
48 | private i18n: I18n | |
49 | ) {} | |
50 | ||
51 | ngOnInit () { | |
52 | // Find highlighted comment in params | |
53 | this.sub = this.activatedRoute.params.subscribe( | |
54 | params => { | |
55 | if (params['threadId']) { | |
56 | const highlightedThreadId = +params['threadId'] | |
57 | this.processHighlightedThread(highlightedThreadId) | |
58 | } | |
59 | } | |
60 | ) | |
61 | } | |
62 | ||
63 | ngOnChanges (changes: SimpleChanges) { | |
64 | if (changes['video']) { | |
65 | this.resetVideo() | |
66 | } | |
67 | } | |
68 | ||
69 | ngOnDestroy () { | |
70 | if (this.sub) this.sub.unsubscribe() | |
71 | } | |
72 | ||
73 | viewReplies (commentId: number, highlightThread = false) { | |
74 | this.threadLoading[commentId] = true | |
75 | ||
76 | this.videoCommentService.getVideoThreadComments(this.video.id, commentId) | |
77 | .subscribe( | |
78 | res => { | |
79 | this.threadComments[commentId] = res | |
80 | this.threadLoading[commentId] = false | |
81 | ||
82 | if (highlightThread) { | |
83 | this.highlightedThread = new VideoComment(res.comment) | |
84 | ||
85 | // Scroll to the highlighted thread | |
86 | setTimeout(() => { | |
87 | // -60 because of the fixed header | |
88 | const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60 | |
89 | window.scroll(0, scrollY) | |
90 | }, 500) | |
91 | } | |
92 | }, | |
93 | ||
94 | err => this.notifier.error(err.message) | |
95 | ) | |
96 | } | |
97 | ||
98 | loadMoreComments () { | |
99 | this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort) | |
100 | .subscribe( | |
101 | res => { | |
102 | this.comments = this.comments.concat(res.comments) | |
103 | this.componentPagination.totalItems = res.totalComments | |
104 | }, | |
105 | ||
106 | err => this.notifier.error(err.message) | |
107 | ) | |
108 | } | |
109 | ||
110 | onCommentThreadCreated (comment: VideoComment) { | |
111 | this.comments.unshift(comment) | |
112 | } | |
113 | ||
114 | onWantedToReply (comment: VideoComment) { | |
115 | this.inReplyToCommentId = comment.id | |
116 | } | |
117 | ||
118 | onResetReply () { | |
119 | this.inReplyToCommentId = undefined | |
120 | } | |
121 | ||
122 | onThreadCreated (commentTree: VideoCommentThreadTree) { | |
123 | this.viewReplies(commentTree.comment.id) | |
124 | } | |
125 | ||
126 | async onWantedToDelete (commentToDelete: VideoComment) { | |
127 | let message = 'Do you really want to delete this comment?' | |
128 | if (commentToDelete.totalReplies !== 0) { | |
129 | message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies }) | |
130 | } | |
131 | ||
132 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
133 | if (res === false) return | |
134 | ||
135 | this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id) | |
136 | .subscribe( | |
137 | () => { | |
138 | // Delete the comment in the tree | |
139 | if (commentToDelete.inReplyToCommentId) { | |
140 | const thread = this.threadComments[commentToDelete.threadId] | |
141 | if (!thread) { | |
142 | console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`) | |
143 | return | |
144 | } | |
145 | ||
146 | this.deleteLocalCommentThread(thread, commentToDelete) | |
147 | return | |
148 | } | |
149 | ||
150 | // Delete the thread | |
151 | this.comments = this.comments.filter(c => c.id !== commentToDelete.id) | |
152 | this.componentPagination.totalItems-- | |
153 | ||
154 | if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined | |
155 | }, | |
156 | ||
157 | err => this.notifier.error(err.message) | |
158 | ) | |
159 | } | |
160 | ||
161 | isUserLoggedIn () { | |
162 | return this.authService.isLoggedIn() | |
163 | } | |
164 | ||
165 | onNearOfBottom () { | |
166 | this.componentPagination.currentPage++ | |
167 | ||
168 | if (this.hasMoreComments()) { | |
169 | this.loadMoreComments() | |
170 | } | |
171 | } | |
172 | ||
173 | private hasMoreComments () { | |
174 | // No results | |
175 | if (this.componentPagination.totalItems === 0) return false | |
176 | ||
177 | // Not loaded yet | |
178 | if (!this.componentPagination.totalItems) return true | |
179 | ||
180 | const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage | |
181 | return maxPage > this.componentPagination.currentPage | |
182 | } | |
183 | ||
184 | private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) { | |
185 | for (const commentChild of parentComment.children) { | |
186 | if (commentChild.comment.id === commentToDelete.id) { | |
187 | parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id) | |
188 | return | |
189 | } | |
190 | ||
191 | this.deleteLocalCommentThread(commentChild, commentToDelete) | |
192 | } | |
193 | } | |
194 | ||
195 | private resetVideo () { | |
196 | if (this.video.commentsEnabled === true) { | |
197 | // Reset all our fields | |
198 | this.highlightedThread = null | |
199 | this.comments = [] | |
200 | this.threadComments = {} | |
201 | this.threadLoading = {} | |
202 | this.inReplyToCommentId = undefined | |
203 | this.componentPagination.currentPage = 1 | |
204 | this.componentPagination.totalItems = null | |
205 | ||
206 | this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid) | |
207 | ||
208 | this.loadMoreComments() | |
209 | } | |
210 | } | |
211 | ||
212 | private processHighlightedThread (highlightedThreadId: number) { | |
213 | this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId) | |
214 | ||
215 | const highlightThread = true | |
216 | this.viewReplies(highlightedThreadId, highlightThread) | |
217 | } | |
218 | } |