]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
67ed6552 1import { Subject, Subscription } from 'rxjs'
be27ef3b 2import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
1263fc4e 3import { ActivatedRoute } from '@angular/router'
67ed6552
C
4import { AuthService, ComponentPagination, ConfirmService, hasMoreItems, Notifier, User } from '@app/core'
5import { HooksService } from '@app/core/plugins/hooks.service'
6import { Syndication, VideoDetails } from '@app/shared/shared-main'
cfde28ba 7import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
19b163d0 8import { ThisReceiver } from '@angular/compiler'
4635f59d
C
9
10@Component({
11 selector: 'my-video-comments',
12 templateUrl: './video-comments.component.html',
13 styleUrls: ['./video-comments.component.scss']
14})
1263fc4e 15export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
2f5d2ec5 16 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
47564bbe 17 @Input() video: VideoDetails
4635f59d
C
18 @Input() user: User
19
b29bf61d
RK
20 @Output() timestampClicked = new EventEmitter<number>()
21
4635f59d 22 comments: VideoComment[] = []
5b8072ee 23 highlightedThread: VideoComment
9d6b9d10 24
67ed6552 25 sort = '-createdAt'
9d6b9d10 26
4635f59d
C
27 componentPagination: ComponentPagination = {
28 currentPage: 1,
7416fbf3 29 itemsPerPage: 10,
4635f59d
C
30 totalItems: null
31 }
9d6b9d10
C
32 totalNotDeletedComments: number
33
4635f59d 34 inReplyToCommentId: number
f63c03fb 35 commentReplyRedraftValue: string
36 commentThreadRedraftValue: string
9d6b9d10 37
4635f59d
C
38 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
39 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e 40
c199c427 41 syndicationItems: Syndication[] = []
53877968 42
ad453580
C
43 onDataSubject = new Subject<any[]>()
44
1263fc4e 45 private sub: Subscription
4635f59d
C
46
47 constructor (
48 private authService: AuthService,
f8b2c1b4 49 private notifier: Notifier,
4cb6d457 50 private confirmService: ConfirmService,
d5b53822 51 private videoCommentService: VideoCommentService,
b1d40cff 52 private activatedRoute: ActivatedRoute,
93cae479 53 private hooks: HooksService
4635f59d
C
54 ) {}
55
1263fc4e
C
56 ngOnInit () {
57 // Find highlighted comment in params
58 this.sub = this.activatedRoute.params.subscribe(
59 params => {
5b8072ee
C
60 if (params['threadId']) {
61 const highlightedThreadId = +params['threadId']
62 this.processHighlightedThread(highlightedThreadId)
1263fc4e
C
63 }
64 }
65 )
66 }
67
339632b4
C
68 ngOnChanges (changes: SimpleChanges) {
69 if (changes['video']) {
1263fc4e 70 this.resetVideo()
47564bbe 71 }
4635f59d
C
72 }
73
1263fc4e
C
74 ngOnDestroy () {
75 if (this.sub) this.sub.unsubscribe()
76 }
77
5b8072ee 78 viewReplies (commentId: number, highlightThread = false) {
1263fc4e 79 this.threadLoading[commentId] = true
4635f59d 80
93cae479
C
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(
4635f59d 95 res => {
1263fc4e
C
96 this.threadComments[commentId] = res
97 this.threadLoading[commentId] = false
cf387740 98 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
1263fc4e 99
bf079b7b
C
100 if (highlightThread) {
101 this.highlightedThread = new VideoComment(res.comment)
102
103 // Scroll to the highlighted thread
43483d12 104 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
bf079b7b 105 }
4635f59d
C
106 },
107
f8b2c1b4 108 err => this.notifier.error(err.message)
4635f59d
C
109 )
110 }
111
93cae479
C
112 loadMoreThreads () {
113 const params = {
114 videoId: this.video.id,
115 componentPagination: this.componentPagination,
116 sort: this.sort
117 }
7416fbf3 118
93cae479
C
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 => {
e8f902c0 129 this.comments = this.comments.concat(res.data)
9d6b9d10
C
130 this.componentPagination.totalItems = res.total
131 this.totalNotDeletedComments = res.totalNotDeletedComments
ad453580
C
132
133 this.onDataSubject.next(res.data)
0840ca76 134 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
93cae479
C
135 },
136
137 err => this.notifier.error(err.message)
138 )
7416fbf3
C
139 }
140
4635f59d
C
141 onCommentThreadCreated (comment: VideoComment) {
142 this.comments.unshift(comment)
fdd12965 143 this.commentThreadRedraftValue = undefined
4635f59d
C
144 }
145
146 onWantedToReply (comment: VideoComment) {
147 this.inReplyToCommentId = comment.id
148 }
149
150 onResetReply () {
151 this.inReplyToCommentId = undefined
fdd12965 152 this.commentReplyRedraftValue = undefined
4635f59d
C
153 }
154
4cb6d457 155 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 156 this.viewReplies(commentTree.comment.id)
4cb6d457 157 }
b29bf61d 158
67ed6552 159 handleSortChange (sort: string) {
c1125bca
RK
160 if (this.sort === sort) return
161
162 this.sort = sort
163 this.resetVideo()
164 }
165
b29bf61d
RK
166 handleTimestampClicked (timestamp: number) {
167 this.timestampClicked.emit(timestamp)
168 }
4cb6d457 169
93e903ac
C
170 async onWantedToDelete (
171 commentToDelete: VideoComment,
172 title = $localize`Delete`,
173 message = $localize`Do you really want to delete this comment?`
174 ): Promise<boolean> {
e00aa353 175 if (commentToDelete.isLocal || this.video.isLocal) {
66357162 176 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
7e73f071 177 } else {
66357162 178 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
7e73f071
C
179 }
180
ddb0303f 181 const res = await this.confirmService.confirm(message, title)
182 if (res === false) return false
1f30a185
C
183
184 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
185 .subscribe(
186 () => {
e00aa353
C
187 if (this.highlightedThread?.id === commentToDelete.id) {
188 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
189
190 this.highlightedThread = undefined
191 }
192
69222afa
JM
193 // Mark the comment as deleted
194 this.softDeleteComment(commentToDelete)
1f30a185
C
195 },
196
f8b2c1b4 197 err => this.notifier.error(err.message)
1f30a185 198 )
f63c03fb 199
200 return true
201 }
202
ddb0303f 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?`)
f63c03fb 205
206 if (confirm) {
207 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
208
110d463f 209 // Restore line feed for editing
6a9498e3
K
210 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
211
f63c03fb 212 if (commentToRedraft.threadId === commentToRedraft.id) {
6a9498e3 213 this.commentThreadRedraftValue = commentToRedraftText
f63c03fb 214 } else {
6a9498e3 215 this.commentReplyRedraftValue = commentToRedraftText
f63c03fb 216 }
217
218 }
4cb6d457
C
219 }
220
4635f59d
C
221 isUserLoggedIn () {
222 return this.authService.isLoggedIn()
223 }
7416fbf3 224
0503e975 225 onNearOfBottom () {
2f1548fd 226 if (hasMoreItems(this.componentPagination)) {
34102d19 227 this.componentPagination.currentPage++
93cae479 228 this.loadMoreThreads()
7416fbf3
C
229 }
230 }
231
69222afa
JM
232 private softDeleteComment (comment: VideoComment) {
233 comment.isDeleted = true
234 comment.deletedAt = new Date()
235 comment.text = ''
236 comment.account = null
4cb6d457 237 }
339632b4 238
1263fc4e 239 private resetVideo () {
339632b4
C
240 if (this.video.commentsEnabled === true) {
241 // Reset all our fields
5b8072ee 242 this.highlightedThread = null
339632b4
C
243 this.comments = []
244 this.threadComments = {}
245 this.threadLoading = {}
246 this.inReplyToCommentId = undefined
0cd4344f
C
247 this.componentPagination.currentPage = 1
248 this.componentPagination.totalItems = null
9d6b9d10 249 this.totalNotDeletedComments = null
339632b4 250
53877968 251 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
93cae479 252 this.loadMoreThreads()
339632b4
C
253 }
254 }
1263fc4e 255
5b8072ee
C
256 private processHighlightedThread (highlightedThreadId: number) {
257 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 258
5b8072ee
C
259 const highlightThread = true
260 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 261 }
4635f59d 262}