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 { AfterViewInit, 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 { AbuseMessageModalComponent } from './abuse-message-modal.component'
15 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
16 import { ProcessedAbuse } from './processed-abuse.model'
17 import { AdvancedInputFilter } from '../shared-forms'
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, AfterViewInit {
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()
92 this.listenToSearchChange()
96 if (this.search) this.setTableFilter(this.search, false)
100 return this.viewType === 'admin'
104 return 'AbuseListTableComponent'
107 openModerationCommentModal (abuse: AdminAbuse) {
108 this.moderationCommentModal.openModal(abuse)
111 onModerationCommentUpdated () {
115 isAbuseAccepted (abuse: AdminAbuse) {
116 return abuse.state.id === AbuseState.ACCEPTED
119 isAbuseRejected (abuse: AdminAbuse) {
120 return abuse.state.id === AbuseState.REJECTED
123 getVideoUrl (abuse: AdminAbuse) {
124 return Video.buildClientUrl(abuse.video.uuid)
127 getCommentUrl (abuse: AdminAbuse) {
128 return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
131 getAccountUrl (abuse: ProcessedAbuse) {
132 return '/accounts/' + abuse.flaggedAccount.nameWithHost
135 getVideoEmbed (abuse: AdminAbuse) {
136 return buildVideoOrPlaylistEmbed(
138 baseUrl: `${environment.originServerUrl}/videos/embed/${abuse.video.uuid}`,
141 startTime: abuse.video.startAt,
142 stopTime: abuse.video.endAt
148 async removeAbuse (abuse: AdminAbuse) {
149 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
150 if (res === false) return
152 this.abuseService.removeAbuse(abuse).subscribe(
154 this.notifier.success($localize`Abuse deleted.`)
158 err => this.notifier.error(err.message)
162 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
163 this.abuseService.updateAbuse(abuse, { state })
165 () => this.loadData(),
167 err => this.notifier.error(err.message)
171 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
172 const abuse = this.abuses.find(a => a.id === event.abuseId)
175 console.error('Cannot find abuse %d.', event.abuseId)
179 abuse.countMessages = event.countMessages
182 openAbuseMessagesModal (abuse: AdminAbuse) {
183 this.abuseMessagesModal.openModal(abuse)
186 isLocalAbuse (abuse: AdminAbuse) {
187 if (this.viewType === 'user') return true
189 return Actor.IS_LOCAL(abuse.reporterAccount.host)
192 protected loadData () {
193 logger('Loading data.')
196 pagination: this.pagination,
201 const observable = this.viewType === 'admin'
202 ? this.abuseService.getAdminAbuses(options)
203 : this.abuseService.getUserAbuses(options)
205 return observable.subscribe(
206 async resultList => {
207 this.totalRecords = resultList.total
211 for (const a of resultList.data) {
212 const abuse = a as ProcessedAbuse
214 abuse.reasonHtml = await this.toHtml(abuse.reason)
216 if (abuse.moderationComment) {
217 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
221 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
223 if (abuse.video.channel?.ownerAccount) {
224 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
229 if (abuse.comment.deleted) {
230 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
232 const truncated = truncate(abuse.comment.text, { length: 100 })
233 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
234 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
238 if (abuse.reporterAccount) {
239 abuse.reporterAccount = new Account(abuse.reporterAccount)
242 if (abuse.flaggedAccount) {
243 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
246 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
248 this.abuses.push(abuse)
252 err => this.notifier.error(err.message)
256 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
259 label: $localize`Internal actions`,
263 label: this.isAdminView()
264 ? $localize`Messages with reporter`
265 : $localize`Messages with moderators`,
266 handler: abuse => this.openAbuseMessagesModal(abuse),
267 isDisplayed: abuse => this.isLocalAbuse(abuse)
270 label: $localize`Update internal note`,
271 handler: abuse => this.openModerationCommentModal(abuse),
272 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
275 label: $localize`Mark as accepted`,
276 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
277 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
280 label: $localize`Mark as rejected`,
281 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
282 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
285 label: $localize`Add internal note`,
286 handler: abuse => this.openModerationCommentModal(abuse),
287 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
290 label: $localize`Delete report`,
291 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
296 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
297 if (!this.isAdminView()) return []
301 label: $localize`Actions for the flagged account`,
303 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
307 label: $localize`Mute account`,
308 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
309 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
313 label: $localize`Mute server account`,
314 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
315 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
320 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
321 if (!this.isAdminView()) return []
325 label: $localize`Actions for the reporter`,
327 isDisplayed: abuse => !!abuse.reporterAccount
331 label: $localize`Mute reporter`,
332 isDisplayed: abuse => !!abuse.reporterAccount,
333 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
337 label: $localize`Mute server`,
338 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
339 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
344 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
345 if (!this.isAdminView()) return []
349 label: $localize`Actions for the video`,
351 isDisplayed: abuse => abuse.video && !abuse.video.deleted
354 label: $localize`Block video`,
355 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
357 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal)
360 this.notifier.success($localize`Video blocked.`)
362 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
365 err => this.notifier.error(err.message)
370 label: $localize`Unblock video`,
371 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
373 this.videoBlocklistService.unblockVideo(abuse.video.id)
376 this.notifier.success($localize`Video unblocked.`)
378 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
381 err => this.notifier.error(err.message)
386 label: $localize`Delete video`,
387 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
388 handler: async abuse => {
389 const res = await this.confirmService.confirm(
390 $localize`Do you really want to delete this video?`,
393 if (res === false) return
395 this.videoService.removeVideo(abuse.video.id)
398 this.notifier.success($localize`Video deleted.`)
400 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
403 err => this.notifier.error(err.message)
410 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
411 if (!this.isAdminView()) return []
415 label: $localize`Actions for the comment`,
417 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
421 label: $localize`Delete comment`,
422 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
423 handler: async abuse => {
424 const res = await this.confirmService.confirm(
425 $localize`Do you really want to delete this comment?`,
428 if (res === false) return
430 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
433 this.notifier.success($localize`Comment deleted.`)
435 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
438 err => this.notifier.error(err.message)
445 private muteAccountHelper (account: Account) {
446 this.blocklistService.blockAccountByInstance(account)
449 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
450 account.mutedByInstance = true
453 err => this.notifier.error(err.message)
457 private muteServerHelper (host: string) {
458 this.blocklistService.blockServerByInstance(host)
461 this.notifier.success($localize`Server ${host} muted by the instance.`)
464 err => this.notifier.error(err.message)
468 private toHtml (text: string) {
469 return this.markdownRenderer.textMarkdownToHTML(text)