]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment.component.ts
Add get user cache for comments
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2 import { User, UserRight } from '../../../../../../shared/models/users'
3 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4 import { AuthService } from '@app/core/auth'
5 import { AccountService } from '@app/shared/account/account.service'
6 import { Video } from '@app/shared/video/video.model'
7 import { VideoComment } from './video-comment.model'
8 import { MarkdownService } from '@app/shared/renderer'
9 import { Account } from '@app/shared/account/account.model'
10 import { Notifier } from '@app/core'
11 import { UserService } from '@app/shared'
12
13 @Component({
14 selector: 'my-video-comment',
15 templateUrl: './video-comment.component.html',
16 styleUrls: ['./video-comment.component.scss']
17 })
18 export class VideoCommentComponent implements OnInit, OnChanges {
19 @Input() video: Video
20 @Input() comment: VideoComment
21 @Input() parentComments: VideoComment[] = []
22 @Input() commentTree: VideoCommentThreadTree
23 @Input() inReplyToCommentId: number
24 @Input() highlightedComment = false
25 @Input() firstInThread = false
26
27 @Output() wantedToDelete = new EventEmitter<VideoComment>()
28 @Output() wantedToReply = new EventEmitter<VideoComment>()
29 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
30 @Output() resetReply = new EventEmitter()
31 @Output() timestampClicked = new EventEmitter<number>()
32
33 sanitizedCommentHTML = ''
34 newParentComments: VideoComment[] = []
35
36 commentAccount: Account
37 commentUser: User
38
39 constructor (
40 private markdownService: MarkdownService,
41 private authService: AuthService,
42 private accountService: AccountService,
43 private userService: UserService,
44 private notifier: Notifier
45 ) {}
46
47 get user () {
48 return this.authService.getUser()
49 }
50
51 ngOnInit () {
52 this.init()
53 }
54
55 ngOnChanges () {
56 this.init()
57 }
58
59 onCommentReplyCreated (createdComment: VideoComment) {
60 if (!this.commentTree) {
61 this.commentTree = {
62 comment: this.comment,
63 children: []
64 }
65
66 this.threadCreated.emit(this.commentTree)
67 }
68
69 this.commentTree.children.unshift({
70 comment: createdComment,
71 children: []
72 })
73 this.resetReply.emit()
74 }
75
76 onWantToReply (comment?: VideoComment) {
77 this.wantedToReply.emit(comment || this.comment)
78 }
79
80 onWantToDelete (comment?: VideoComment) {
81 this.wantedToDelete.emit(comment || this.comment)
82 }
83
84 isUserLoggedIn () {
85 return this.authService.isLoggedIn()
86 }
87
88 onResetReply () {
89 this.resetReply.emit()
90 }
91
92 handleTimestampClicked (timestamp: number) {
93 this.timestampClicked.emit(timestamp)
94 }
95
96 isRemovableByUser () {
97 return this.comment.account && this.isUserLoggedIn() &&
98 (
99 this.user.account.id === this.comment.account.id ||
100 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
101 )
102 }
103
104 private getUserIfNeeded (account: Account) {
105 if (!account.userId) return
106 if (!this.authService.isLoggedIn()) return
107
108 const user = this.authService.getUser()
109 if (user.hasRight(UserRight.MANAGE_USERS)) {
110 this.userService.getUserWithCache(account.userId)
111 .subscribe(
112 user => this.commentUser = user,
113
114 err => this.notifier.error(err.message)
115 )
116 }
117 }
118
119 private async init () {
120 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
121 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
122 this.newParentComments = this.parentComments.concat([ this.comment ])
123 this.commentAccount = new Account(this.comment.account)
124 this.getUserIfNeeded(this.commentAccount)
125 }
126 }