]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comments.component.ts
Merge branch 'release/2.3.0' into develop
[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 { I18n } from '@ngx-translate/i18n-polyfill'
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 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
32 threadLoading: { [ id: number ]: boolean } = {}
33
34 syndicationItems: Syndication[] = []
35
36 onDataSubject = new Subject<any[]>()
37
38 private sub: Subscription
39
40 constructor (
41 private authService: AuthService,
42 private notifier: Notifier,
43 private confirmService: ConfirmService,
44 private videoCommentService: VideoCommentService,
45 private activatedRoute: ActivatedRoute,
46 private i18n: I18n,
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 }
137
138 onWantedToReply (comment: VideoComment) {
139 this.inReplyToCommentId = comment.id
140 }
141
142 onResetReply () {
143 this.inReplyToCommentId = undefined
144 }
145
146 onThreadCreated (commentTree: VideoCommentThreadTree) {
147 this.viewReplies(commentTree.comment.id)
148 }
149
150 handleSortChange (sort: string) {
151 if (this.sort === sort) return
152
153 this.sort = sort
154 this.resetVideo()
155 }
156
157 handleTimestampClicked (timestamp: number) {
158 this.timestampClicked.emit(timestamp)
159 }
160
161 async onWantedToDelete (commentToDelete: VideoComment) {
162 let message = 'Do you really want to delete this comment?'
163
164 if (commentToDelete.isLocal || this.video.isLocal) {
165 message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.')
166 } else {
167 message += this.i18n(' 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, this.i18n('Delete'))
171 if (res === false) return
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
190 isUserLoggedIn () {
191 return this.authService.isLoggedIn()
192 }
193
194 onNearOfBottom () {
195 if (hasMoreItems(this.componentPagination)) {
196 this.componentPagination.currentPage++
197 this.loadMoreThreads()
198 }
199 }
200
201 private softDeleteComment (comment: VideoComment) {
202 comment.isDeleted = true
203 comment.deletedAt = new Date()
204 comment.text = ''
205 comment.account = null
206 }
207
208 private resetVideo () {
209 if (this.video.commentsEnabled === true) {
210 // Reset all our fields
211 this.highlightedThread = null
212 this.comments = []
213 this.threadComments = {}
214 this.threadLoading = {}
215 this.inReplyToCommentId = undefined
216 this.componentPagination.currentPage = 1
217 this.componentPagination.totalItems = null
218
219 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
220 this.loadMoreThreads()
221 }
222 }
223
224 private processHighlightedThread (highlightedThreadId: number) {
225 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
226
227 const highlightThread = true
228 this.viewReplies(highlightedThreadId, highlightThread)
229 }
230 }