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
124 switchToDefaultAvatar ($event: Event) {
125 ($event.target as HTMLImageElement).src = Account.GET_DEFAULT_AVATAR_URL()
128 async removeAbuse (abuse: AdminAbuse) {
129 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
130 if (res === false) return
132 this.abuseService.removeAbuse(abuse).subscribe(
134 this.notifier.success($localize`Abuse deleted.`)
138 err => this.notifier.error(err.message)
142 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
143 this.abuseService.updateAbuse(abuse, { state })
145 () => this.loadData(),
147 err => this.notifier.error(err.message)
151 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
152 const abuse = this.abuses.find(a => a.id === event.abuseId)
155 console.error('Cannot find abuse %d.', event.abuseId)
159 abuse.countMessages = event.countMessages
162 openAbuseMessagesModal (abuse: AdminAbuse) {
163 this.abuseMessagesModal.openModal(abuse)
166 isLocalAbuse (abuse: AdminAbuse) {
167 if (this.viewType === 'user') return true
169 return Actor.IS_LOCAL(abuse.reporterAccount.host)
172 protected loadData () {
173 logger('Loading data.')
176 pagination: this.pagination,
181 const observable = this.viewType === 'admin'
182 ? this.abuseService.getAdminAbuses(options)
183 : this.abuseService.getUserAbuses(options)
185 return observable.subscribe(
186 async resultList => {
187 this.totalRecords = resultList.total
191 for (const a of resultList.data) {
192 const abuse = a as ProcessedAbuse
194 abuse.reasonHtml = await this.toHtml(abuse.reason)
196 if (abuse.moderationComment) {
197 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
201 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
203 if (abuse.video.channel?.ownerAccount) {
204 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
209 if (abuse.comment.deleted) {
210 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
212 const truncated = truncate(abuse.comment.text, { length: 100 })
213 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
214 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
218 if (abuse.reporterAccount) {
219 abuse.reporterAccount = new Account(abuse.reporterAccount)
222 if (abuse.flaggedAccount) {
223 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
226 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
228 this.abuses.push(abuse)
232 err => this.notifier.error(err.message)
236 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
239 label: $localize`Internal actions`,
243 label: this.isAdminView()
244 ? $localize`Messages with reporter`
245 : $localize`Messages with moderators`,
246 handler: abuse => this.openAbuseMessagesModal(abuse),
247 isDisplayed: abuse => this.isLocalAbuse(abuse)
250 label: $localize`Update internal note`,
251 handler: abuse => this.openModerationCommentModal(abuse),
252 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
255 label: $localize`Mark as accepted`,
256 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
257 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
260 label: $localize`Mark as rejected`,
261 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
262 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
265 label: $localize`Add internal note`,
266 handler: abuse => this.openModerationCommentModal(abuse),
267 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
270 label: $localize`Delete report`,
271 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
276 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
277 if (!this.isAdminView()) return []
281 label: $localize`Actions for the flagged account`,
283 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
287 label: $localize`Mute account`,
288 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
289 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
293 label: $localize`Mute server account`,
294 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
295 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
300 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
301 if (!this.isAdminView()) return []
305 label: $localize`Actions for the reporter`,
307 isDisplayed: abuse => !!abuse.reporterAccount
311 label: $localize`Mute reporter`,
312 isDisplayed: abuse => !!abuse.reporterAccount,
313 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
317 label: $localize`Mute server`,
318 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
319 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
324 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
325 if (!this.isAdminView()) return []
329 label: $localize`Actions for the video`,
331 isDisplayed: abuse => abuse.video && !abuse.video.deleted
334 label: $localize`Block video`,
335 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
337 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, abuse.video.channel.isLocal)
340 this.notifier.success($localize`Video blocked.`)
342 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
345 err => this.notifier.error(err.message)
350 label: $localize`Unblock video`,
351 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
353 this.videoBlocklistService.unblockVideo(abuse.video.id)
356 this.notifier.success($localize`Video unblocked.`)
358 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
361 err => this.notifier.error(err.message)
366 label: $localize`Delete video`,
367 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
368 handler: async abuse => {
369 const res = await this.confirmService.confirm(
370 $localize`Do you really want to delete this video?`,
373 if (res === false) return
375 this.videoService.removeVideo(abuse.video.id)
378 this.notifier.success($localize`Video deleted.`)
380 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
383 err => this.notifier.error(err.message)
390 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
391 if (!this.isAdminView()) return []
395 label: $localize`Actions for the comment`,
397 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
401 label: $localize`Delete comment`,
402 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
403 handler: async abuse => {
404 const res = await this.confirmService.confirm(
405 $localize`Do you really want to delete this comment?`,
408 if (res === false) return
410 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
413 this.notifier.success($localize`Comment deleted.`)
415 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
418 err => this.notifier.error(err.message)
425 private muteAccountHelper (account: Account) {
426 this.blocklistService.blockAccountByInstance(account)
429 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
430 account.mutedByInstance = true
433 err => this.notifier.error(err.message)
437 private muteServerHelper (host: string) {
438 this.blocklistService.blockServerByInstance(host)
441 this.notifier.success($localize`Server ${host} muted by the instance.`)
444 err => this.notifier.error(err.message)
448 private toHtml (text: string) {
449 return this.markdownRenderer.textMarkdownToHTML(text)