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