]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core'
2import { ActivatedRoute } from '@angular/router'
3import { ConfirmService, Notifier } from '@app/core'
4import { Subject, Subscription } from 'rxjs'
5import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
6import { AuthService } from '../../../core/auth'
7import { ComponentPagination, hasMoreItems } from '../../../shared/rest/component-pagination.model'
8import { User } from '../../../shared/users'
9import { VideoSortField } from '../../../shared/video/sort-field.type'
10import { VideoDetails } from '../../../shared/video/video-details.model'
11import { VideoComment } from './video-comment.model'
12import { VideoCommentService } from './video-comment.service'
13import { I18n } from '@ngx-translate/i18n-polyfill'
14import { Syndication } from '@app/shared/video/syndication.model'
15import { HooksService } from '@app/core/plugins/hooks.service'
16
17@Component({
18 selector: 'my-video-comments',
19 templateUrl: './video-comments.component.html',
20 styleUrls: ['./video-comments.component.scss']
21})
22export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
23 @ViewChild('commentHighlightBlock', { static: false }) commentHighlightBlock: ElementRef
24 @Input() video: VideoDetails
25 @Input() user: User
26
27 comments: VideoComment[] = []
28 highlightedThread: VideoComment
29 sort: VideoSortField = '-createdAt'
30 componentPagination: ComponentPagination = {
31 currentPage: 1,
32 itemsPerPage: 10,
33 totalItems: null
34 }
35 inReplyToCommentId: number
36 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
37 threadLoading: { [ id: number ]: boolean } = {}
38
39 syndicationItems: Syndication[] = []
40
41 onDataSubject = new Subject<any[]>()
42
43 private sub: Subscription
44
45 constructor (
46 private authService: AuthService,
47 private notifier: Notifier,
48 private confirmService: ConfirmService,
49 private videoCommentService: VideoCommentService,
50 private activatedRoute: ActivatedRoute,
51 private i18n: I18n,
52 private hooks: HooksService
53 ) {}
54
55 ngOnInit () {
56 // Find highlighted comment in params
57 this.sub = this.activatedRoute.params.subscribe(
58 params => {
59 if (params['threadId']) {
60 const highlightedThreadId = +params['threadId']
61 this.processHighlightedThread(highlightedThreadId)
62 }
63 }
64 )
65 }
66
67 ngOnChanges (changes: SimpleChanges) {
68 if (changes['video']) {
69 this.resetVideo()
70 }
71 }
72
73 ngOnDestroy () {
74 if (this.sub) this.sub.unsubscribe()
75 }
76
77 viewReplies (commentId: number, highlightThread = false) {
78 this.threadLoading[commentId] = true
79
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(
94 res => {
95 this.threadComments[commentId] = res
96 this.threadLoading[commentId] = false
97
98 if (highlightThread) {
99 this.highlightedThread = new VideoComment(res.comment)
100
101 // Scroll to the highlighted thread
102 setTimeout(() => this.commentHighlightBlock.nativeElement.scrollIntoView(), 0)
103 }
104 },
105
106 err => this.notifier.error(err.message)
107 )
108 }
109
110 loadMoreThreads () {
111 const params = {
112 videoId: this.video.id,
113 componentPagination: this.componentPagination,
114 sort: this.sort
115 }
116
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 => {
127 this.comments = this.comments.concat(res.data)
128 this.componentPagination.totalItems = res.total
129
130 this.onDataSubject.next(res.data)
131 },
132
133 err => this.notifier.error(err.message)
134 )
135 }
136
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
149 onThreadCreated (commentTree: VideoCommentThreadTree) {
150 this.viewReplies(commentTree.comment.id)
151 }
152
153 async onWantedToDelete (commentToDelete: VideoComment) {
154 let message = 'Do you really want to delete this comment?'
155
156 if (commentToDelete.isLocal) {
157 message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.')
158 } else {
159 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
160 }
161
162 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
163 if (res === false) return
164
165 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
166 .subscribe(
167 () => {
168 // Mark the comment as deleted
169 this.softDeleteComment(commentToDelete)
170
171 if (this.highlightedThread.id === commentToDelete.id) this.highlightedThread = undefined
172 },
173
174 err => this.notifier.error(err.message)
175 )
176 }
177
178 isUserLoggedIn () {
179 return this.authService.isLoggedIn()
180 }
181
182 onNearOfBottom () {
183 this.componentPagination.currentPage++
184
185 if (hasMoreItems(this.componentPagination)) {
186 this.loadMoreThreads()
187 }
188 }
189
190 private softDeleteComment (comment: VideoComment) {
191 comment.isDeleted = true
192 comment.deletedAt = new Date()
193 comment.text = ''
194 comment.account = null
195 }
196
197 private resetVideo () {
198 if (this.video.commentsEnabled === true) {
199 // Reset all our fields
200 this.highlightedThread = null
201 this.comments = []
202 this.threadComments = {}
203 this.threadLoading = {}
204 this.inReplyToCommentId = undefined
205 this.componentPagination.currentPage = 1
206 this.componentPagination.totalItems = null
207
208 this.syndicationItems = this.videoCommentService.getVideoCommentsFeeds(this.video.uuid)
209
210 this.loadMoreThreads()
211 }
212 }
213
214 private processHighlightedThread (highlightedThreadId: number) {
215 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
216
217 const highlightThread = true
218 this.viewReplies(highlightedThreadId, highlightThread)
219 }
220}