]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comment.component.ts
Restore line feed for markdown lists support in comments
[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 @Input() redraftValue?: string
26
27 @Output() wantedToReply = new EventEmitter<VideoComment>()
28 @Output() wantedToDelete = new EventEmitter<VideoComment>()
29 @Output() wantedToRedraft = new EventEmitter<VideoComment>()
30 @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
31 @Output() resetReply = new EventEmitter()
32 @Output() timestampClicked = new EventEmitter<number>()
33
34 prependModerationActions: DropdownAction<any>[]
35
36 sanitizedCommentHTML = ''
37 newParentComments: VideoComment[] = []
38
39 commentAccount: Account
40 commentUser: User
41
42 constructor (
43 private markdownService: MarkdownService,
44 private authService: AuthService,
45 private userService: UserService,
46 private notifier: Notifier
47 ) {}
48
49 get user () {
50 return this.authService.getUser()
51 }
52
53 ngOnInit () {
54 this.init()
55 }
56
57 ngOnChanges () {
58 this.init()
59 }
60
61 onCommentReplyCreated (createdComment: VideoComment) {
62 if (!this.commentTree) {
63 this.commentTree = {
64 comment: this.comment,
65 children: []
66 }
67
68 this.threadCreated.emit(this.commentTree)
69 }
70
71 this.commentTree.children.unshift({
72 comment: createdComment,
73 children: []
74 })
75
76 this.resetReply.emit()
77
78 this.redraftValue = undefined
79 }
80
81 onWantToReply (comment?: VideoComment) {
82 this.wantedToReply.emit(comment || this.comment)
83 }
84
85 onWantToDelete (comment?: VideoComment) {
86 this.wantedToDelete.emit(comment || this.comment)
87 }
88
89 onWantToRedraft (comment?: VideoComment) {
90 this.wantedToRedraft.emit(comment || this.comment)
91 }
92
93 isUserLoggedIn () {
94 return this.authService.isLoggedIn()
95 }
96
97 onResetReply () {
98 this.resetReply.emit()
99 }
100
101 handleTimestampClicked (timestamp: number) {
102 this.timestampClicked.emit(timestamp)
103 }
104
105 isRemovableByUser () {
106 return this.comment.account && this.isUserLoggedIn() &&
107 (
108 this.user.account.id === this.comment.account.id ||
109 this.user.account.id === this.video.account.id ||
110 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
111 )
112 }
113
114 isRedraftableByUser () {
115 return (
116 this.comment.account &&
117 this.isUserLoggedIn() &&
118 this.user.account.id === this.comment.account.id &&
119 this.comment.totalReplies === 0
120 )
121 }
122
123 isReportableByUser () {
124 return (
125 this.comment.account &&
126 this.isUserLoggedIn() &&
127 this.comment.isDeleted === false &&
128 this.user.account.id !== this.comment.account.id
129 )
130 }
131
132 switchToDefaultAvatar ($event: Event) {
133 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
134 }
135
136 isNotDeletedOrDeletedWithReplies () {
137 return !this.comment.isDeleted || this.comment.isDeleted && this.comment.totalReplies !== 0
138 }
139
140 private getUserIfNeeded (account: Account) {
141 if (!account.userId) return
142 if (!this.authService.isLoggedIn()) return
143
144 const user = this.authService.getUser()
145 if (user.hasRight(UserRight.MANAGE_USERS)) {
146 this.userService.getUserWithCache(account.userId)
147 .subscribe(
148 user => this.commentUser = user,
149
150 err => this.notifier.error(err.message)
151 )
152 }
153 }
154
155 private async init () {
156 // Before HTML rendering restore line feed for markdown list compatibility
157 const commentText = this.comment.text.replace(/<br.?\/?>/g, '\r\n')
158 const html = await this.markdownService.textMarkdownToHTML(commentText, true, true)
159 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
160 this.newParentComments = this.parentComments.concat([ this.comment ])
161
162 if (this.comment.account) {
163 this.commentAccount = new Account(this.comment.account)
164 this.getUserIfNeeded(this.commentAccount)
165 } else {
166 this.comment.account = null
167 }
168
169 this.prependModerationActions = []
170
171 if (this.isReportableByUser()) {
172 this.prependModerationActions.push({
173 label: $localize`Report this comment`,
174 iconName: 'flag',
175 handler: () => this.showReportModal()
176 })
177 }
178
179 if (this.isRemovableByUser()) {
180 this.prependModerationActions.push({
181 label: $localize`Remove`,
182 iconName: 'delete',
183 handler: () => this.onWantToDelete()
184 })
185 }
186
187 if (this.isRedraftableByUser()) {
188 this.prependModerationActions.push({
189 label: $localize`Remove & re-draft`,
190 iconName: 'edit',
191 handler: () => this.onWantToRedraft()
192 })
193 }
194
195 if (this.prependModerationActions.length === 0) {
196 this.prependModerationActions = undefined
197 }
198 }
199
200 private showReportModal () {
201 this.commentReportModal.show()
202 }
203 }