]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comments.component.ts
Fix video comments display with deleted comments
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / 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 { ThisReceiver } from '@angular/compiler'
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.id,
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 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 err => this.notifier.error(err.message)
109 )
110 }
111
112 loadMoreThreads () {
113 const params = {
114 videoId: this.video.id,
115 componentPagination: this.componentPagination,
116 sort: this.sort
117 }
118
119 const obs = this.hooks.wrapObsFun(
120 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
121 params,
122 'video-watch',
123 'filter:api.video-watch.video-threads.list.params',
124 'filter:api.video-watch.video-threads.list.result'
125 )
126
127 obs.subscribe(
128 res => {
129 this.comments = this.comments.concat(res.data)
130 this.componentPagination.totalItems = res.total
131 this.totalNotDeletedComments = res.totalNotDeletedComments
132
133 this.onDataSubject.next(res.data)
134 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
135 },
136
137 err => this.notifier.error(err.message)
138 )
139 }
140
141 onCommentThreadCreated (comment: VideoComment) {
142 this.comments.unshift(comment)
143 this.commentThreadRedraftValue = undefined
144 }
145
146 onWantedToReply (comment: VideoComment) {
147 this.inReplyToCommentId = comment.id
148 }
149
150 onResetReply () {
151 this.inReplyToCommentId = undefined
152 this.commentReplyRedraftValue = undefined
153 }
154
155 onThreadCreated (commentTree: VideoCommentThreadTree) {
156 this.viewReplies(commentTree.comment.id)
157 }
158
159 handleSortChange (sort: string) {
160 if (this.sort === sort) return
161
162 this.sort = sort
163 this.resetVideo()
164 }
165
166 handleTimestampClicked (timestamp: number) {
167 this.timestampClicked.emit(timestamp)
168 }
169
170 async onWantedToDelete (
171 commentToDelete: VideoComment,
172 title = $localize`Delete`,
173 message = $localize`Do you really want to delete this comment?`
174 ): Promise<boolean> {
175 if (commentToDelete.isLocal || this.video.isLocal) {
176 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
177 } else {
178 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
179 }
180
181 const res = await this.confirmService.confirm(message, title)
182 if (res === false) return false
183
184 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
185 .subscribe(
186 () => {
187 if (this.highlightedThread?.id === commentToDelete.id) {
188 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
189
190 this.highlightedThread = undefined
191 }
192
193 // Mark the comment as deleted
194 this.softDeleteComment(commentToDelete)
195 },
196
197 err => this.notifier.error(err.message)
198 )
199
200 return true
201 }
202
203 async onWantedToRedraft (commentToRedraft: VideoComment) {
204 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
205
206 if (confirm) {
207 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
208
209 // Restore line feed for editing
210 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
211
212 if (commentToRedraft.threadId === commentToRedraft.id) {
213 this.commentThreadRedraftValue = commentToRedraftText
214 } else {
215 this.commentReplyRedraftValue = commentToRedraftText
216 }
217
218 }
219 }
220
221 isUserLoggedIn () {
222 return this.authService.isLoggedIn()
223 }
224
225 onNearOfBottom () {
226 if (hasMoreItems(this.componentPagination)) {
227 this.componentPagination.currentPage++
228 this.loadMoreThreads()
229 }
230 }
231
232 private softDeleteComment (comment: VideoComment) {
233 comment.isDeleted = true
234 comment.deletedAt = new Date()
235 comment.text = ''
236 comment.account = null
237 }
238
239 private resetVideo () {
240 if (this.video.commentsEnabled === true) {
241 // Reset all our fields
242 this.highlightedThread = null
243 this.comments = []
244 this.threadComments = {}
245 this.threadLoading = {}
246 this.inReplyToCommentId = undefined
247 this.componentPagination.currentPage = 1
248 this.componentPagination.totalItems = null
249 this.totalNotDeletedComments = null
250
251 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
252 this.loadMoreThreads()
253 }
254 }
255
256 private processHighlightedThread (highlightedThreadId: number) {
257 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
258
259 const highlightThread = true
260 this.viewReplies(highlightedThreadId, highlightThread)
261 }
262 }