]>
Commit | Line | Data |
---|---|---|
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.authService.getUser().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 | const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true, true) | |
157 | this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html) | |
158 | this.newParentComments = this.parentComments.concat([ this.comment ]) | |
159 | ||
160 | if (this.comment.account) { | |
161 | this.commentAccount = new Account(this.comment.account) | |
162 | this.getUserIfNeeded(this.commentAccount) | |
163 | } else { | |
164 | this.comment.account = null | |
165 | } | |
166 | ||
167 | this.prependModerationActions = [] | |
168 | ||
169 | if (this.isReportableByUser()) { | |
170 | this.prependModerationActions.push({ | |
171 | label: $localize`Report`, | |
172 | iconName: 'flag', | |
173 | handler: () => this.showReportModal() | |
174 | }) | |
175 | } | |
176 | ||
177 | if (this.isRemovableByUser()) { | |
178 | this.prependModerationActions.push({ | |
179 | label: $localize`Remove`, | |
180 | iconName: 'delete', | |
181 | handler: () => this.onWantToDelete() | |
182 | }) | |
183 | } | |
184 | ||
185 | if (this.isRedraftableByUser()) { | |
186 | this.prependModerationActions.push({ | |
187 | label: $localize`Remove & re-draft`, | |
188 | iconName: 'edit', | |
189 | handler: () => this.onWantToRedraft() | |
190 | }) | |
191 | } | |
192 | ||
193 | if (this.prependModerationActions.length === 0) { | |
194 | this.prependModerationActions = undefined | |
195 | } | |
196 | } | |
197 | ||
198 | private showReportModal () { | |
199 | this.commentReportModal.show() | |
200 | } | |
201 | } |