]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comments.component.ts
Better typings
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
1 import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild, ElementRef } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { ConfirmService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { Subscription } from 'rxjs'
6 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
7 import { AuthService } from '../../../core/auth'
8 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
9 import { User } from '../../../shared/users'
10 import { VideoSortField } from '../../../shared/video/sort-field.type'
11 import { VideoDetails } from '../../../shared/video/video-details.model'
12 import { VideoComment } from './video-comment.model'
13 import { VideoCommentService } from './video-comment.service'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { Syndication } from '@app/shared/video/syndication.model'
16
17 @Component({
18 selector: 'my-video-comments',
19 templateUrl: './video-comments.component.html',
20 styleUrls: ['./video-comments.component.scss']
21 })
22 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
23 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
24 @Input() video: VideoDetails
25 @Input() user: User
26
27 comments: VideoComment[] = []
28 highlightedThread: VideoComment
29 sort: VideoSortField = '-createdAt'
30 componentPagination: ComponentPagination = {
31 currentPage: 1,
32 itemsPerPage: 10,
33 totalItems: null
34 }
35 inReplyToCommentId: number
36 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
37 threadLoading: { [ id: number ]: boolean } = {}
38
39 syndicationItems: Syndication[] = []
40
41 private sub: Subscription
42
43 constructor (
44 private authService: AuthService,
45 private notificationsService: NotificationsService,
46 private confirmService: ConfirmService,
47 private videoCommentService: VideoCommentService,
48 private activatedRoute: ActivatedRoute,
49 private i18n: I18n
50 ) {}
51
52 ngOnInit () {
53 // Find highlighted comment in params
54 this.sub = this.activatedRoute.params.subscribe(
55 params => {
56 if (params['threadId']) {
57 const highlightedThreadId = +params['threadId']
58 this.processHighlightedThread(highlightedThreadId)
59 }
60 }
61 )
62 }
63
64 ngOnChanges (changes: SimpleChanges) {
65 if (changes['video']) {
66 this.resetVideo()
67 }
68 }
69
70 ngOnDestroy () {
71 if (this.sub) this.sub.unsubscribe()
72 }
73
74 viewReplies (commentId: number, highlightThread = false) {
75 this.threadLoading[commentId] = true
76
77 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
78 .subscribe(
79 res => {
80 this.threadComments[commentId] = res
81 this.threadLoading[commentId] = false
82
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
89 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
90 window.scroll(0, scrollY)
91 }, 500)
92 }
93 },
94
95 err => this.notificationsService.error(this.i18n('Error'), err.message)
96 )
97 }
98
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
107 err => this.notificationsService.error(this.i18n('Error'), err.message)
108 )
109 }
110
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
123 onThreadCreated (commentTree: VideoCommentThreadTree) {
124 this.viewReplies(commentTree.comment.id)
125 }
126
127 async onWantedToDelete (commentToDelete: VideoComment) {
128 let message = 'Do you really want to delete this comment?'
129 if (commentToDelete.totalReplies !== 0) {
130 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
131 }
132
133 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
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--
154
155 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
156 },
157
158 err => this.notificationsService.error(this.i18n('Error'), err.message)
159 )
160 }
161
162 isUserLoggedIn () {
163 return this.authService.isLoggedIn()
164 }
165
166 onNearOfBottom () {
167 this.componentPagination.currentPage++
168
169 if (this.hasMoreComments()) {
170 this.loadMoreComments()
171 }
172 }
173
174 private hasMoreComments () {
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 }
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 }
195
196 private resetVideo () {
197 if (this.video.commentsEnabled === true) {
198 // Reset all our fields
199 this.highlightedThread = null
200 this.comments = []
201 this.threadComments = {}
202 this.threadLoading = {}
203 this.inReplyToCommentId = undefined
204 this.componentPagination.currentPage = 1
205 this.componentPagination.totalItems = null
206
207 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
208
209 this.loadMoreComments()
210 }
211 }
212
213 private processHighlightedThread (highlightedThreadId: number) {
214 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
215
216 const highlightThread = true
217 this.viewReplies(highlightedThreadId, highlightThread)
218 }
219 }