]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/videos/+video-watch/comment/video-comments.component.ts
Automatically jump to the highlighted thread
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
... / ...
CommitLineData
1import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild, ElementRef } from '@angular/core'
2import { ActivatedRoute } from '@angular/router'
3import { ConfirmService } from '@app/core'
4import { NotificationsService } from 'angular2-notifications'
5import { Subscription } from 'rxjs'
6import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
7import { AuthService } from '../../../core/auth'
8import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
9import { User } from '../../../shared/users'
10import { VideoSortField } from '../../../shared/video/sort-field.type'
11import { VideoDetails } from '../../../shared/video/video-details.model'
12import { VideoComment } from './video-comment.model'
13import { VideoCommentService } from './video-comment.service'
14import { I18n } from '@ngx-translate/i18n-polyfill'
15
16@Component({
17 selector: 'my-video-comments',
18 templateUrl: './video-comments.component.html',
19 styleUrls: ['./video-comments.component.scss']
20})
21export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
22 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
23 @Input() video: VideoDetails
24 @Input() user: User
25
26 comments: VideoComment[] = []
27 highlightedThread: VideoComment
28 sort: VideoSortField = '-createdAt'
29 componentPagination: ComponentPagination = {
30 currentPage: 1,
31 itemsPerPage: 10,
32 totalItems: null
33 }
34 inReplyToCommentId: number
35 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
36 threadLoading: { [ id: number ]: boolean } = {}
37
38 private sub: Subscription
39
40 constructor (
41 private authService: AuthService,
42 private notificationsService: NotificationsService,
43 private confirmService: ConfirmService,
44 private videoCommentService: VideoCommentService,
45 private activatedRoute: ActivatedRoute,
46 private i18n: I18n
47 ) {}
48
49 ngOnInit () {
50 // Find highlighted comment in params
51 this.sub = this.activatedRoute.params.subscribe(
52 params => {
53 if (params['threadId']) {
54 const highlightedThreadId = +params['threadId']
55 this.processHighlightedThread(highlightedThreadId)
56 }
57 }
58 )
59 }
60
61 ngOnChanges (changes: SimpleChanges) {
62 if (changes['video']) {
63 this.resetVideo()
64 }
65 }
66
67 ngOnDestroy () {
68 if (this.sub) this.sub.unsubscribe()
69 }
70
71 viewReplies (commentId: number, highlightThread = false) {
72 this.threadLoading[commentId] = true
73
74 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
75 .subscribe(
76 res => {
77 this.threadComments[commentId] = res
78 this.threadLoading[commentId] = false
79
80 if (highlightThread) {
81 this.highlightedThread = new VideoComment(res.comment)
82
83 // Scroll to the highlighted thread
84 setTimeout(() => {
85 // -60 because of the fixed header
86 console.log(this.commentHighlightBlock.nativeElement.offsetTop)
87 const scrollY = this.commentHighlightBlock.nativeElement.offsetTop - 60
88 window.scroll(0, scrollY)
89 }, 500)
90 }
91 },
92
93 err => this.notificationsService.error(this.i18n('Error'), err.message)
94 )
95 }
96
97 loadMoreComments () {
98 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
99 .subscribe(
100 res => {
101 this.comments = this.comments.concat(res.comments)
102 this.componentPagination.totalItems = res.totalComments
103 },
104
105 err => this.notificationsService.error(this.i18n('Error'), err.message)
106 )
107 }
108
109 onCommentThreadCreated (comment: VideoComment) {
110 this.comments.unshift(comment)
111 }
112
113 onWantedToReply (comment: VideoComment) {
114 this.inReplyToCommentId = comment.id
115 }
116
117 onResetReply () {
118 this.inReplyToCommentId = undefined
119 }
120
121 onThreadCreated (commentTree: VideoCommentThreadTree) {
122 this.viewReplies(commentTree.comment.id)
123 }
124
125 async onWantedToDelete (commentToDelete: VideoComment) {
126 let message = 'Do you really want to delete this comment?'
127 if (commentToDelete.totalReplies !== 0) {
128 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
129 }
130
131 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
132 if (res === false) return
133
134 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
135 .subscribe(
136 () => {
137 // Delete the comment in the tree
138 if (commentToDelete.inReplyToCommentId) {
139 const thread = this.threadComments[commentToDelete.threadId]
140 if (!thread) {
141 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
142 return
143 }
144
145 this.deleteLocalCommentThread(thread, commentToDelete)
146 return
147 }
148
149 // Delete the thread
150 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
151 this.componentPagination.totalItems--
152 },
153
154 err => this.notificationsService.error(this.i18n('Error'), err.message)
155 )
156 }
157
158 isUserLoggedIn () {
159 return this.authService.isLoggedIn()
160 }
161
162 onNearOfBottom () {
163 this.componentPagination.currentPage++
164
165 if (this.hasMoreComments()) {
166 this.loadMoreComments()
167 }
168 }
169
170 private hasMoreComments () {
171 // No results
172 if (this.componentPagination.totalItems === 0) return false
173
174 // Not loaded yet
175 if (!this.componentPagination.totalItems) return true
176
177 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
178 return maxPage > this.componentPagination.currentPage
179 }
180
181 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
182 for (const commentChild of parentComment.children) {
183 if (commentChild.comment.id === commentToDelete.id) {
184 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
185 return
186 }
187
188 this.deleteLocalCommentThread(commentChild, commentToDelete)
189 }
190 }
191
192 private resetVideo () {
193 if (this.video.commentsEnabled === true) {
194 // Reset all our fields
195 this.highlightedThread = null
196 this.comments = []
197 this.threadComments = {}
198 this.threadLoading = {}
199 this.inReplyToCommentId = undefined
200 this.componentPagination.currentPage = 1
201 this.componentPagination.totalItems = null
202
203 this.loadMoreComments()
204 }
205 }
206
207 private processHighlightedThread (highlightedThreadId: number) {
208 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
209
210 const highlightThread = true
211 this.viewReplies(highlightedThreadId, highlightThread)
212 }
213}