]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comments.component.ts
5bafc55e5fa474c560f336424cbd0f70f696b039
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
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, hasMoreItems } 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 import { HooksService } from '@app/core/plugins/hooks.service'
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 notifier: Notifier,
46 private confirmService: ConfirmService,
47 private videoCommentService: VideoCommentService,
48 private activatedRoute: ActivatedRoute,
49 private i18n: I18n,
50 private hooks: HooksService
51 ) {}
52
53 ngOnInit () {
54 // Find highlighted comment in params
55 this.sub = this.activatedRoute.params.subscribe(
56 params => {
57 if (params['threadId']) {
58 const highlightedThreadId = +params['threadId']
59 this.processHighlightedThread(highlightedThreadId)
60 }
61 }
62 )
63 }
64
65 ngOnChanges (changes: SimpleChanges) {
66 if (changes['video']) {
67 this.resetVideo()
68 }
69 }
70
71 ngOnDestroy () {
72 if (this.sub) this.sub.unsubscribe()
73 }
74
75 viewReplies (commentId: number, highlightThread = false) {
76 this.threadLoading[commentId] = true
77
78 const params = {
79 videoId: this.video.id,
80 threadId: commentId
81 }
82
83 const obs = this.hooks.wrapObsFun(
84 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
85 params,
86 'video-watch',
87 'filter:api.video-watch.video-thread-replies.list.params',
88 'filter:api.video-watch.video-thread-replies.list.result'
89 )
90
91 obs.subscribe(
92 res => {
93 this.threadComments[commentId] = res
94 this.threadLoading[commentId] = false
95
96 if (highlightThread) {
97 this.highlightedThread = new VideoComment(res.comment)
98
99 // Scroll to the highlighted thread
100 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
101 }
102 },
103
104 err => this.notifier.error(err.message)
105 )
106 }
107
108 loadMoreThreads () {
109 const params = {
110 videoId: this.video.id,
111 componentPagination: this.componentPagination,
112 sort: this.sort
113 }
114
115 const obs = this.hooks.wrapObsFun(
116 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
117 params,
118 'video-watch',
119 'filter:api.video-watch.video-threads.list.params',
120 'filter:api.video-watch.video-threads.list.result'
121 )
122
123 obs.subscribe(
124 res => {
125 this.comments = this.comments.concat(res.data)
126 this.componentPagination.totalItems = res.total
127 },
128
129 err => this.notifier.error(err.message)
130 )
131 }
132
133 onCommentThreadCreated (comment: VideoComment) {
134 this.comments.unshift(comment)
135 }
136
137 onWantedToReply (comment: VideoComment) {
138 this.inReplyToCommentId = comment.id
139 }
140
141 onResetReply () {
142 this.inReplyToCommentId = undefined
143 }
144
145 onThreadCreated (commentTree: VideoCommentThreadTree) {
146 this.viewReplies(commentTree.comment.id)
147 }
148
149 async onWantedToDelete (commentToDelete: VideoComment) {
150 let message = 'Do you really want to delete this comment?'
151
152 if (commentToDelete.totalReplies !== 0) {
153 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
154 }
155
156 if (commentToDelete.isLocal) {
157 message += this.i18n(' The deletion will be sent to remote instances so they remove the comment too.')
158 } else {
159 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
160 }
161
162 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
163 if (res === false) return
164
165 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
166 .subscribe(
167 () => {
168 // Delete the comment in the tree
169 if (commentToDelete.inReplyToCommentId) {
170 const thread = this.threadComments[commentToDelete.threadId]
171 if (!thread) {
172 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
173 return
174 }
175
176 this.deleteLocalCommentThread(thread, commentToDelete)
177 return
178 }
179
180 // Delete the thread
181 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
182 this.componentPagination.totalItems--
183
184 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
185 },
186
187 err => this.notifier.error(err.message)
188 )
189 }
190
191 isUserLoggedIn () {
192 return this.authService.isLoggedIn()
193 }
194
195 onNearOfBottom () {
196 this.componentPagination.currentPage++
197
198 if (hasMoreItems(this.componentPagination)) {
199 this.loadMoreThreads()
200 }
201 }
202
203 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
204 for (const commentChild of parentComment.children) {
205 if (commentChild.comment.id === commentToDelete.id) {
206 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
207 return
208 }
209
210 this.deleteLocalCommentThread(commentChild, commentToDelete)
211 }
212 }
213
214 private resetVideo () {
215 if (this.video.commentsEnabled === true) {
216 // Reset all our fields
217 this.highlightedThread = null
218 this.comments = []
219 this.threadComments = {}
220 this.threadLoading = {}
221 this.inReplyToCommentId = undefined
222 this.componentPagination.currentPage = 1
223 this.componentPagination.totalItems = null
224
225 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
226
227 this.loadMoreThreads()
228 }
229 }
230
231 private processHighlightedThread (highlightedThreadId: number) {
232 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
233
234 const highlightThread = true
235 this.viewReplies(highlightedThreadId, highlightThread)
236 }
237 }