]>
Commit | Line | Data |
---|---|---|
94148c90 C |
1 | import * as debug from 'debug' |
2 | import truncate from 'lodash-es/truncate' | |
3 | import { SortMeta } from 'primeng/api' | |
66357162 | 4 | import { buildVideoLink, buildVideoOrPlaylistEmbed } from 'src/assets/player/utils' |
94148c90 | 5 | import { environment } from 'src/environments/environment' |
66357162 | 6 | import { AfterViewInit, Component, Input, OnInit, ViewChild } from '@angular/core' |
94148c90 | 7 | import { DomSanitizer } from '@angular/platform-browser' |
5ed46c1b | 8 | import { ActivatedRoute, Router } from '@angular/router' |
94148c90 C |
9 | import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core' |
10 | import { Account, Actor, DropdownAction, Video, VideoService } from '@app/shared/shared-main' | |
11 | import { AbuseService, BlocklistService, VideoBlockService } from '@app/shared/shared-moderation' | |
12 | import { VideoCommentService } from '@app/shared/shared-video-comment' | |
94148c90 C |
13 | import { AbuseState, AdminAbuse } from '@shared/models' |
14 | import { AbuseMessageModalComponent } from './abuse-message-modal.component' | |
15 | import { ModerationCommentModalComponent } from './moderation-comment-modal.component' | |
16 | import { ProcessedAbuse } from './processed-abuse.model' | |
17 | ||
18 | const logger = debug('peertube:moderation:AbuseListTableComponent') | |
19 | ||
20 | @Component({ | |
21 | selector: 'my-abuse-list-table', | |
22 | templateUrl: './abuse-list-table.component.html', | |
23 | styleUrls: [ '../shared-moderation/moderation.scss', './abuse-list-table.component.scss' ] | |
24 | }) | |
25 | export class AbuseListTableComponent extends RestTable implements OnInit, AfterViewInit { | |
26 | @Input() viewType: 'admin' | 'user' | |
27 | @Input() baseRoute: string | |
28 | ||
29 | @ViewChild('abuseMessagesModal', { static: true }) abuseMessagesModal: AbuseMessageModalComponent | |
30 | @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent | |
31 | ||
32 | abuses: ProcessedAbuse[] = [] | |
33 | totalRecords = 0 | |
34 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
35 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
36 | ||
37 | abuseActions: DropdownAction<ProcessedAbuse>[][] = [] | |
38 | ||
39 | constructor ( | |
5ed46c1b C |
40 | protected route: ActivatedRoute, |
41 | protected router: Router, | |
94148c90 C |
42 | private notifier: Notifier, |
43 | private abuseService: AbuseService, | |
44 | private blocklistService: BlocklistService, | |
45 | private commentService: VideoCommentService, | |
46 | private videoService: VideoService, | |
47 | private videoBlocklistService: VideoBlockService, | |
48 | private confirmService: ConfirmService, | |
94148c90 | 49 | private markdownRenderer: MarkdownService, |
5ed46c1b | 50 | private sanitizer: DomSanitizer |
94148c90 C |
51 | ) { |
52 | super() | |
53 | } | |
54 | ||
55 | ngOnInit () { | |
56 | this.abuseActions = [ | |
57 | this.buildInternalActions(), | |
58 | ||
59 | this.buildFlaggedAccountActions(), | |
60 | ||
61 | this.buildCommentActions(), | |
62 | ||
63 | this.buildVideoActions(), | |
64 | ||
65 | this.buildAccountActions() | |
66 | ] | |
67 | ||
68 | this.initialize() | |
5ed46c1b | 69 | this.listenToSearchChange() |
94148c90 C |
70 | } |
71 | ||
72 | ngAfterViewInit () { | |
3f6441e0 | 73 | if (this.search) this.setTableFilter(this.search, false) |
94148c90 C |
74 | } |
75 | ||
76 | isAdminView () { | |
77 | return this.viewType === 'admin' | |
78 | } | |
79 | ||
80 | getIdentifier () { | |
81 | return 'AbuseListTableComponent' | |
82 | } | |
83 | ||
84 | openModerationCommentModal (abuse: AdminAbuse) { | |
85 | this.moderationCommentModal.openModal(abuse) | |
86 | } | |
87 | ||
88 | onModerationCommentUpdated () { | |
89 | this.loadData() | |
90 | } | |
91 | ||
94148c90 C |
92 | isAbuseAccepted (abuse: AdminAbuse) { |
93 | return abuse.state.id === AbuseState.ACCEPTED | |
94 | } | |
95 | ||
96 | isAbuseRejected (abuse: AdminAbuse) { | |
97 | return abuse.state.id === AbuseState.REJECTED | |
98 | } | |
99 | ||
100 | getVideoUrl (abuse: AdminAbuse) { | |
101 | return Video.buildClientUrl(abuse.video.uuid) | |
102 | } | |
103 | ||
104 | getCommentUrl (abuse: AdminAbuse) { | |
105 | return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId | |
106 | } | |
107 | ||
108 | getAccountUrl (abuse: ProcessedAbuse) { | |
109 | return '/accounts/' + abuse.flaggedAccount.nameWithHost | |
110 | } | |
111 | ||
112 | getVideoEmbed (abuse: AdminAbuse) { | |
951b582f | 113 | return buildVideoOrPlaylistEmbed( |
94148c90 | 114 | buildVideoLink({ |
5beb89f2 | 115 | baseUrl: `${environment.originServerUrl}/videos/embed/${abuse.video.uuid}`, |
94148c90 C |
116 | title: false, |
117 | warningTitle: false, | |
7a4ea932 C |
118 | startTime: abuse.video.startAt, |
119 | stopTime: abuse.video.endAt | |
4097c6d6 TP |
120 | }), |
121 | abuse.video.name | |
94148c90 C |
122 | ) |
123 | } | |
124 | ||
125 | switchToDefaultAvatar ($event: Event) { | |
c418d483 | 126 | ($event.target as HTMLImageElement).src = Account.GET_DEFAULT_AVATAR_URL() |
94148c90 C |
127 | } |
128 | ||
129 | async removeAbuse (abuse: AdminAbuse) { | |
66357162 | 130 | const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`) |
94148c90 C |
131 | if (res === false) return |
132 | ||
133 | this.abuseService.removeAbuse(abuse).subscribe( | |
134 | () => { | |
66357162 | 135 | this.notifier.success($localize`Abuse deleted.`) |
94148c90 C |
136 | this.loadData() |
137 | }, | |
138 | ||
139 | err => this.notifier.error(err.message) | |
140 | ) | |
141 | } | |
142 | ||
143 | updateAbuseState (abuse: AdminAbuse, state: AbuseState) { | |
144 | this.abuseService.updateAbuse(abuse, { state }) | |
145 | .subscribe( | |
146 | () => this.loadData(), | |
147 | ||
148 | err => this.notifier.error(err.message) | |
149 | ) | |
150 | } | |
151 | ||
152 | onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) { | |
153 | const abuse = this.abuses.find(a => a.id === event.abuseId) | |
154 | ||
155 | if (!abuse) { | |
156 | console.error('Cannot find abuse %d.', event.abuseId) | |
157 | return | |
158 | } | |
159 | ||
160 | abuse.countMessages = event.countMessages | |
161 | } | |
162 | ||
163 | openAbuseMessagesModal (abuse: AdminAbuse) { | |
164 | this.abuseMessagesModal.openModal(abuse) | |
165 | } | |
166 | ||
167 | isLocalAbuse (abuse: AdminAbuse) { | |
168 | if (this.viewType === 'user') return true | |
169 | ||
170 | return Actor.IS_LOCAL(abuse.reporterAccount.host) | |
171 | } | |
172 | ||
173 | protected loadData () { | |
174 | logger('Loading data.') | |
175 | ||
176 | const options = { | |
177 | pagination: this.pagination, | |
178 | sort: this.sort, | |
179 | search: this.search | |
180 | } | |
181 | ||
182 | const observable = this.viewType === 'admin' | |
183 | ? this.abuseService.getAdminAbuses(options) | |
184 | : this.abuseService.getUserAbuses(options) | |
185 | ||
186 | return observable.subscribe( | |
187 | async resultList => { | |
188 | this.totalRecords = resultList.total | |
189 | ||
190 | this.abuses = [] | |
191 | ||
192 | for (const a of resultList.data) { | |
193 | const abuse = a as ProcessedAbuse | |
194 | ||
195 | abuse.reasonHtml = await this.toHtml(abuse.reason) | |
196 | ||
197 | if (abuse.moderationComment) { | |
198 | abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment) | |
199 | } | |
200 | ||
201 | if (abuse.video) { | |
202 | abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)) | |
203 | ||
204 | if (abuse.video.channel?.ownerAccount) { | |
205 | abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount) | |
206 | } | |
207 | } | |
208 | ||
209 | if (abuse.comment) { | |
210 | if (abuse.comment.deleted) { | |
66357162 | 211 | abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment` |
94148c90 C |
212 | } else { |
213 | const truncated = truncate(abuse.comment.text, { length: 100 }) | |
214 | abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true) | |
215 | abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true) | |
216 | } | |
217 | } | |
218 | ||
219 | if (abuse.reporterAccount) { | |
220 | abuse.reporterAccount = new Account(abuse.reporterAccount) | |
221 | } | |
222 | ||
223 | if (abuse.flaggedAccount) { | |
224 | abuse.flaggedAccount = new Account(abuse.flaggedAccount) | |
225 | } | |
226 | ||
227 | if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt | |
228 | ||
229 | this.abuses.push(abuse) | |
230 | } | |
231 | }, | |
232 | ||
233 | err => this.notifier.error(err.message) | |
234 | ) | |
235 | } | |
236 | ||
237 | private buildInternalActions (): DropdownAction<ProcessedAbuse>[] { | |
238 | return [ | |
239 | { | |
66357162 | 240 | label: $localize`Internal actions`, |
94148c90 C |
241 | isHeader: true |
242 | }, | |
243 | { | |
244 | label: this.isAdminView() | |
66357162 C |
245 | ? $localize`Messages with reporter` |
246 | : $localize`Messages with moderators`, | |
94148c90 C |
247 | handler: abuse => this.openAbuseMessagesModal(abuse), |
248 | isDisplayed: abuse => this.isLocalAbuse(abuse) | |
249 | }, | |
250 | { | |
66357162 | 251 | label: $localize`Update internal note`, |
94148c90 C |
252 | handler: abuse => this.openModerationCommentModal(abuse), |
253 | isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment | |
254 | }, | |
255 | { | |
66357162 | 256 | label: $localize`Mark as accepted`, |
94148c90 C |
257 | handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED), |
258 | isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse) | |
259 | }, | |
260 | { | |
66357162 | 261 | label: $localize`Mark as rejected`, |
94148c90 C |
262 | handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED), |
263 | isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse) | |
264 | }, | |
265 | { | |
66357162 | 266 | label: $localize`Add internal note`, |
94148c90 C |
267 | handler: abuse => this.openModerationCommentModal(abuse), |
268 | isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment | |
269 | }, | |
270 | { | |
66357162 | 271 | label: $localize`Delete report`, |
94148c90 C |
272 | handler: abuse => this.isAdminView() && this.removeAbuse(abuse) |
273 | } | |
274 | ] | |
275 | } | |
276 | ||
277 | private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] { | |
278 | if (!this.isAdminView()) return [] | |
279 | ||
280 | return [ | |
281 | { | |
66357162 | 282 | label: $localize`Actions for the flagged account`, |
94148c90 C |
283 | isHeader: true, |
284 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video | |
285 | }, | |
286 | ||
287 | { | |
66357162 | 288 | label: $localize`Mute account`, |
94148c90 C |
289 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video, |
290 | handler: abuse => this.muteAccountHelper(abuse.flaggedAccount) | |
291 | }, | |
292 | ||
293 | { | |
66357162 | 294 | label: $localize`Mute server account`, |
94148c90 C |
295 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video, |
296 | handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host) | |
297 | } | |
298 | ] | |
299 | } | |
300 | ||
301 | private buildAccountActions (): DropdownAction<ProcessedAbuse>[] { | |
302 | if (!this.isAdminView()) return [] | |
303 | ||
304 | return [ | |
305 | { | |
66357162 | 306 | label: $localize`Actions for the reporter`, |
94148c90 C |
307 | isHeader: true, |
308 | isDisplayed: abuse => !!abuse.reporterAccount | |
309 | }, | |
310 | ||
311 | { | |
66357162 | 312 | label: $localize`Mute reporter`, |
94148c90 C |
313 | isDisplayed: abuse => !!abuse.reporterAccount, |
314 | handler: abuse => this.muteAccountHelper(abuse.reporterAccount) | |
315 | }, | |
316 | ||
317 | { | |
66357162 | 318 | label: $localize`Mute server`, |
94148c90 C |
319 | isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId, |
320 | handler: abuse => this.muteServerHelper(abuse.reporterAccount.host) | |
321 | } | |
322 | ] | |
323 | } | |
324 | ||
325 | private buildVideoActions (): DropdownAction<ProcessedAbuse>[] { | |
326 | if (!this.isAdminView()) return [] | |
327 | ||
328 | return [ | |
329 | { | |
66357162 | 330 | label: $localize`Actions for the video`, |
94148c90 C |
331 | isHeader: true, |
332 | isDisplayed: abuse => abuse.video && !abuse.video.deleted | |
333 | }, | |
334 | { | |
66357162 | 335 | label: $localize`Block video`, |
94148c90 C |
336 | isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted, |
337 | handler: abuse => { | |
365d9083 | 338 | this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal) |
94148c90 C |
339 | .subscribe( |
340 | () => { | |
66357162 | 341 | this.notifier.success($localize`Video blocked.`) |
94148c90 C |
342 | |
343 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
344 | }, | |
345 | ||
346 | err => this.notifier.error(err.message) | |
347 | ) | |
348 | } | |
349 | }, | |
350 | { | |
66357162 | 351 | label: $localize`Unblock video`, |
94148c90 C |
352 | isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted, |
353 | handler: abuse => { | |
354 | this.videoBlocklistService.unblockVideo(abuse.video.id) | |
355 | .subscribe( | |
356 | () => { | |
66357162 | 357 | this.notifier.success($localize`Video unblocked.`) |
94148c90 C |
358 | |
359 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
360 | }, | |
361 | ||
362 | err => this.notifier.error(err.message) | |
363 | ) | |
364 | } | |
365 | }, | |
366 | { | |
66357162 | 367 | label: $localize`Delete video`, |
94148c90 C |
368 | isDisplayed: abuse => abuse.video && !abuse.video.deleted, |
369 | handler: async abuse => { | |
370 | const res = await this.confirmService.confirm( | |
66357162 C |
371 | $localize`Do you really want to delete this video?`, |
372 | $localize`Delete` | |
94148c90 C |
373 | ) |
374 | if (res === false) return | |
375 | ||
376 | this.videoService.removeVideo(abuse.video.id) | |
377 | .subscribe( | |
378 | () => { | |
66357162 | 379 | this.notifier.success($localize`Video deleted.`) |
94148c90 C |
380 | |
381 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
382 | }, | |
383 | ||
384 | err => this.notifier.error(err.message) | |
385 | ) | |
386 | } | |
387 | } | |
388 | ] | |
389 | } | |
390 | ||
391 | private buildCommentActions (): DropdownAction<ProcessedAbuse>[] { | |
392 | if (!this.isAdminView()) return [] | |
393 | ||
394 | return [ | |
395 | { | |
66357162 | 396 | label: $localize`Actions for the comment`, |
94148c90 C |
397 | isHeader: true, |
398 | isDisplayed: abuse => abuse.comment && !abuse.comment.deleted | |
399 | }, | |
400 | ||
401 | { | |
66357162 | 402 | label: $localize`Delete comment`, |
94148c90 C |
403 | isDisplayed: abuse => abuse.comment && !abuse.comment.deleted, |
404 | handler: async abuse => { | |
405 | const res = await this.confirmService.confirm( | |
66357162 C |
406 | $localize`Do you really want to delete this comment?`, |
407 | $localize`Delete` | |
94148c90 C |
408 | ) |
409 | if (res === false) return | |
410 | ||
411 | this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id) | |
412 | .subscribe( | |
413 | () => { | |
66357162 | 414 | this.notifier.success($localize`Comment deleted.`) |
94148c90 C |
415 | |
416 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
417 | }, | |
418 | ||
419 | err => this.notifier.error(err.message) | |
420 | ) | |
421 | } | |
422 | } | |
423 | ] | |
424 | } | |
425 | ||
426 | private muteAccountHelper (account: Account) { | |
427 | this.blocklistService.blockAccountByInstance(account) | |
428 | .subscribe( | |
429 | () => { | |
66357162 | 430 | this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`) |
94148c90 C |
431 | account.mutedByInstance = true |
432 | }, | |
433 | ||
434 | err => this.notifier.error(err.message) | |
435 | ) | |
436 | } | |
437 | ||
438 | private muteServerHelper (host: string) { | |
439 | this.blocklistService.blockServerByInstance(host) | |
440 | .subscribe( | |
441 | () => { | |
66357162 | 442 | this.notifier.success($localize`Server ${host} muted by the instance.`) |
94148c90 C |
443 | }, |
444 | ||
445 | err => this.notifier.error(err.message) | |
446 | ) | |
447 | } | |
448 | ||
449 | private toHtml (text: string) { | |
450 | return this.markdownRenderer.textMarkdownToHTML(text) | |
451 | } | |
452 | } |