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