]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comments.component.ts
Add hyperlink video timestamps in description
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
CommitLineData
f8b2c1b4 1import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core'
1263fc4e 2import { ActivatedRoute } from '@angular/router'
f8b2c1b4 3import { ConfirmService, Notifier } from '@app/core'
ad453580 4import { Subject, Subscription } from 'rxjs'
1263fc4e 5import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4635f59d 6import { AuthService } from '../../../core/auth'
2f1548fd 7import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model'
4635f59d 8import { User } from '../../../shared/users'
7b87d2d5 9import { VideoSortField } from '../../../shared/video/sort-field.type'
47564bbe 10import { VideoDetails } from '../../../shared/video/video-details.model'
4635f59d
C
11import { VideoComment } from './video-comment.model'
12import { VideoCommentService } from './video-comment.service'
b1d40cff 13import { I18n } from '@ngx-translate/i18n-polyfill'
c199c427 14import { Syndication } from '@app/shared/video/syndication.model'
93cae479 15import { HooksService } from '@app/core/plugins/hooks.service'
4635f59d
C
16
17@Component({
18 selector: 'my-video-comments',
19 templateUrl: './video-comments.component.html',
20 styleUrls: ['./video-comments.component.scss']
21})
1263fc4e 22export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
f36da21e 23 @ViewChild('commentHighlightBlock', { static: false }) commentHighlightBlock: ElementRef
47564bbe 24 @Input() video: VideoDetails
4635f59d
C
25 @Input() user: User
26
27 comments: VideoComment[] = []
5b8072ee 28 highlightedThread: VideoComment
7b87d2d5 29 sort: VideoSortField = '-createdAt'
4635f59d
C
30 componentPagination: ComponentPagination = {
31 currentPage: 1,
7416fbf3 32 itemsPerPage: 10,
4635f59d
C
33 totalItems: null
34 }
35 inReplyToCommentId: number
36 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
37 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e 38
c199c427 39 syndicationItems: Syndication[] = []
53877968 40
ad453580
C
41 onDataSubject = new Subject<any[]>()
42
1263fc4e 43 private sub: Subscription
4635f59d
C
44
45 constructor (
46 private authService: AuthService,
f8b2c1b4 47 private notifier: Notifier,
4cb6d457 48 private confirmService: ConfirmService,
d5b53822 49 private videoCommentService: VideoCommentService,
b1d40cff 50 private activatedRoute: ActivatedRoute,
93cae479
C
51 private i18n: I18n,
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
97
bf079b7b
C
98 if (highlightThread) {
99 this.highlightedThread = new VideoComment(res.comment)
100
101 // Scroll to the highlighted thread
43483d12 102 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
bf079b7b 103 }
4635f59d
C
104 },
105
f8b2c1b4 106 err => this.notifier.error(err.message)
4635f59d
C
107 )
108 }
109
93cae479
C
110 loadMoreThreads () {
111 const params = {
112 videoId: this.video.id,
113 componentPagination: this.componentPagination,
114 sort: this.sort
115 }
7416fbf3 116
93cae479
C
117 const obs = this.hooks.wrapObsFun(
118 this.videoCommentService.getVideoCommentThreads.bind(this.videoCommentService),
119 params,
120 'video-watch',
121 'filter:api.video-watch.video-threads.list.params',
122 'filter:api.video-watch.video-threads.list.result'
123 )
124
125 obs.subscribe(
126 res => {
e8f902c0
C
127 this.comments = this.comments.concat(res.data)
128 this.componentPagination.totalItems = res.total
ad453580
C
129
130 this.onDataSubject.next(res.data)
93cae479
C
131 },
132
133 err => this.notifier.error(err.message)
134 )
7416fbf3
C
135 }
136
4635f59d
C
137 onCommentThreadCreated (comment: VideoComment) {
138 this.comments.unshift(comment)
139 }
140
141 onWantedToReply (comment: VideoComment) {
142 this.inReplyToCommentId = comment.id
143 }
144
145 onResetReply () {
146 this.inReplyToCommentId = undefined
147 }
148
4cb6d457 149 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 150 this.viewReplies(commentTree.comment.id)
4cb6d457
C
151 }
152
1f30a185 153 async onWantedToDelete (commentToDelete: VideoComment) {
4cb6d457 154 let message = 'Do you really want to delete this comment?'
7e73f071 155
7e73f071 156 if (commentToDelete.isLocal) {
32d7f2b7 157 message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.')
7e73f071
C
158 } else {
159 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
160 }
161
b1d40cff 162 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
1f30a185
C
163 if (res === false) return
164
165 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
166 .subscribe(
167 () => {
69222afa
JM
168 // Mark the comment as deleted
169 this.softDeleteComment(commentToDelete)
73dc4da0
C
170
171 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
1f30a185
C
172 },
173
f8b2c1b4 174 err => this.notifier.error(err.message)
1f30a185 175 )
4cb6d457
C
176 }
177
4635f59d
C
178 isUserLoggedIn () {
179 return this.authService.isLoggedIn()
180 }
7416fbf3
C
181
182 onNearOfBottom () {
183 this.componentPagination.currentPage++
184
2f1548fd 185 if (hasMoreItems(this.componentPagination)) {
93cae479 186 this.loadMoreThreads()
7416fbf3
C
187 }
188 }
189
69222afa
JM
190 private softDeleteComment (comment: VideoComment) {
191 comment.isDeleted = true
192 comment.deletedAt = new Date()
193 comment.text = ''
194 comment.account = null
4cb6d457 195 }
339632b4 196
1263fc4e 197 private resetVideo () {
339632b4
C
198 if (this.video.commentsEnabled === true) {
199 // Reset all our fields
5b8072ee 200 this.highlightedThread = null
339632b4
C
201 this.comments = []
202 this.threadComments = {}
203 this.threadLoading = {}
204 this.inReplyToCommentId = undefined
0cd4344f
C
205 this.componentPagination.currentPage = 1
206 this.componentPagination.totalItems = null
339632b4 207
53877968
C
208 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
209
93cae479 210 this.loadMoreThreads()
339632b4
C
211 }
212 }
1263fc4e 213
5b8072ee
C
214 private processHighlightedThread (highlightedThreadId: number) {
215 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 216
5b8072ee
C
217 const highlightThread = true
218 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 219 }
4635f59d 220}