]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/shared/comment/video-comments.component.ts
video-watch.video-threads.loaded after DOM loading
[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
9 @Component({
10 selector: 'my-video-comments',
11 templateUrl: './video-comments.component.html',
12 styleUrls: [ './video-comments.component.scss' ]
13 })
14 export 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
24 sort = '-createdAt'
25
26 componentPagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10,
29 totalItems: null
30 }
31 totalNotDeletedComments: number
32
33 inReplyToCommentId: number
34 commentReplyRedraftValue: string
35 commentThreadRedraftValue: string
36
37 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
38 threadLoading: { [ id: number ]: boolean } = {}
39
40 syndicationItems: Syndication[] = []
41
42 onDataSubject = new Subject<any[]>()
43
44 private sub: Subscription
45
46 constructor (
47 private authService: AuthService,
48 private notifier: Notifier,
49 private confirmService: ConfirmService,
50 private videoCommentService: VideoCommentService,
51 private activatedRoute: ActivatedRoute,
52 private hooks: HooksService
53 ) {}
54
55 ngOnInit () {
56 // Find highlighted comment in params
57 this.sub = this.activatedRoute.params.subscribe(
58 params => {
59 if (params['threadId']) {
60 const highlightedThreadId = +params['threadId']
61 this.processHighlightedThread(highlightedThreadId)
62 }
63 }
64 )
65 }
66
67 ngOnChanges (changes: SimpleChanges) {
68 if (changes['video']) {
69 this.resetVideo()
70 }
71 }
72
73 ngOnDestroy () {
74 if (this.sub) this.sub.unsubscribe()
75 }
76
77 viewReplies (commentId: number, highlightThread = false) {
78 this.threadLoading[commentId] = true
79
80 const params = {
81 videoId: this.video.uuid,
82 threadId: commentId
83 }
84
85 const obs = this.hooks.wrapObsFun(
86 this.videoCommentService.getVideoThreadComments.bind(this.videoCommentService),
87 params,
88 'video-watch',
89 'filter:api.video-watch.video-thread-replies.list.params',
90 'filter:api.video-watch.video-thread-replies.list.result'
91 )
92
93 obs.subscribe({
94 next: res => {
95 this.threadComments[commentId] = res
96 this.threadLoading[commentId] = false
97 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
98
99 if (highlightThread) {
100 this.highlightedThread = new VideoComment(res.comment)
101
102 // Scroll to the highlighted thread
103 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
104 }
105 },
106
107 error: err => this.notifier.error(err.message)
108 })
109 }
110
111 loadMoreThreads () {
112 const params = {
113 videoId: this.video.uuid,
114 componentPagination: this.componentPagination,
115 sort: this.sort
116 }
117
118 const obs = this.hooks.wrapObsFun(
119 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
120 params,
121 'video-watch',
122 'filter:api.video-watch.video-threads.list.params',
123 'filter:api.video-watch.video-threads.list.result'
124 )
125
126 obs.subscribe({
127 next: res => {
128 this.comments = this.comments.concat(res.data)
129 this.componentPagination.totalItems = res.total
130 this.totalNotDeletedComments = res.totalNotDeletedComments
131
132 this.onDataSubject.next(res.data)
133
134 setTimeout(() => this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination }))
135 },
136
137 error: 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 next: () => {
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 error: err => this.notifier.error(err.message)
198 })
199
200 return true
201 }
202
203 async onWantedToRedraft (commentToRedraft: VideoComment) {
204 const confirm = await this.onWantedToDelete(
205 commentToRedraft,
206 $localize`Delete and re-draft`,
207 $localize`Do you really want to delete and re-draft this comment?`
208 )
209
210 if (confirm) {
211 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
212
213 // Restore line feed for editing
214 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
215
216 if (commentToRedraft.threadId === commentToRedraft.id) {
217 this.commentThreadRedraftValue = commentToRedraftText
218 } else {
219 this.commentReplyRedraftValue = commentToRedraftText
220 }
221
222 }
223 }
224
225 isUserLoggedIn () {
226 return this.authService.isLoggedIn()
227 }
228
229 onNearOfBottom () {
230 if (hasMoreItems(this.componentPagination)) {
231 this.componentPagination.currentPage++
232 this.loadMoreThreads()
233 }
234 }
235
236 private softDeleteComment (comment: VideoComment) {
237 comment.isDeleted = true
238 comment.deletedAt = new Date()
239 comment.text = ''
240 comment.account = null
241 }
242
243 private resetVideo () {
244 if (this.video.commentsEnabled === true) {
245 // Reset all our fields
246 this.highlightedThread = null
247 this.comments = []
248 this.threadComments = {}
249 this.threadLoading = {}
250 this.inReplyToCommentId = undefined
251 this.componentPagination.currentPage = 1
252 this.componentPagination.totalItems = null
253 this.totalNotDeletedComments = null
254
255 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
256 this.loadMoreThreads()
257 }
258 }
259
260 private processHighlightedThread (highlightedThreadId: number) {
261 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
262
263 const highlightThread = true
264 this.viewReplies(highlightedThreadId, highlightThread)
265 }
266 }