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