]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comment.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment.component.ts
1
2 import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
3 import { MarkdownService, Notifier, UserService } from '@app/core'
4 import { AuthService } from '@app/core/auth'
5 import { Account, Actor, DropdownAction, Video } from '@app/shared/shared-main'
6 import { CommentReportComponent } from '@app/shared/shared-moderation/report-modals/comment-report.component'
7 import { VideoComment, VideoCommentThreadTree } from '@app/shared/shared-video-comment'
8 import { User, UserRight } from '@shared/models'
9
10 @Component({
11 selector: 'my-video-comment',
12 templateUrl: './video-comment.component.html',
13 styleUrls: ['./video-comment.component.scss']
14 })
15 export class VideoCommentComponent implements OnInit, OnChanges {
16 @ViewChild('commentReportModal') commentReportModal: CommentReportComponent
17
18 @Input() video: Video
19 @Input() comment: VideoComment
20 @Input() parentComments: VideoComment[] = []
21 @Input() commentTree: VideoCommentThreadTree
22 @Input() inReplyToCommentId: number
23 @Input() highlightedComment = false
24 @Input() firstInThread = false
25
26 @Output() wantedToDelete = new EventEmitter<VideoComment>()
27 @Output() wantedToReply = new EventEmitter<VideoComment>()
28 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
29 @Output() resetReply = new EventEmitter()
30 @Output() timestampClicked = new EventEmitter<number>()
31
32 prependModerationActions: DropdownAction<any>[]
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 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.account.id === this.video.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
129 if (this.comment.account) {
130 this.commentAccount = new Account(this.comment.account)
131 this.getUserIfNeeded(this.commentAccount)
132 } else {
133 this.comment.account = null
134 }
135
136 if (this.isUserLoggedIn() && this.comment.isDeleted === false && this.authService.getUser().account.id !== this.comment.account.id) {
137 this.prependModerationActions = [
138 {
139 label: $localize`Report comment`,
140 handler: () => this.showReportModal()
141 }
142 ]
143 } else {
144 this.prependModerationActions = undefined
145 }
146 }
147
148 private showReportModal () {
149 this.commentReportModal.show()
150 }
151 }