]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comments.component.ts
Add ability for uploaders to schedule video update
[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
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 private sub: Subscription
39
40 constructor (
41 private authService: AuthService,
42 private notificationsService: NotificationsService,
43 private confirmService: ConfirmService,
44 private videoCommentService: VideoCommentService,
45 private activatedRoute: ActivatedRoute,
46 private i18n: I18n
47 ) {}
48
49 ngOnInit () {
50 // Find highlighted comment in params
51 this.sub = this.activatedRoute.params.subscribe(
52 params => {
53 if (params['threadId']) {
54 const highlightedThreadId = +params['threadId']
55 this.processHighlightedThread(highlightedThreadId)
56 }
57 }
58 )
59 }
60
61 ngOnChanges (changes: SimpleChanges) {
62 if (changes['video']) {
63 this.resetVideo()
64 }
65 }
66
67 ngOnDestroy () {
68 if (this.sub) this.sub.unsubscribe()
69 }
70
71 viewReplies (commentId: number, highlightThread = false) {
72 this.threadLoading[commentId] = true
73
74 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
75 .subscribe(
76 res => {
77 this.threadComments[commentId] = res
78 this.threadLoading[commentId] = false
79
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
86 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
87 window.scroll(0, scrollY)
88 }, 500)
89 }
90 },
91
92 err => this.notificationsService.error(this.i18n('Error'), err.message)
93 )
94 }
95
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
104 err => this.notificationsService.error(this.i18n('Error'), err.message)
105 )
106 }
107
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
120 onThreadCreated (commentTree: VideoCommentThreadTree) {
121 this.viewReplies(commentTree.comment.id)
122 }
123
124 async onWantedToDelete (commentToDelete: VideoComment) {
125 let message = 'Do you really want to delete this comment?'
126 if (commentToDelete.totalReplies !== 0) {
127 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
128 }
129
130 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
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
153 err => this.notificationsService.error(this.i18n('Error'), err.message)
154 )
155 }
156
157 isUserLoggedIn () {
158 return this.authService.isLoggedIn()
159 }
160
161 onNearOfBottom () {
162 this.componentPagination.currentPage++
163
164 if (this.hasMoreComments()) {
165 this.loadMoreComments()
166 }
167 }
168
169 private hasMoreComments () {
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 }
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 }
190
191 private resetVideo () {
192 if (this.video.commentsEnabled === true) {
193 // Reset all our fields
194 this.highlightedThread = null
195 this.comments = []
196 this.threadComments = {}
197 this.threadLoading = {}
198 this.inReplyToCommentId = undefined
199 this.componentPagination.currentPage = 1
200 this.componentPagination.totalItems = null
201
202 this.loadMoreComments()
203 }
204 }
205
206 private processHighlightedThread (highlightedThreadId: number) {
207 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
208
209 const highlightThread = true
210 this.viewReplies(highlightedThreadId, highlightThread)
211 }
212 }