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