1 import * as debug from 'debug'
2 import truncate from 'lodash-es/truncate'
3 import { SortMeta } from 'primeng/api'
4 import { buildVideoLink, buildVideoOrPlaylistEmbed } from 'src/assets/player/utils'
5 import { environment } from 'src/environments/environment'
6 import { Component, Input, OnInit, ViewChild } from '@angular/core'
7 import { DomSanitizer } from '@angular/platform-browser'
8 import { ActivatedRoute, Router } from '@angular/router'
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'
13 import { AbuseState, AdminAbuse } from '@shared/models'
14 import { AdvancedInputFilter } from '../shared-forms'
15 import { AbuseMessageModalComponent } from './abuse-message-modal.component'
16 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
17 import { ProcessedAbuse } from './processed-abuse.model'
19 const logger = debug('peertube:moderation:AbuseListTableComponent')
22 selector: 'my-abuse-list-table',
23 templateUrl: './abuse-list-table.component.html',
24 styleUrls: [ '../shared-moderation/moderation.scss', './abuse-list-table.component.scss' ]
26 export class AbuseListTableComponent extends RestTable implements OnInit {
27 @Input() viewType: 'admin' | 'user'
29 @ViewChild('abuseMessagesModal', { static: true }) abuseMessagesModal: AbuseMessageModalComponent
30 @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
32 abuses: ProcessedAbuse[] = []
34 sort: SortMeta = { field: 'createdAt', order: 1 }
35 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
37 abuseActions: DropdownAction<ProcessedAbuse>[][] = []
39 inputFilters: AdvancedInputFilter[] = [
41 queryParams: { 'search': 'state:pending' },
42 label: $localize`Unsolved reports`
45 queryParams: { 'search': 'state:accepted' },
46 label: $localize`Accepted reports`
49 queryParams: { 'search': 'state:rejected' },
50 label: $localize`Refused reports`
53 queryParams: { 'search': 'videoIs:blacklisted' },
54 label: $localize`Reports with blocked videos`
57 queryParams: { 'search': 'videoIs:deleted' },
58 label: $localize`Reports with deleted videos`
63 protected route: ActivatedRoute,
64 protected router: Router,
65 private notifier: Notifier,
66 private abuseService: AbuseService,
67 private blocklistService: BlocklistService,
68 private commentService: VideoCommentService,
69 private videoService: VideoService,
70 private videoBlocklistService: VideoBlockService,
71 private confirmService: ConfirmService,
72 private markdownRenderer: MarkdownService,
73 private sanitizer: DomSanitizer
80 this.buildInternalActions(),
82 this.buildFlaggedAccountActions(),
84 this.buildCommentActions(),
86 this.buildVideoActions(),
88 this.buildAccountActions()
95 return this.viewType === 'admin'
99 return 'AbuseListTableComponent'
102 openModerationCommentModal (abuse: AdminAbuse) {
103 this.moderationCommentModal.openModal(abuse)
106 onModerationCommentUpdated () {
110 isAbuseAccepted (abuse: AdminAbuse) {
111 return abuse.state.id === AbuseState.ACCEPTED
114 isAbuseRejected (abuse: AdminAbuse) {
115 return abuse.state.id === AbuseState.REJECTED
118 getVideoUrl (abuse: AdminAbuse) {
119 return Video.buildClientUrl(abuse.video.uuid)
122 getCommentUrl (abuse: AdminAbuse) {
123 return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
126 getAccountUrl (abuse: ProcessedAbuse) {
127 return '/a/' + abuse.flaggedAccount.nameWithHost
130 getVideoEmbed (abuse: AdminAbuse) {
131 return buildVideoOrPlaylistEmbed(
133 baseUrl: `${environment.originServerUrl}/videos/embed/${abuse.video.uuid}`,
136 startTime: abuse.video.startAt,
137 stopTime: abuse.video.endAt
143 async removeAbuse (abuse: AdminAbuse) {
144 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
145 if (res === false) return
147 this.abuseService.removeAbuse(abuse).subscribe(
149 this.notifier.success($localize`Abuse deleted.`)
153 err => this.notifier.error(err.message)
157 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
158 this.abuseService.updateAbuse(abuse, { state })
160 () => this.reloadData(),
162 err => this.notifier.error(err.message)
166 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
167 const abuse = this.abuses.find(a => a.id === event.abuseId)
170 console.error('Cannot find abuse %d.', event.abuseId)
174 abuse.countMessages = event.countMessages
177 openAbuseMessagesModal (abuse: AdminAbuse) {
178 this.abuseMessagesModal.openModal(abuse)
181 isLocalAbuse (abuse: AdminAbuse) {
182 if (this.viewType === 'user') return true
184 return Actor.IS_LOCAL(abuse.reporterAccount.host)
187 protected reloadData () {
188 logger('Loading data.')
191 pagination: this.pagination,
196 const observable = this.viewType === 'admin'
197 ? this.abuseService.getAdminAbuses(options)
198 : this.abuseService.getUserAbuses(options)
200 return observable.subscribe(
201 async resultList => {
202 this.totalRecords = resultList.total
206 for (const a of resultList.data) {
207 const abuse = a as ProcessedAbuse
209 abuse.reasonHtml = await this.toHtml(abuse.reason)
211 if (abuse.moderationComment) {
212 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
216 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
218 if (abuse.video.channel?.ownerAccount) {
219 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
224 if (abuse.comment.deleted) {
225 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
227 const truncated = truncate(abuse.comment.text, { length: 100 })
228 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
229 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
233 if (abuse.reporterAccount) {
234 abuse.reporterAccount = new Account(abuse.reporterAccount)
237 if (abuse.flaggedAccount) {
238 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
241 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
243 this.abuses.push(abuse)
247 err => this.notifier.error(err.message)
251 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
254 label: $localize`Internal actions`,
258 label: this.isAdminView()
259 ? $localize`Messages with reporter`
260 : $localize`Messages with moderators`,
261 handler: abuse => this.openAbuseMessagesModal(abuse),
262 isDisplayed: abuse => this.isLocalAbuse(abuse)
265 label: $localize`Update internal note`,
266 handler: abuse => this.openModerationCommentModal(abuse),
267 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
270 label: $localize`Mark as accepted`,
271 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
272 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
275 label: $localize`Mark as rejected`,
276 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
277 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
280 label: $localize`Add internal note`,
281 handler: abuse => this.openModerationCommentModal(abuse),
282 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
285 label: $localize`Delete report`,
286 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
291 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
292 if (!this.isAdminView()) return []
296 label: $localize`Actions for the flagged account`,
298 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
302 label: $localize`Mute account`,
303 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
304 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
308 label: $localize`Mute server account`,
309 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
310 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
315 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
316 if (!this.isAdminView()) return []
320 label: $localize`Actions for the reporter`,
322 isDisplayed: abuse => !!abuse.reporterAccount
326 label: $localize`Mute reporter`,
327 isDisplayed: abuse => !!abuse.reporterAccount,
328 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
332 label: $localize`Mute server`,
333 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
334 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
339 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
340 if (!this.isAdminView()) return []
344 label: $localize`Actions for the video`,
346 isDisplayed: abuse => abuse.video && !abuse.video.deleted
349 label: $localize`Block video`,
350 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
352 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal)
355 this.notifier.success($localize`Video blocked.`)
357 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
360 err => this.notifier.error(err.message)
365 label: $localize`Unblock video`,
366 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
368 this.videoBlocklistService.unblockVideo(abuse.video.id)
371 this.notifier.success($localize`Video unblocked.`)
373 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
376 err => this.notifier.error(err.message)
381 label: $localize`Delete video`,
382 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
383 handler: async abuse => {
384 const res = await this.confirmService.confirm(
385 $localize`Do you really want to delete this video?`,
388 if (res === false) return
390 this.videoService.removeVideo(abuse.video.id)
393 this.notifier.success($localize`Video deleted.`)
395 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
398 err => this.notifier.error(err.message)
405 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
406 if (!this.isAdminView()) return []
410 label: $localize`Actions for the comment`,
412 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
416 label: $localize`Delete comment`,
417 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
418 handler: async abuse => {
419 const res = await this.confirmService.confirm(
420 $localize`Do you really want to delete this comment?`,
423 if (res === false) return
425 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
428 this.notifier.success($localize`Comment deleted.`)
430 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
433 err => this.notifier.error(err.message)
440 private muteAccountHelper (account: Account) {
441 this.blocklistService.blockAccountByInstance(account)
444 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
445 account.mutedByInstance = true
448 err => this.notifier.error(err.message)
452 private muteServerHelper (host: string) {
453 this.blocklistService.blockServerByInstance(host)
456 this.notifier.success($localize`Server ${host} muted by the instance.`)
459 err => this.notifier.error(err.message)
463 private toHtml (text: string) {
464 return this.markdownRenderer.textMarkdownToHTML(text)