1 import * as debug from 'debug'
2 import truncate from 'lodash-es/truncate'
3 import { SortMeta } from 'primeng/api'
4 import { Component, Input, OnInit, ViewChild } from '@angular/core'
5 import { DomSanitizer } from '@angular/platform-browser'
6 import { ActivatedRoute, Router } from '@angular/router'
7 import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core'
8 import { Account, Actor, DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9 import { AbuseService, BlocklistService, VideoBlockService } from '@app/shared/shared-moderation'
10 import { VideoCommentService } from '@app/shared/shared-video-comment'
11 import { AbuseState, AdminAbuse } from '@shared/models'
12 import { AdvancedInputFilter } from '../shared-forms'
13 import { AbuseMessageModalComponent } from './abuse-message-modal.component'
14 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
15 import { ProcessedAbuse } from './processed-abuse.model'
17 const logger = debug('peertube:moderation:AbuseListTableComponent')
20 selector: 'my-abuse-list-table',
21 templateUrl: './abuse-list-table.component.html',
22 styleUrls: [ '../shared-moderation/moderation.scss', './abuse-list-table.component.scss' ]
24 export class AbuseListTableComponent extends RestTable implements OnInit {
25 @Input() viewType: 'admin' | 'user'
27 @ViewChild('abuseMessagesModal', { static: true }) abuseMessagesModal: AbuseMessageModalComponent
28 @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
30 abuses: ProcessedAbuse[] = []
32 sort: SortMeta = { field: 'createdAt', order: 1 }
33 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
35 abuseActions: DropdownAction<ProcessedAbuse>[][] = []
37 inputFilters: AdvancedInputFilter[] = [
39 title: $localize`Advanced filters`,
42 value: 'state:pending',
43 label: $localize`Unsolved reports`
46 value: 'state:accepted',
47 label: $localize`Accepted reports`
50 value: 'state:rejected',
51 label: $localize`Refused reports`
54 value: 'videoIs:blacklisted',
55 label: $localize`Reports with blocked videos`
58 value: 'videoIs:deleted',
59 label: $localize`Reports with deleted videos`
66 protected route: ActivatedRoute,
67 protected router: Router,
68 private notifier: Notifier,
69 private abuseService: AbuseService,
70 private blocklistService: BlocklistService,
71 private commentService: VideoCommentService,
72 private videoService: VideoService,
73 private videoBlocklistService: VideoBlockService,
74 private confirmService: ConfirmService,
75 private markdownRenderer: MarkdownService,
76 private sanitizer: DomSanitizer
83 this.buildInternalActions(),
85 this.buildFlaggedAccountActions(),
87 this.buildCommentActions(),
89 this.buildVideoActions(),
91 this.buildAccountActions()
98 return this.viewType === 'admin'
102 return 'AbuseListTableComponent'
105 openModerationCommentModal (abuse: AdminAbuse) {
106 this.moderationCommentModal.openModal(abuse)
109 onModerationCommentUpdated () {
113 isAbuseAccepted (abuse: AdminAbuse) {
114 return abuse.state.id === AbuseState.ACCEPTED
117 isAbuseRejected (abuse: AdminAbuse) {
118 return abuse.state.id === AbuseState.REJECTED
121 getVideoUrl (abuse: AdminAbuse) {
122 return Video.buildWatchUrl(abuse.video)
125 getCommentUrl (abuse: AdminAbuse) {
126 return Video.buildWatchUrl(abuse.comment.video) + ';threadId=' + abuse.comment.threadId
129 getAccountUrl (abuse: ProcessedAbuse) {
130 return '/a/' + abuse.flaggedAccount.nameWithHost
133 async removeAbuse (abuse: AdminAbuse) {
134 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
135 if (res === false) return
137 this.abuseService.removeAbuse(abuse)
140 this.notifier.success($localize`Abuse deleted.`)
144 error: err => this.notifier.error(err.message)
148 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
149 this.abuseService.updateAbuse(abuse, { state })
151 next: () => this.reloadData(),
153 error: err => this.notifier.error(err.message)
157 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
158 const abuse = this.abuses.find(a => a.id === event.abuseId)
161 console.error('Cannot find abuse %d.', event.abuseId)
165 abuse.countMessages = event.countMessages
168 openAbuseMessagesModal (abuse: AdminAbuse) {
169 this.abuseMessagesModal.openModal(abuse)
172 isLocalAbuse (abuse: AdminAbuse) {
173 if (this.viewType === 'user') return true
174 if (!abuse.reporterAccount) return false
176 return Actor.IS_LOCAL(abuse.reporterAccount.host)
179 protected reloadData () {
180 logger('Loading data.')
183 pagination: this.pagination,
188 const observable = this.viewType === 'admin'
189 ? this.abuseService.getAdminAbuses(options)
190 : this.abuseService.getUserAbuses(options)
192 return observable.subscribe({
193 next: async resultList => {
194 this.totalRecords = resultList.total
198 for (const a of resultList.data) {
199 const abuse = a as ProcessedAbuse
201 abuse.reasonHtml = await this.toHtml(abuse.reason)
203 if (abuse.moderationComment) {
204 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
208 if (abuse.video.channel?.ownerAccount) {
209 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
214 if (abuse.comment.deleted) {
215 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
217 const truncated = truncate(abuse.comment.text, { length: 100 })
218 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
219 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
223 if (abuse.reporterAccount) {
224 abuse.reporterAccount = new Account(abuse.reporterAccount)
227 if (abuse.flaggedAccount) {
228 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
231 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
233 this.abuses.push(abuse)
237 error: err => this.notifier.error(err.message)
241 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
244 label: $localize`Internal actions`,
248 label: this.isAdminView()
249 ? $localize`Messages with reporter`
250 : $localize`Messages with moderators`,
251 handler: abuse => this.openAbuseMessagesModal(abuse),
252 isDisplayed: abuse => this.isLocalAbuse(abuse)
255 label: $localize`Update internal note`,
256 handler: abuse => this.openModerationCommentModal(abuse),
257 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
260 label: $localize`Mark as accepted`,
261 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
262 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
265 label: $localize`Mark as rejected`,
266 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
267 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
270 label: $localize`Add internal note`,
271 handler: abuse => this.openModerationCommentModal(abuse),
272 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
275 label: $localize`Delete report`,
276 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
281 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
282 if (!this.isAdminView()) return []
286 label: $localize`Actions for the flagged account`,
288 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
292 label: $localize`Mute account`,
293 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
294 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
298 label: $localize`Mute server account`,
299 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
300 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
305 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
306 if (!this.isAdminView()) return []
310 label: $localize`Actions for the reporter`,
312 isDisplayed: abuse => !!abuse.reporterAccount
316 label: $localize`Mute reporter`,
317 isDisplayed: abuse => !!abuse.reporterAccount,
318 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
322 label: $localize`Mute server`,
323 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
324 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
329 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
330 if (!this.isAdminView()) return []
334 label: $localize`Actions for the video`,
336 isDisplayed: abuse => abuse.video && !abuse.video.deleted
339 label: $localize`Block video`,
340 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
342 this.videoBlocklistService.blockVideo([ { videoId: abuse.video.id, unfederate: abuse.video.channel.isLocal } ])
345 this.notifier.success($localize`Video blocked.`)
347 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
350 error: err => this.notifier.error(err.message)
355 label: $localize`Unblock video`,
356 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
358 this.videoBlocklistService.unblockVideo(abuse.video.id)
361 this.notifier.success($localize`Video unblocked.`)
363 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
366 error: err => this.notifier.error(err.message)
371 label: $localize`Delete video`,
372 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
373 handler: async abuse => {
374 const res = await this.confirmService.confirm(
375 $localize`Do you really want to delete this video?`,
378 if (res === false) return
380 this.videoService.removeVideo(abuse.video.id)
383 this.notifier.success($localize`Video deleted.`)
385 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
388 error: err => this.notifier.error(err.message)
395 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
396 if (!this.isAdminView()) return []
400 label: $localize`Actions for the comment`,
402 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
406 label: $localize`Delete comment`,
407 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
408 handler: async abuse => {
409 const res = await this.confirmService.confirm(
410 $localize`Do you really want to delete this comment?`,
413 if (res === false) return
415 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
418 this.notifier.success($localize`Comment deleted.`)
420 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
423 error: err => this.notifier.error(err.message)
430 private muteAccountHelper (account: Account) {
431 this.blocklistService.blockAccountByInstance(account)
434 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
435 account.mutedByInstance = true
438 error: err => this.notifier.error(err.message)
442 private muteServerHelper (host: string) {
443 this.blocklistService.blockServerByInstance(host)
446 this.notifier.success($localize`Server ${host} muted by the instance.`)
449 error: err => this.notifier.error(err.message)
453 private toHtml (text: string) {
454 return this.markdownRenderer.textMarkdownToHTML(text)