]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
bf079b7b 1import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild, ElementRef } from '@angular/core'
1263fc4e 2import { ActivatedRoute } from '@angular/router'
4cb6d457 3import { ConfirmService } from '@app/core'
4635f59d 4import { NotificationsService } from 'angular2-notifications'
db400f44 5import { Subscription } from 'rxjs'
1263fc4e 6import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4635f59d
C
7import { AuthService } from '../../../core/auth'
8import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
9import { User } from '../../../shared/users'
7b87d2d5 10import { VideoSortField } from '../../../shared/video/sort-field.type'
47564bbe 11import { VideoDetails } from '../../../shared/video/video-details.model'
4635f59d
C
12import { VideoComment } from './video-comment.model'
13import { VideoCommentService } from './video-comment.service'
b1d40cff 14import { I18n } from '@ngx-translate/i18n-polyfill'
4635f59d
C
15
16@Component({
17 selector: 'my-video-comments',
18 templateUrl: './video-comments.component.html',
19 styleUrls: ['./video-comments.component.scss']
20})
1263fc4e 21export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
bf079b7b 22 @ViewChild('commentHighlightBlock') commentHighlightBlock: ElementRef
47564bbe 23 @Input() video: VideoDetails
4635f59d
C
24 @Input() user: User
25
26 comments: VideoComment[] = []
5b8072ee 27 highlightedThread: VideoComment
7b87d2d5 28 sort: VideoSortField = '-createdAt'
4635f59d
C
29 componentPagination: ComponentPagination = {
30 currentPage: 1,
7416fbf3 31 itemsPerPage: 10,
4635f59d
C
32 totalItems: null
33 }
34 inReplyToCommentId: number
35 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
36 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e
C
37
38 private sub: Subscription
4635f59d
C
39
40 constructor (
41 private authService: AuthService,
42 private notificationsService: NotificationsService,
4cb6d457 43 private confirmService: ConfirmService,
d5b53822 44 private videoCommentService: VideoCommentService,
b1d40cff
C
45 private activatedRoute: ActivatedRoute,
46 private i18n: I18n
4635f59d
C
47 ) {}
48
1263fc4e
C
49 ngOnInit () {
50 // Find highlighted comment in params
51 this.sub = this.activatedRoute.params.subscribe(
52 params => {
5b8072ee
C
53 if (params['threadId']) {
54 const highlightedThreadId = +params['threadId']
55 this.processHighlightedThread(highlightedThreadId)
1263fc4e
C
56 }
57 }
58 )
59 }
60
339632b4
C
61 ngOnChanges (changes: SimpleChanges) {
62 if (changes['video']) {
1263fc4e 63 this.resetVideo()
47564bbe 64 }
4635f59d
C
65 }
66
1263fc4e
C
67 ngOnDestroy () {
68 if (this.sub) this.sub.unsubscribe()
69 }
70
5b8072ee 71 viewReplies (commentId: number, highlightThread = false) {
1263fc4e 72 this.threadLoading[commentId] = true
4635f59d 73
1263fc4e 74 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
4635f59d
C
75 .subscribe(
76 res => {
1263fc4e
C
77 this.threadComments[commentId] = res
78 this.threadLoading[commentId] = false
79
bf079b7b
C
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 }
4635f59d
C
91 },
92
b1d40cff 93 err => this.notificationsService.error(this.i18n('Error'), err.message)
4635f59d
C
94 )
95 }
96
7416fbf3
C
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
b1d40cff 105 err => this.notificationsService.error(this.i18n('Error'), err.message)
7416fbf3
C
106 )
107 }
108
4635f59d
C
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
4cb6d457 121 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 122 this.viewReplies(commentTree.comment.id)
4cb6d457
C
123 }
124
1f30a185 125 async onWantedToDelete (commentToDelete: VideoComment) {
4cb6d457 126 let message = 'Do you really want to delete this comment?'
b1d40cff 127 if (commentToDelete.totalReplies !== 0) {
25acef90 128 message += this.i18n(' {{totalReplies}} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
b1d40cff 129 }
4cb6d457 130
b1d40cff 131 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
1f30a185
C
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
b1d40cff 154 err => this.notificationsService.error(this.i18n('Error'), err.message)
1f30a185 155 )
4cb6d457
C
156 }
157
4635f59d
C
158 isUserLoggedIn () {
159 return this.authService.isLoggedIn()
160 }
7416fbf3
C
161
162 onNearOfBottom () {
163 this.componentPagination.currentPage++
164
165 if (this.hasMoreComments()) {
166 this.loadMoreComments()
167 }
168 }
169
4cb6d457 170 private hasMoreComments () {
7416fbf3
C
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 }
4cb6d457
C
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 }
339632b4 191
1263fc4e 192 private resetVideo () {
339632b4
C
193 if (this.video.commentsEnabled === true) {
194 // Reset all our fields
5b8072ee 195 this.highlightedThread = null
339632b4
C
196 this.comments = []
197 this.threadComments = {}
198 this.threadLoading = {}
199 this.inReplyToCommentId = undefined
0cd4344f
C
200 this.componentPagination.currentPage = 1
201 this.componentPagination.totalItems = null
339632b4
C
202
203 this.loadMoreComments()
204 }
205 }
1263fc4e 206
5b8072ee
C
207 private processHighlightedThread (highlightedThreadId: number) {
208 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 209
5b8072ee
C
210 const highlightThread = true
211 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 212 }
4635f59d 213}