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'
18 const logger = debug('peertube:moderation:AbuseListTableComponent')
21 selector: 'my-abuse-list-table',
22 templateUrl: './abuse-list-table.component.html',
23 styleUrls: [ '../shared-moderation/moderation.scss', './abuse-list-table.component.scss' ]
25 export class AbuseListTableComponent extends RestTable implements OnInit, AfterViewInit {
26 @Input() viewType: 'admin' | 'user'
27 @Input() baseRoute: string
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>[][] = []
40 protected route: ActivatedRoute,
41 protected router: Router,
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,
49 private markdownRenderer: MarkdownService,
50 private sanitizer: DomSanitizer
57 this.buildInternalActions(),
59 this.buildFlaggedAccountActions(),
61 this.buildCommentActions(),
63 this.buildVideoActions(),
65 this.buildAccountActions()
69 this.listenToSearchChange()
73 if (this.search) this.setTableFilter(this.search, false)
77 return this.viewType === 'admin'
81 return 'AbuseListTableComponent'
84 openModerationCommentModal (abuse: AdminAbuse) {
85 this.moderationCommentModal.openModal(abuse)
88 onModerationCommentUpdated () {
92 isAbuseAccepted (abuse: AdminAbuse) {
93 return abuse.state.id === AbuseState.ACCEPTED
96 isAbuseRejected (abuse: AdminAbuse) {
97 return abuse.state.id === AbuseState.REJECTED
100 getVideoUrl (abuse: AdminAbuse) {
101 return Video.buildClientUrl(abuse.video.uuid)
104 getCommentUrl (abuse: AdminAbuse) {
105 return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
108 getAccountUrl (abuse: ProcessedAbuse) {
109 return '/accounts/' + abuse.flaggedAccount.nameWithHost
112 getVideoEmbed (abuse: AdminAbuse) {
113 return buildVideoOrPlaylistEmbed(
115 baseUrl: `${environment.originServerUrl}/videos/embed/${abuse.video.uuid}`,
118 startTime: abuse.video.startAt,
119 stopTime: abuse.video.endAt
125 async removeAbuse (abuse: AdminAbuse) {
126 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
127 if (res === false) return
129 this.abuseService.removeAbuse(abuse).subscribe(
131 this.notifier.success($localize`Abuse deleted.`)
135 err => this.notifier.error(err.message)
139 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
140 this.abuseService.updateAbuse(abuse, { state })
142 () => this.loadData(),
144 err => this.notifier.error(err.message)
148 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
149 const abuse = this.abuses.find(a => a.id === event.abuseId)
152 console.error('Cannot find abuse %d.', event.abuseId)
156 abuse.countMessages = event.countMessages
159 openAbuseMessagesModal (abuse: AdminAbuse) {
160 this.abuseMessagesModal.openModal(abuse)
163 isLocalAbuse (abuse: AdminAbuse) {
164 if (this.viewType === 'user') return true
166 return Actor.IS_LOCAL(abuse.reporterAccount.host)
169 protected loadData () {
170 logger('Loading data.')
173 pagination: this.pagination,
178 const observable = this.viewType === 'admin'
179 ? this.abuseService.getAdminAbuses(options)
180 : this.abuseService.getUserAbuses(options)
182 return observable.subscribe(
183 async resultList => {
184 this.totalRecords = resultList.total
188 for (const a of resultList.data) {
189 const abuse = a as ProcessedAbuse
191 abuse.reasonHtml = await this.toHtml(abuse.reason)
193 if (abuse.moderationComment) {
194 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
198 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
200 if (abuse.video.channel?.ownerAccount) {
201 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
206 if (abuse.comment.deleted) {
207 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
209 const truncated = truncate(abuse.comment.text, { length: 100 })
210 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
211 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
215 if (abuse.reporterAccount) {
216 abuse.reporterAccount = new Account(abuse.reporterAccount)
219 if (abuse.flaggedAccount) {
220 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
223 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
225 this.abuses.push(abuse)
229 err => this.notifier.error(err.message)
233 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
236 label: $localize`Internal actions`,
240 label: this.isAdminView()
241 ? $localize`Messages with reporter`
242 : $localize`Messages with moderators`,
243 handler: abuse => this.openAbuseMessagesModal(abuse),
244 isDisplayed: abuse => this.isLocalAbuse(abuse)
247 label: $localize`Update internal note`,
248 handler: abuse => this.openModerationCommentModal(abuse),
249 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
252 label: $localize`Mark as accepted`,
253 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
254 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
257 label: $localize`Mark as rejected`,
258 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
259 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
262 label: $localize`Add internal note`,
263 handler: abuse => this.openModerationCommentModal(abuse),
264 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
267 label: $localize`Delete report`,
268 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
273 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
274 if (!this.isAdminView()) return []
278 label: $localize`Actions for the flagged account`,
280 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
284 label: $localize`Mute account`,
285 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
286 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
290 label: $localize`Mute server account`,
291 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
292 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
297 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
298 if (!this.isAdminView()) return []
302 label: $localize`Actions for the reporter`,
304 isDisplayed: abuse => !!abuse.reporterAccount
308 label: $localize`Mute reporter`,
309 isDisplayed: abuse => !!abuse.reporterAccount,
310 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
314 label: $localize`Mute server`,
315 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
316 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
321 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
322 if (!this.isAdminView()) return []
326 label: $localize`Actions for the video`,
328 isDisplayed: abuse => abuse.video && !abuse.video.deleted
331 label: $localize`Block video`,
332 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
334 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal)
337 this.notifier.success($localize`Video blocked.`)
339 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
342 err => this.notifier.error(err.message)
347 label: $localize`Unblock video`,
348 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
350 this.videoBlocklistService.unblockVideo(abuse.video.id)
353 this.notifier.success($localize`Video unblocked.`)
355 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
358 err => this.notifier.error(err.message)
363 label: $localize`Delete video`,
364 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
365 handler: async abuse => {
366 const res = await this.confirmService.confirm(
367 $localize`Do you really want to delete this video?`,
370 if (res === false) return
372 this.videoService.removeVideo(abuse.video.id)
375 this.notifier.success($localize`Video deleted.`)
377 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
380 err => this.notifier.error(err.message)
387 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
388 if (!this.isAdminView()) return []
392 label: $localize`Actions for the comment`,
394 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
398 label: $localize`Delete comment`,
399 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
400 handler: async abuse => {
401 const res = await this.confirmService.confirm(
402 $localize`Do you really want to delete this comment?`,
405 if (res === false) return
407 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
410 this.notifier.success($localize`Comment deleted.`)
412 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
415 err => this.notifier.error(err.message)
422 private muteAccountHelper (account: Account) {
423 this.blocklistService.blockAccountByInstance(account)
426 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
427 account.mutedByInstance = true
430 err => this.notifier.error(err.message)
434 private muteServerHelper (host: string) {
435 this.blocklistService.blockServerByInstance(host)
438 this.notifier.success($localize`Server ${host} muted by the instance.`)
441 err => this.notifier.error(err.message)
445 private toHtml (text: string) {
446 return this.markdownRenderer.textMarkdownToHTML(text)