]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comments.component.ts
add the comment from https://github.com/Chocobozzz/PeerTube/pull/617/files#diff-5003d...
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
CommitLineData
1263fc4e
C
1import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'
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 {
47564bbe 22 @Input() video: VideoDetails
4635f59d
C
23 @Input() user: User
24
25 comments: VideoComment[] = []
5b8072ee 26 highlightedThread: VideoComment
7b87d2d5 27 sort: VideoSortField = '-createdAt'
4635f59d
C
28 componentPagination: ComponentPagination = {
29 currentPage: 1,
7416fbf3 30 itemsPerPage: 10,
4635f59d
C
31 totalItems: null
32 }
33 inReplyToCommentId: number
34 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
35 threadLoading: { [ id: number ]: boolean } = {}
1263fc4e
C
36
37 private sub: Subscription
4635f59d
C
38
39 constructor (
40 private authService: AuthService,
41 private notificationsService: NotificationsService,
4cb6d457 42 private confirmService: ConfirmService,
d5b53822 43 private videoCommentService: VideoCommentService,
b1d40cff
C
44 private activatedRoute: ActivatedRoute,
45 private i18n: I18n
4635f59d
C
46 ) {}
47
1263fc4e
C
48 ngOnInit () {
49 // Find highlighted comment in params
50 this.sub = this.activatedRoute.params.subscribe(
51 params => {
5b8072ee
C
52 if (params['threadId']) {
53 const highlightedThreadId = +params['threadId']
54 this.processHighlightedThread(highlightedThreadId)
1263fc4e
C
55 }
56 }
57 )
58 }
59
339632b4
C
60 ngOnChanges (changes: SimpleChanges) {
61 if (changes['video']) {
1263fc4e 62 this.resetVideo()
47564bbe 63 }
4635f59d
C
64 }
65
1263fc4e
C
66 ngOnDestroy () {
67 if (this.sub) this.sub.unsubscribe()
68 }
69
5b8072ee 70 viewReplies (commentId: number, highlightThread = false) {
1263fc4e 71 this.threadLoading[commentId] = true
4635f59d 72
1263fc4e 73 this.videoCommentService.getVideoThreadComments(this.video.id, commentId)
4635f59d
C
74 .subscribe(
75 res => {
1263fc4e
C
76 this.threadComments[commentId] = res
77 this.threadLoading[commentId] = false
78
5b8072ee 79 if (highlightThread) this.highlightedThread = new VideoComment(res.comment)
4635f59d
C
80 },
81
b1d40cff 82 err => this.notificationsService.error(this.i18n('Error'), err.message)
4635f59d
C
83 )
84 }
85
7416fbf3
C
86 loadMoreComments () {
87 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
88 .subscribe(
89 res => {
90 this.comments = this.comments.concat(res.comments)
91 this.componentPagination.totalItems = res.totalComments
92 },
93
b1d40cff 94 err => this.notificationsService.error(this.i18n('Error'), err.message)
7416fbf3
C
95 )
96 }
97
4635f59d
C
98 onCommentThreadCreated (comment: VideoComment) {
99 this.comments.unshift(comment)
100 }
101
102 onWantedToReply (comment: VideoComment) {
103 this.inReplyToCommentId = comment.id
104 }
105
106 onResetReply () {
107 this.inReplyToCommentId = undefined
108 }
109
4cb6d457 110 onThreadCreated (commentTree: VideoCommentThreadTree) {
1263fc4e 111 this.viewReplies(commentTree.comment.id)
4cb6d457
C
112 }
113
1f30a185 114 async onWantedToDelete (commentToDelete: VideoComment) {
4cb6d457 115 let message = 'Do you really want to delete this comment?'
b1d40cff
C
116 if (commentToDelete.totalReplies !== 0) {
117 message += this.i18n(' {{ totalReplies }} replies will be deleted too.', { totalReplies: commentToDelete.totalReplies })
118 }
4cb6d457 119
b1d40cff 120 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
1f30a185
C
121 if (res === false) return
122
123 this.videoCommentService.deleteVideoComment(commentToDelete.videoId, commentToDelete.id)
124 .subscribe(
125 () => {
126 // Delete the comment in the tree
127 if (commentToDelete.inReplyToCommentId) {
128 const thread = this.threadComments[commentToDelete.threadId]
129 if (!thread) {
130 console.error(`Cannot find thread ${commentToDelete.threadId} of the comment to delete ${commentToDelete.id}`)
131 return
132 }
133
134 this.deleteLocalCommentThread(thread, commentToDelete)
135 return
136 }
137
138 // Delete the thread
139 this.comments = this.comments.filter(c => c.id !== commentToDelete.id)
140 this.componentPagination.totalItems--
141 },
142
b1d40cff 143 err => this.notificationsService.error(this.i18n('Error'), err.message)
1f30a185 144 )
4cb6d457
C
145 }
146
4635f59d
C
147 isUserLoggedIn () {
148 return this.authService.isLoggedIn()
149 }
7416fbf3
C
150
151 onNearOfBottom () {
152 this.componentPagination.currentPage++
153
154 if (this.hasMoreComments()) {
155 this.loadMoreComments()
156 }
157 }
158
4cb6d457 159 private hasMoreComments () {
7416fbf3
C
160 // No results
161 if (this.componentPagination.totalItems === 0) return false
162
163 // Not loaded yet
164 if (!this.componentPagination.totalItems) return true
165
166 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
167 return maxPage > this.componentPagination.currentPage
168 }
4cb6d457
C
169
170 private deleteLocalCommentThread (parentComment: VideoCommentThreadTree, commentToDelete: VideoComment) {
171 for (const commentChild of parentComment.children) {
172 if (commentChild.comment.id === commentToDelete.id) {
173 parentComment.children = parentComment.children.filter(c => c.comment.id !== commentToDelete.id)
174 return
175 }
176
177 this.deleteLocalCommentThread(commentChild, commentToDelete)
178 }
179 }
339632b4 180
1263fc4e 181 private resetVideo () {
339632b4
C
182 if (this.video.commentsEnabled === true) {
183 // Reset all our fields
5b8072ee 184 this.highlightedThread = null
339632b4
C
185 this.comments = []
186 this.threadComments = {}
187 this.threadLoading = {}
188 this.inReplyToCommentId = undefined
0cd4344f
C
189 this.componentPagination.currentPage = 1
190 this.componentPagination.totalItems = null
339632b4
C
191
192 this.loadMoreComments()
193 }
194 }
1263fc4e 195
5b8072ee
C
196 private processHighlightedThread (highlightedThreadId: number) {
197 this.highlightedThread = this.comments.find(c => c.id === highlightedThreadId)
1263fc4e 198
5b8072ee
C
199 const highlightThread = true
200 this.viewReplies(highlightedThreadId, highlightThread)
1263fc4e 201 }
4635f59d 202}