]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comments.component.ts
4d801c97057b4a1e67a9055da9535e79aeb9e3d7
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comments.component.ts
1 import { Component, Input, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4 import { AuthService } from '../../../core/auth'
5 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
6 import { User } from '../../../shared/users'
7 import { SortField } from '../../../shared/video/sort-field.type'
8 import { VideoDetails } from '../../../shared/video/video-details.model'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
12
13 @Component({
14 selector: 'my-video-comments',
15 templateUrl: './video-comments.component.html',
16 styleUrls: ['./video-comments.component.scss']
17 })
18 export class VideoCommentsComponent implements OnInit {
19 @Input() video: VideoDetails
20 @Input() user: User
21
22 comments: VideoComment[] = []
23 sort: SortField = '-createdAt'
24 componentPagination: ComponentPagination = {
25 currentPage: 1,
26 itemsPerPage: 10,
27 totalItems: null
28 }
29 inReplyToCommentId: number
30 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
31 threadLoading: { [ id: number ]: boolean } = {}
32
33 constructor (
34 private authService: AuthService,
35 private notificationsService: NotificationsService,
36 private videoCommentService: VideoCommentService
37 ) {}
38
39 ngOnInit () {
40 if (this.video.commentsEnabled === true) {
41 this.loadMoreComments()
42 }
43 }
44
45 viewReplies (comment: VideoComment) {
46 this.threadLoading[comment.id] = true
47
48 this.videoCommentService.getVideoThreadComments(this.video.id, comment.id)
49 .subscribe(
50 res => {
51 this.threadComments[comment.id] = res
52 this.threadLoading[comment.id] = false
53 },
54
55 err => this.notificationsService.error('Error', err.message)
56 )
57 }
58
59 loadMoreComments () {
60 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
61 .subscribe(
62 res => {
63 this.comments = this.comments.concat(res.comments)
64 this.componentPagination.totalItems = res.totalComments
65 },
66
67 err => this.notificationsService.error('Error', err.message)
68 )
69 }
70
71 onCommentThreadCreated (comment: VideoComment) {
72 this.comments.unshift(comment)
73 }
74
75 onWantedToReply (comment: VideoComment) {
76 this.inReplyToCommentId = comment.id
77 }
78
79 onResetReply () {
80 this.inReplyToCommentId = undefined
81 }
82
83 isUserLoggedIn () {
84 return this.authService.isLoggedIn()
85 }
86
87 onNearOfBottom () {
88 this.componentPagination.currentPage++
89
90 if (this.hasMoreComments()) {
91 this.loadMoreComments()
92 }
93 }
94
95 protected hasMoreComments () {
96 // No results
97 if (this.componentPagination.totalItems === 0) return false
98
99 // Not loaded yet
100 if (!this.componentPagination.totalItems) return true
101
102 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
103 return maxPage > this.componentPagination.currentPage
104 }
105 }