]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/shared/comment/video-comments.component.ts
Reorganize watch components
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / comment / video-comments.component.ts
CommitLineData
67ed6552 1import { Subject, Subscription } from 'rxjs'
be27ef3b 2import { Component, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
1263fc4e 3import { ActivatedRoute } from '@angular/router'
67ed6552
C
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'
cfde28ba 7import { VideoComment, VideoCommentService, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
4635f59d
C
8
9@Component({
10 selector: 'my-video-comments',
11 templateUrl: './video-comments.component.html',
12 styleUrls: ['./video-comments.component.scss']
13})
1263fc4e 14export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
2f5d2ec5 15 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
47564bbe 16 @Input() video: VideoDetails
4635f59d
C
17 @Input() user: User
18
b29bf61d
RK
19 @Output() timestampClicked = new EventEmitter<number>()
20
4635f59d 21 comments: VideoComment[] = []
5b8072ee 22 highlightedThread: VideoComment
9d6b9d10 23
67ed6552 24 sort = '-createdAt'
9d6b9d10 25
4635f59d
C
26 componentPagination: ComponentPagination = {
27 currentPage: 1,
7416fbf3 28 itemsPerPage: 10,
4635f59d
C
29 totalItems: null
30 }
9d6b9d10
C
31 totalNotDeletedComments: number
32
4635f59d 33 inReplyToCommentId: number
f63c03fb 34 commentReplyRedraftValue: string
35 commentThreadRedraftValue: string
9d6b9d10 36
4635f59d
C
37 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
38 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e 39
c199c427 40 syndicationItems: Syndication[] = []
53877968 41
ad453580
C
42 onDataSubject = new Subject<any[]>()
43
1263fc4e 44 private sub: Subscription
4635f59d
C
45
46 constructor (
47 private authService: AuthService,
f8b2c1b4 48 private notifier: Notifier,
4cb6d457 49 private confirmService: ConfirmService,
d5b53822 50 private videoCommentService: VideoCommentService,
b1d40cff 51 private activatedRoute: ActivatedRoute,
93cae479 52 private hooks: HooksService
4635f59d
C
53 ) {}
54
1263fc4e
C
55 ngOnInit () {
56 // Find highlighted comment in params
57 this.sub = this.activatedRoute.params.subscribe(
58 params => {
5b8072ee
C
59 if (params['threadId']) {
60 const highlightedThreadId = +params['threadId']
61 this.processHighlightedThread(highlightedThreadId)
1263fc4e
C
62 }
63 }
64 )
65 }
66
339632b4
C
67 ngOnChanges (changes: SimpleChanges) {
68 if (changes['video']) {
1263fc4e 69 this.resetVideo()
47564bbe 70 }
4635f59d
C
71 }
72
1263fc4e
C
73 ngOnDestroy () {
74 if (this.sub) this.sub.unsubscribe()
75 }
76
5b8072ee 77 viewReplies (commentId: number, highlightThread = false) {
1263fc4e 78 this.threadLoading[commentId] = true
4635f59d 79
93cae479
C
80 const params = {
81 videoId: this.video.id,
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(
4635f59d 94 res => {
1263fc4e
C
95 this.threadComments[commentId] = res
96 this.threadLoading[commentId] = false
cf387740 97 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
1263fc4e 98
bf079b7b
C
99 if (highlightThread) {
100 this.highlightedThread = new VideoComment(res.comment)
101
102 // Scroll to the highlighted thread
43483d12 103 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
bf079b7b 104 }
4635f59d
C
105 },
106
f8b2c1b4 107 err => this.notifier.error(err.message)
4635f59d
C
108 )
109 }
110
93cae479
C
111 loadMoreThreads () {
112 const params = {
113 videoId: this.video.id,
114 componentPagination: this.componentPagination,
115 sort: this.sort
116 }
7416fbf3 117
93cae479
C
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 res => {
e8f902c0 128 this.comments = this.comments.concat(res.data)
9d6b9d10
C
129 this.componentPagination.totalItems = res.total
130 this.totalNotDeletedComments = res.totalNotDeletedComments
ad453580
C
131
132 this.onDataSubject.next(res.data)
0840ca76 133 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
93cae479
C
134 },
135
136 err => this.notifier.error(err.message)
137 )
7416fbf3
C
138 }
139
4635f59d
C
140 onCommentThreadCreated (comment: VideoComment) {
141 this.comments.unshift(comment)
fdd12965 142 this.commentThreadRedraftValue = undefined
4635f59d
C
143 }
144
145 onWantedToReply (comment: VideoComment) {
146 this.inReplyToCommentId = comment.id
147 }
148
149 onResetReply () {
150 this.inReplyToCommentId = undefined
fdd12965 151 this.commentReplyRedraftValue = undefined
4635f59d
C
152 }
153
4cb6d457 154 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 155 this.viewReplies(commentTree.comment.id)
4cb6d457 156 }
b29bf61d 157
67ed6552 158 handleSortChange (sort: string) {
c1125bca
RK
159 if (this.sort === sort) return
160
161 this.sort = sort
162 this.resetVideo()
163 }
164
b29bf61d
RK
165 handleTimestampClicked (timestamp: number) {
166 this.timestampClicked.emit(timestamp)
167 }
4cb6d457 168
93e903ac
C
169 async onWantedToDelete (
170 commentToDelete: VideoComment,
171 title = $localize`Delete`,
172 message = $localize`Do you really want to delete this comment?`
173 ): Promise<boolean> {
e00aa353 174 if (commentToDelete.isLocal || this.video.isLocal) {
66357162 175 message += $localize` The deletion will be sent to remote instances so they can reflect the change.`
7e73f071 176 } else {
66357162 177 message += $localize` It is a remote comment, so the deletion will only be effective on your instance.`
7e73f071
C
178 }
179
ddb0303f 180 const res = await this.confirmService.confirm(message, title)
181 if (res === false) return false
1f30a185
C
182
183 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
184 .subscribe(
185 () => {
e00aa353
C
186 if (this.highlightedThread?.id === commentToDelete.id) {
187 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
188
189 this.highlightedThread = undefined
190 }
191
69222afa
JM
192 // Mark the comment as deleted
193 this.softDeleteComment(commentToDelete)
1f30a185
C
194 },
195
f8b2c1b4 196 err => this.notifier.error(err.message)
1f30a185 197 )
f63c03fb 198
199 return true
200 }
201
ddb0303f 202 async onWantedToRedraft (commentToRedraft: VideoComment) {
203 const confirm = await this.onWantedToDelete(commentToRedraft, $localize`Delete and re-draft`, $localize`Do you really want to delete and re-draft this comment?`)
f63c03fb 204
205 if (confirm) {
206 this.inReplyToCommentId = commentToRedraft.inReplyToCommentId
207
110d463f 208 // Restore line feed for editing
6a9498e3
K
209 const commentToRedraftText = commentToRedraft.text.replace(/<br.?\/?>/g, '\r\n')
210
f63c03fb 211 if (commentToRedraft.threadId === commentToRedraft.id) {
6a9498e3 212 this.commentThreadRedraftValue = commentToRedraftText
f63c03fb 213 } else {
6a9498e3 214 this.commentReplyRedraftValue = commentToRedraftText
f63c03fb 215 }
216
217 }
4cb6d457
C
218 }
219
4635f59d
C
220 isUserLoggedIn () {
221 return this.authService.isLoggedIn()
222 }
7416fbf3 223
0503e975 224 onNearOfBottom () {
2f1548fd 225 if (hasMoreItems(this.componentPagination)) {
34102d19 226 this.componentPagination.currentPage++
93cae479 227 this.loadMoreThreads()
7416fbf3
C
228 }
229 }
230
69222afa
JM
231 private softDeleteComment (comment: VideoComment) {
232 comment.isDeleted = true
233 comment.deletedAt = new Date()
234 comment.text = ''
235 comment.account = null
4cb6d457 236 }
339632b4 237
1263fc4e 238 private resetVideo () {
339632b4
C
239 if (this.video.commentsEnabled === true) {
240 // Reset all our fields
5b8072ee 241 this.highlightedThread = null
339632b4
C
242 this.comments = []
243 this.threadComments = {}
244 this.threadLoading = {}
245 this.inReplyToCommentId = undefined
0cd4344f
C
246 this.componentPagination.currentPage = 1
247 this.componentPagination.totalItems = null
9d6b9d10 248 this.totalNotDeletedComments = null
339632b4 249
d4a8e7a6 250 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video)
93cae479 251 this.loadMoreThreads()
339632b4
C
252 }
253 }
1263fc4e 254
5b8072ee
C
255 private processHighlightedThread (highlightedThreadId: number) {
256 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 257
5b8072ee
C
258 const highlightThread = true
259 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 260 }
4635f59d 261}