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