]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/comment/video-comments.component.ts
Update build steps for localization
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / 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'
67ed6552 8import { I18n } from '@ngx-translate/i18n-polyfill'
4635f59d
C
9
10@Component({
11 selector: 'my-video-comments',
12 templateUrl: './video-comments.component.html',
13 styleUrls: ['./video-comments.component.scss']
14})
1263fc4e 15export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
2f5d2ec5 16 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
47564bbe 17 @Input() video: VideoDetails
4635f59d
C
18 @Input() user: User
19
b29bf61d
RK
20 @Output() timestampClicked = new EventEmitter<number>()
21
4635f59d 22 comments: VideoComment[] = []
5b8072ee 23 highlightedThread: VideoComment
67ed6552 24 sort = '-createdAt'
4635f59d
C
25 componentPagination: ComponentPagination = {
26 currentPage: 1,
7416fbf3 27 itemsPerPage: 10,
4635f59d
C
28 totalItems: null
29 }
30 inReplyToCommentId: number
31 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
32 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e 33
c199c427 34 syndicationItems: Syndication[] = []
53877968 35
ad453580
C
36 onDataSubject = new Subject<any[]>()
37
1263fc4e 38 private sub: Subscription
4635f59d
C
39
40 constructor (
41 private authService: AuthService,
f8b2c1b4 42 private notifier: Notifier,
4cb6d457 43 private confirmService: ConfirmService,
d5b53822 44 private videoCommentService: VideoCommentService,
b1d40cff 45 private activatedRoute: ActivatedRoute,
93cae479
C
46 private i18n: I18n,
47 private hooks: HooksService
4635f59d
C
48 ) {}
49
1263fc4e
C
50 ngOnInit () {
51 // Find highlighted comment in params
52 this.sub = this.activatedRoute.params.subscribe(
53 params => {
5b8072ee
C
54 if (params['threadId']) {
55 const highlightedThreadId = +params['threadId']
56 this.processHighlightedThread(highlightedThreadId)
1263fc4e
C
57 }
58 }
59 )
60 }
61
339632b4
C
62 ngOnChanges (changes: SimpleChanges) {
63 if (changes['video']) {
1263fc4e 64 this.resetVideo()
47564bbe 65 }
4635f59d
C
66 }
67
1263fc4e
C
68 ngOnDestroy () {
69 if (this.sub) this.sub.unsubscribe()
70 }
71
5b8072ee 72 viewReplies (commentId: number, highlightThread = false) {
1263fc4e 73 this.threadLoading[commentId] = true
4635f59d 74
93cae479
C
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(
4635f59d 89 res => {
1263fc4e
C
90 this.threadComments[commentId] = res
91 this.threadLoading[commentId] = false
cf387740 92 this.hooks.runAction('action:video-watch.video-thread-replies.loaded', 'video-watch', { data: res })
1263fc4e 93
bf079b7b
C
94 if (highlightThread) {
95 this.highlightedThread = new VideoComment(res.comment)
96
97 // Scroll to the highlighted thread
43483d12 98 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
bf079b7b 99 }
4635f59d
C
100 },
101
f8b2c1b4 102 err => this.notifier.error(err.message)
4635f59d
C
103 )
104 }
105
93cae479
C
106 loadMoreThreads () {
107 const params = {
108 videoId: this.video.id,
109 componentPagination: this.componentPagination,
110 sort: this.sort
111 }
7416fbf3 112
93cae479
C
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 => {
e8f902c0
C
123 this.comments = this.comments.concat(res.data)
124 this.componentPagination.totalItems = res.total
ad453580
C
125
126 this.onDataSubject.next(res.data)
0840ca76 127 this.hooks.runAction('action:video-watch.video-threads.loaded', 'video-watch', { data: this.componentPagination })
93cae479
C
128 },
129
130 err => this.notifier.error(err.message)
131 )
7416fbf3
C
132 }
133
4635f59d
C
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
4cb6d457 146 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 147 this.viewReplies(commentTree.comment.id)
4cb6d457 148 }
b29bf61d 149
67ed6552 150 handleSortChange (sort: string) {
c1125bca
RK
151 if (this.sort === sort) return
152
153 this.sort = sort
154 this.resetVideo()
155 }
156
b29bf61d
RK
157 handleTimestampClicked (timestamp: number) {
158 this.timestampClicked.emit(timestamp)
159 }
4cb6d457 160
1f30a185 161 async onWantedToDelete (commentToDelete: VideoComment) {
4cb6d457 162 let message = 'Do you really want to delete this comment?'
7e73f071 163
e00aa353 164 if (commentToDelete.isLocal || this.video.isLocal) {
41a94d07 165 message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.')
7e73f071
C
166 } else {
167 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
168 }
169
b1d40cff 170 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
1f30a185
C
171 if (res === false) return
172
173 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
174 .subscribe(
175 () => {
e00aa353
C
176 if (this.highlightedThread?.id === commentToDelete.id) {
177 commentToDelete = this.comments.find(c => c.id === commentToDelete.id)
178
179 this.highlightedThread = undefined
180 }
181
69222afa
JM
182 // Mark the comment as deleted
183 this.softDeleteComment(commentToDelete)
1f30a185
C
184 },
185
f8b2c1b4 186 err => this.notifier.error(err.message)
1f30a185 187 )
4cb6d457
C
188 }
189
4635f59d
C
190 isUserLoggedIn () {
191 return this.authService.isLoggedIn()
192 }
7416fbf3 193
0503e975 194 onNearOfBottom () {
2f1548fd 195 if (hasMoreItems(this.componentPagination)) {
34102d19 196 this.componentPagination.currentPage++
93cae479 197 this.loadMoreThreads()
7416fbf3
C
198 }
199 }
200
69222afa
JM
201 private softDeleteComment (comment: VideoComment) {
202 comment.isDeleted = true
203 comment.deletedAt = new Date()
204 comment.text = ''
205 comment.account = null
4cb6d457 206 }
339632b4 207
1263fc4e 208 private resetVideo () {
339632b4
C
209 if (this.video.commentsEnabled === true) {
210 // Reset all our fields
5b8072ee 211 this.highlightedThread = null
339632b4
C
212 this.comments = []
213 this.threadComments = {}
214 this.threadLoading = {}
215 this.inReplyToCommentId = undefined
0cd4344f
C
216 this.componentPagination.currentPage = 1
217 this.componentPagination.totalItems = null
339632b4 218
53877968 219 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
93cae479 220 this.loadMoreThreads()
339632b4
C
221 }
222 }
1263fc4e 223
5b8072ee
C
224 private processHighlightedThread (highlightedThreadId: number) {
225 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 226
5b8072ee
C
227 const highlightThread = true
228 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 229 }
4635f59d 230}