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 switchToDefaultAvatar ($event: Event) {
126 ($event.target as HTMLImageElement).src = Account.GET_DEFAULT_AVATAR_URL()
129 async removeAbuse (abuse: AdminAbuse) {
130 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
131 if (res === false) return
133 this.abuseService.removeAbuse(abuse).subscribe(
135 this.notifier.success($localize`Abuse deleted.`)
139 err => this.notifier.error(err.message)
143 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
144 this.abuseService.updateAbuse(abuse, { state })
146 () => this.loadData(),
148 err => this.notifier.error(err.message)
152 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
153 const abuse = this.abuses.find(a => a.id === event.abuseId)
156 console.error('Cannot find abuse %d.', event.abuseId)
160 abuse.countMessages = event.countMessages
163 openAbuseMessagesModal (abuse: AdminAbuse) {
164 this.abuseMessagesModal.openModal(abuse)
167 isLocalAbuse (abuse: AdminAbuse) {
168 if (this.viewType === 'user') return true
170 return Actor.IS_LOCAL(abuse.reporterAccount.host)
173 protected loadData () {
174 logger('Loading data.')
177 pagination: this.pagination,
182 const observable = this.viewType === 'admin'
183 ? this.abuseService.getAdminAbuses(options)
184 : this.abuseService.getUserAbuses(options)
186 return observable.subscribe(
187 async resultList => {
188 this.totalRecords = resultList.total
192 for (const a of resultList.data) {
193 const abuse = a as ProcessedAbuse
195 abuse.reasonHtml = await this.toHtml(abuse.reason)
197 if (abuse.moderationComment) {
198 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
202 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
204 if (abuse.video.channel?.ownerAccount) {
205 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
210 if (abuse.comment.deleted) {
211 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
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)
219 if (abuse.reporterAccount) {
220 abuse.reporterAccount = new Account(abuse.reporterAccount)
223 if (abuse.flaggedAccount) {
224 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
227 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
229 this.abuses.push(abuse)
233 err => this.notifier.error(err.message)
237 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
240 label: $localize`Internal actions`,
244 label: this.isAdminView()
245 ? $localize`Messages with reporter`
246 : $localize`Messages with moderators`,
247 handler: abuse => this.openAbuseMessagesModal(abuse),
248 isDisplayed: abuse => this.isLocalAbuse(abuse)
251 label: $localize`Update internal note`,
252 handler: abuse => this.openModerationCommentModal(abuse),
253 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
256 label: $localize`Mark as accepted`,
257 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
258 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
261 label: $localize`Mark as rejected`,
262 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
263 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
266 label: $localize`Add internal note`,
267 handler: abuse => this.openModerationCommentModal(abuse),
268 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
271 label: $localize`Delete report`,
272 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
277 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
278 if (!this.isAdminView()) return []
282 label: $localize`Actions for the flagged account`,
284 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
288 label: $localize`Mute account`,
289 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
290 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
294 label: $localize`Mute server account`,
295 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
296 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
301 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
302 if (!this.isAdminView()) return []
306 label: $localize`Actions for the reporter`,
308 isDisplayed: abuse => !!abuse.reporterAccount
312 label: $localize`Mute reporter`,
313 isDisplayed: abuse => !!abuse.reporterAccount,
314 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
318 label: $localize`Mute server`,
319 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
320 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
325 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
326 if (!this.isAdminView()) return []
330 label: $localize`Actions for the video`,
332 isDisplayed: abuse => abuse.video && !abuse.video.deleted
335 label: $localize`Block video`,
336 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
338 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal)
341 this.notifier.success($localize`Video blocked.`)
343 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
346 err => this.notifier.error(err.message)
351 label: $localize`Unblock video`,
352 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
354 this.videoBlocklistService.unblockVideo(abuse.video.id)
357 this.notifier.success($localize`Video unblocked.`)
359 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
362 err => this.notifier.error(err.message)
367 label: $localize`Delete video`,
368 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
369 handler: async abuse => {
370 const res = await this.confirmService.confirm(
371 $localize`Do you really want to delete this video?`,
374 if (res === false) return
376 this.videoService.removeVideo(abuse.video.id)
379 this.notifier.success($localize`Video deleted.`)
381 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
384 err => this.notifier.error(err.message)
391 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
392 if (!this.isAdminView()) return []
396 label: $localize`Actions for the comment`,
398 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
402 label: $localize`Delete comment`,
403 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
404 handler: async abuse => {
405 const res = await this.confirmService.confirm(
406 $localize`Do you really want to delete this comment?`,
409 if (res === false) return
411 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
414 this.notifier.success($localize`Comment deleted.`)
416 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
419 err => this.notifier.error(err.message)
426 private muteAccountHelper (account: Account) {
427 this.blocklistService.blockAccountByInstance(account)
430 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
431 account.mutedByInstance = true
434 err => this.notifier.error(err.message)
438 private muteServerHelper (host: string) {
439 this.blocklistService.blockServerByInstance(host)
442 this.notifier.success($localize`Server ${host} muted by the instance.`)
445 err => this.notifier.error(err.message)
449 private toHtml (text: string) {
450 return this.markdownRenderer.textMarkdownToHTML(text)