]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/shared/comment/video-comments.component.ts
Prevent error on highlighted thread
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / comment / video-comments.component.ts
1 import { Subject, Subscription } from 'rxjs'
2 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, User } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { Syndication, VideoDetails } from '@app/shared/shared-main'
7 import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
8 import { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
9
10 @Component({
11 selector: 'my-video-comments',
12 templateUrl: './video-comments.component.html',
13 styleUrls: [ './video-comments.component.scss' ]
14 })
15 export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
16 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
17 @Input() video: VideoDetails
18 @Input() user: User
19
20 @Output() timestampClicked = new EventEmitter<number>()
21
22 comments: VideoComment[] = []
23 highlightedThread: VideoComment
24
25 sort = '-createdAt'
26
27 componentPagination: ComponentPagination = {
28 currentPage: 1,
29 itemsPerPage: 10,
30 totalItems: null
31 }
32 totalNotDeletedComments: number
33
34 inReplyToCommentId: number
35 commentReplyRedraftValue: string
36 commentThreadRedraftValue: string
37
38 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
39 threadLoading: { [ id: number ]: boolean } = {}
40
41 syndicationItems: Syndication[] = []
42
43 onDataSubject = new Subject<any[]>()
44
45 private sub: Subscription
46
47 constructor (
48 private authService: AuthService,
49 private notifier: Notifier,
50 private confirmService: ConfirmService,
51 private videoCommentService: VideoCommentService,
52 private activatedRoute: ActivatedRoute,
53 private hooks: HooksService
54 ) {}
55
56 ngOnInit () {
57 // Find highlighted comment in params
58 this.sub = this.activatedRoute.params.subscribe(
59 params => {
60 if (params['threadId']) {
61 const highlightedThreadId = +params['threadId']
62 this.processHighlightedThread(highlightedThreadId)
63 }
64 }
65 )
66 }
67
68 ngOnChanges (changes: SimpleChanges) {
69 if (changes['video']) {
70 this.resetVideo()
71 }
72 }
73
74 ngOnDestroy () {
75 if (this.sub) this.sub.unsubscribe()
76 }
77
78 viewReplies (commentId: number, highlightThread = false) {
79 this.threadLoading[commentId] = true
80
81 const params = {
82 videoId: this.video.uuid,
83 threadId: commentId
84 }
85
86 const obs = this.hooks.wrapObsFun(
87 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
88 params,
89 'video-watch',
90 'filter:api.video-watch.video-thread-replies.list.params',
91 'filter:api.video-watch.video-thread-replies.list.result'
92 )
93
94 obs.subscribe({
95 next: res => {
96 this.threadComments[commentId] = res
97 this.threadLoading[commentId] = false
98 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
99
100 if (highlightThread) {
101 this.highlightedThread = new VideoComment(res.comment)
102
103 // Scroll to the highlighted thread
104 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
105 }
106 },
107
108 error: err => {
109 // We may try to fetch highlighted thread of another video, skip the error if it is the case
110 // We'll retry the request on video Input() change
111 const errorBody = err.body as PeerTubeProblemDocument
112 if (highlightThread && errorBody?.code === ServerErrorCode.COMMENT_NOT_ASSOCIATED_TO_VIDEO) return
113
114 this.notifier.error(err.message)
115 }
116 })
117 }
118
119 loadMoreThreads () {
120 const params = {
121 videoId: this.video.uuid,
122 componentPagination: this.componentPagination,
123 sort: this.sort
124 }
125
126 const obs = this.hooks.wrapObsFun(
127 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
128 params,
129 'video-watch',
130 'filter:api.video-watch.video-threads.list.params',
131 'filter:api.video-watch.video-threads.list.result'
132 )
133
134 obs.subscribe({
135 next: res => {
136 this.comments = this.comments.concat(res.data)
137 this.componentPagination.totalItems = res.total
138 this.totalNotDeletedComments = res.totalNotDeletedComments
139
140 this.onDataSubject.next(res.data)
141
142 setTimeout(() => this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }))
143 },
144
145 error: err => this.notifier.error(err.message)
146 })
147 }
148
149 onCommentThreadCreated (comment: VideoComment) {
150 this.comments.unshift(comment)
151 this.commentThreadRedraftValue = undefined
152 }
153
154 onWantedToReply (comment: VideoComment) {
155 this.inReplyToCommentId = comment.id
156 }
157
158 onResetReply () {
159 this.inReplyToCommentId = undefined
160 this.commentReplyRedraftValue = undefined
161 }
162
163 onThreadCreated (commentTree: VideoCommentThreadTree) {
164 this.viewReplies(commentTree.comment.id)
165 }
166
167 handleSortChange (sort: string) {
168 if (this.sort === sort) return
169
170 this.sort = sort
171 this.resetVideo()
172 }
173
174 handleTimestampClicked (timestamp: number) {
175 this.timestampClicked.emit(timestamp)
176 }
177
178 async onWantedToDelete (
179 commentToDelete: VideoComment,
180 title = $localize`Delete`,
181 message = $localize`Do you really want to delete this comment?`
182 ): Promise<boolean> {
183 if (commentToDelete.isLocal || this.video.isLocal) {
184 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
185 } else {
186 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
187 }
188
189 const res = await this.confirmService.confirm(message, title)
190 if (res === false) return false
191
192 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
193 .subscribe({
194 next: () => {
195 if (this.highlightedThread?.id === commentToDelete.id) {
196 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
197
198 this.highlightedThread = undefined
199 }
200
201 // Mark the comment as deleted
202 this.softDeleteComment(commentToDelete)
203 },
204
205 error: err => this.notifier.error(err.message)
206 })
207
208 return true
209 }
210
211 async onWantedToRedraft (commentToRedraft: VideoComment) {
212 const confirm = await this.onWantedToDelete(
213 commentToRedraft,
214 $localize`Delete and re-draft`,
215 $localize`Do you really want to delete and re-draft this comment?`
216 )
217
218 if (confirm) {
219 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
220
221 // Restore line feed for editing
222 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
223
224 if (commentToRedraft.threadId === commentToRedraft.id) {
225 this.commentThreadRedraftValue = commentToRedraftText
226 } else {
227 this.commentReplyRedraftValue = commentToRedraftText
228 }
229
230 }
231 }
232
233 isUserLoggedIn () {
234 return this.authService.isLoggedIn()
235 }
236
237 onNearOfBottom () {
238 if (hasMoreItems(this.componentPagination)) {
239 this.componentPagination.currentPage++
240 this.loadMoreThreads()
241 }
242 }
243
244 private softDeleteComment (comment: VideoComment) {
245 comment.isDeleted = true
246 comment.deletedAt = new Date()
247 comment.text = ''
248 comment.account = null
249 }
250
251 private resetVideo () {
252 if (this.video.commentsEnabled === true) {
253 // Reset all our fields
254 this.highlightedThread = null
255 this.comments = []
256 this.threadComments = {}
257 this.threadLoading = {}
258 this.inReplyToCommentId = undefined
259 this.componentPagination.currentPage = 1
260 this.componentPagination.totalItems = null
261 this.totalNotDeletedComments = null
262
263 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
264 this.loadMoreThreads()
265
266 if (this.activatedRoute.params['threadId']) {
267 this.processHighlightedThread(+this.activatedRoute.params['threadId'])
268 }
269 }
270 }
271
272 private processHighlightedThread (highlightedThreadId: number) {
273 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
274
275 const highlightThread = true
276 this.viewReplies(highlightedThreadId, highlightThread)
277 }
278 }