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, Params, 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 private notifier: Notifier,
41 private abuseService: AbuseService,
42 private blocklistService: BlocklistService,
43 private commentService: VideoCommentService,
44 private videoService: VideoService,
45 private videoBlocklistService: VideoBlockService,
46 private confirmService: ConfirmService,
47 private markdownRenderer: MarkdownService,
48 private sanitizer: DomSanitizer,
49 private route: ActivatedRoute,
50 private router: Router
57 this.buildInternalActions(),
59 this.buildFlaggedAccountActions(),
61 this.buildCommentActions(),
63 this.buildVideoActions(),
65 this.buildAccountActions()
70 this.route.queryParams
71 .subscribe(params => {
72 this.search = params.search || ''
74 logger('On URL change (search: %s).', this.search)
76 this.setTableFilter(this.search)
82 if (this.search) this.setTableFilter(this.search)
86 return this.viewType === 'admin'
90 return 'AbuseListTableComponent'
93 openModerationCommentModal (abuse: AdminAbuse) {
94 this.moderationCommentModal.openModal(abuse)
97 onModerationCommentUpdated () {
101 /* Table filter functions */
102 onAbuseSearch (event: Event) {
104 this.setQueryParams((event.target as HTMLInputElement).value)
107 setQueryParams (search: string) {
108 const queryParams: Params = {}
109 if (search) Object.assign(queryParams, { search })
111 this.router.navigate([ this.baseRoute ], { queryParams })
114 resetTableFilter () {
115 this.setTableFilter('')
116 this.setQueryParams('')
119 /* END Table filter functions */
121 isAbuseAccepted (abuse: AdminAbuse) {
122 return abuse.state.id === AbuseState.ACCEPTED
125 isAbuseRejected (abuse: AdminAbuse) {
126 return abuse.state.id === AbuseState.REJECTED
129 getVideoUrl (abuse: AdminAbuse) {
130 return Video.buildClientUrl(abuse.video.uuid)
133 getCommentUrl (abuse: AdminAbuse) {
134 return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
137 getAccountUrl (abuse: ProcessedAbuse) {
138 return '/accounts/' + abuse.flaggedAccount.nameWithHost
141 getVideoEmbed (abuse: AdminAbuse) {
142 return buildVideoOrPlaylistEmbed(
144 baseUrl: `${environment.embedUrl}/videos/embed/${abuse.video.uuid}`,
147 startTime: abuse.startAt,
148 stopTime: abuse.endAt
153 switchToDefaultAvatar ($event: Event) {
154 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
157 async removeAbuse (abuse: AdminAbuse) {
158 const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`)
159 if (res === false) return
161 this.abuseService.removeAbuse(abuse).subscribe(
163 this.notifier.success($localize`Abuse deleted.`)
167 err => this.notifier.error(err.message)
171 updateAbuseState (abuse: AdminAbuse, state: AbuseState) {
172 this.abuseService.updateAbuse(abuse, { state })
174 () => this.loadData(),
176 err => this.notifier.error(err.message)
180 onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) {
181 const abuse = this.abuses.find(a => a.id === event.abuseId)
184 console.error('Cannot find abuse %d.', event.abuseId)
188 abuse.countMessages = event.countMessages
191 openAbuseMessagesModal (abuse: AdminAbuse) {
192 this.abuseMessagesModal.openModal(abuse)
195 isLocalAbuse (abuse: AdminAbuse) {
196 if (this.viewType === 'user') return true
198 return Actor.IS_LOCAL(abuse.reporterAccount.host)
201 protected loadData () {
202 logger('Loading data.')
205 pagination: this.pagination,
210 const observable = this.viewType === 'admin'
211 ? this.abuseService.getAdminAbuses(options)
212 : this.abuseService.getUserAbuses(options)
214 return observable.subscribe(
215 async resultList => {
216 this.totalRecords = resultList.total
220 for (const a of resultList.data) {
221 const abuse = a as ProcessedAbuse
223 abuse.reasonHtml = await this.toHtml(abuse.reason)
225 if (abuse.moderationComment) {
226 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
230 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
232 if (abuse.video.channel?.ownerAccount) {
233 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
238 if (abuse.comment.deleted) {
239 abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment`
241 const truncated = truncate(abuse.comment.text, { length: 100 })
242 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
243 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
247 if (abuse.reporterAccount) {
248 abuse.reporterAccount = new Account(abuse.reporterAccount)
251 if (abuse.flaggedAccount) {
252 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
255 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
257 this.abuses.push(abuse)
261 err => this.notifier.error(err.message)
265 private buildInternalActions (): DropdownAction<ProcessedAbuse>[] {
268 label: $localize`Internal actions`,
272 label: this.isAdminView()
273 ? $localize`Messages with reporter`
274 : $localize`Messages with moderators`,
275 handler: abuse => this.openAbuseMessagesModal(abuse),
276 isDisplayed: abuse => this.isLocalAbuse(abuse)
279 label: $localize`Update internal note`,
280 handler: abuse => this.openModerationCommentModal(abuse),
281 isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment
284 label: $localize`Mark as accepted`,
285 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
286 isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse)
289 label: $localize`Mark as rejected`,
290 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
291 isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse)
294 label: $localize`Add internal note`,
295 handler: abuse => this.openModerationCommentModal(abuse),
296 isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment
299 label: $localize`Delete report`,
300 handler: abuse => this.isAdminView() && this.removeAbuse(abuse)
305 private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] {
306 if (!this.isAdminView()) return []
310 label: $localize`Actions for the flagged account`,
312 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video
316 label: $localize`Mute account`,
317 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
318 handler: abuse => this.muteAccountHelper(abuse.flaggedAccount)
322 label: $localize`Mute server account`,
323 isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video,
324 handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host)
329 private buildAccountActions (): DropdownAction<ProcessedAbuse>[] {
330 if (!this.isAdminView()) return []
334 label: $localize`Actions for the reporter`,
336 isDisplayed: abuse => !!abuse.reporterAccount
340 label: $localize`Mute reporter`,
341 isDisplayed: abuse => !!abuse.reporterAccount,
342 handler: abuse => this.muteAccountHelper(abuse.reporterAccount)
346 label: $localize`Mute server`,
347 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
348 handler: abuse => this.muteServerHelper(abuse.reporterAccount.host)
353 private buildVideoActions (): DropdownAction<ProcessedAbuse>[] {
354 if (!this.isAdminView()) return []
358 label: $localize`Actions for the video`,
360 isDisplayed: abuse => abuse.video && !abuse.video.deleted
363 label: $localize`Block video`,
364 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
366 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, true)
369 this.notifier.success($localize`Video blocked.`)
371 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
374 err => this.notifier.error(err.message)
379 label: $localize`Unblock video`,
380 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
382 this.videoBlocklistService.unblockVideo(abuse.video.id)
385 this.notifier.success($localize`Video unblocked.`)
387 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
390 err => this.notifier.error(err.message)
395 label: $localize`Delete video`,
396 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
397 handler: async abuse => {
398 const res = await this.confirmService.confirm(
399 $localize`Do you really want to delete this video?`,
402 if (res === false) return
404 this.videoService.removeVideo(abuse.video.id)
407 this.notifier.success($localize`Video deleted.`)
409 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
412 err => this.notifier.error(err.message)
419 private buildCommentActions (): DropdownAction<ProcessedAbuse>[] {
420 if (!this.isAdminView()) return []
424 label: $localize`Actions for the comment`,
426 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted
430 label: $localize`Delete comment`,
431 isDisplayed: abuse => abuse.comment && !abuse.comment.deleted,
432 handler: async abuse => {
433 const res = await this.confirmService.confirm(
434 $localize`Do you really want to delete this comment?`,
437 if (res === false) return
439 this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id)
442 this.notifier.success($localize`Comment deleted.`)
444 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
447 err => this.notifier.error(err.message)
454 private muteAccountHelper (account: Account) {
455 this.blocklistService.blockAccountByInstance(account)
458 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
459 account.mutedByInstance = true
462 err => this.notifier.error(err.message)
466 private muteServerHelper (host: string) {
467 this.blocklistService.blockServerByInstance(host)
470 this.notifier.success($localize`Server ${host} muted by the instance.`)
473 err => this.notifier.error(err.message)
477 private toHtml (text: string) {
478 return this.markdownRenderer.textMarkdownToHTML(text)