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