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