1 import { SortMeta } from 'primeng/api'
2 import { buildVideoEmbed, buildVideoLink } from 'src/assets/player/utils'
3 import { environment } from 'src/environments/environment'
4 import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'
5 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
6 import { ActivatedRoute, Params, 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 { I18n } from '@ngx-translate/i18n-polyfill'
11 import { Abuse, AbuseState } from '@shared/models'
12 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
13 import truncate from 'lodash-es/truncate'
15 export type ProcessedAbuse = Abuse & {
16 moderationCommentHtml?: string,
21 // override bare server-side definitions with rich client-side definitions
22 reporterAccount?: Account
23 flaggedAccount?: Account
25 truncatedCommentHtml?: string
28 video: Abuse['video'] & {
29 channel: Abuse['video']['channel'] & {
36 selector: 'my-abuse-list',
37 templateUrl: './abuse-list.component.html',
38 styleUrls: [ '../moderation.component.scss', './abuse-list.component.scss' ]
40 export class AbuseListComponent extends RestTable implements OnInit, AfterViewInit {
41 @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
43 abuses: ProcessedAbuse[] = []
45 sort: SortMeta = { field: 'createdAt', order: 1 }
46 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
48 abuseActions: DropdownAction<Abuse>[][] = []
51 private notifier: Notifier,
52 private abuseService: AbuseService,
53 private blocklistService: BlocklistService,
54 private videoService: VideoService,
55 private videoBlocklistService: VideoBlockService,
56 private confirmService: ConfirmService,
58 private markdownRenderer: MarkdownService,
59 private sanitizer: DomSanitizer,
60 private route: ActivatedRoute,
61 private router: Router
68 label: this.i18n('Internal actions'),
72 label: this.i18n('Delete report'),
73 handler: abuse => this.removeAbuse(abuse)
76 label: this.i18n('Add note'),
77 handler: abuse => this.openModerationCommentModal(abuse),
78 isDisplayed: abuse => !abuse.moderationComment
81 label: this.i18n('Update note'),
82 handler: abuse => this.openModerationCommentModal(abuse),
83 isDisplayed: abuse => !!abuse.moderationComment
86 label: this.i18n('Mark as accepted'),
87 handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
88 isDisplayed: abuse => !this.isAbuseAccepted(abuse)
91 label: this.i18n('Mark as rejected'),
92 handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
93 isDisplayed: abuse => !this.isAbuseRejected(abuse)
98 label: this.i18n('Actions for the video'),
100 isDisplayed: abuse => abuse.video && !abuse.video.deleted
103 label: this.i18n('Block video'),
104 isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
106 this.videoBlocklistService.blockVideo(abuse.video.id, undefined, true)
109 this.notifier.success(this.i18n('Video blocked.'))
111 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
114 err => this.notifier.error(err.message)
119 label: this.i18n('Unblock video'),
120 isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
122 this.videoBlocklistService.unblockVideo(abuse.video.id)
125 this.notifier.success(this.i18n('Video unblocked.'))
127 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
130 err => this.notifier.error(err.message)
135 label: this.i18n('Delete video'),
136 isDisplayed: abuse => abuse.video && !abuse.video.deleted,
137 handler: async abuse => {
138 const res = await this.confirmService.confirm(
139 this.i18n('Do you really want to delete this video?'),
142 if (res === false) return
144 this.videoService.removeVideo(abuse.video.id)
147 this.notifier.success(this.i18n('Video deleted.'))
149 this.updateAbuseState(abuse, AbuseState.ACCEPTED)
152 err => this.notifier.error(err.message)
159 label: this.i18n('Actions for the reporter'),
161 isDisplayed: abuse => !!abuse.reporterAccount
164 label: this.i18n('Mute reporter'),
165 isDisplayed: abuse => !!abuse.reporterAccount,
166 handler: async abuse => {
167 const account = abuse.reporterAccount as Account
169 this.blocklistService.blockAccountByInstance(account)
172 this.notifier.success(
173 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
176 account.mutedByInstance = true
179 err => this.notifier.error(err.message)
184 label: this.i18n('Mute server'),
185 isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
186 handler: async abuse => {
187 this.blocklistService.blockServerByInstance(abuse.reporterAccount.host)
190 this.notifier.success(
191 this.i18n('Server {{host}} muted by the instance.', { host: abuse.reporterAccount.host })
195 err => this.notifier.error(err.message)
206 this.route.queryParams
207 .subscribe(params => {
208 this.search = params.search || ''
210 this.setTableFilter(this.search)
216 if (this.search) this.setTableFilter(this.search)
220 return 'AbuseListComponent'
223 openModerationCommentModal (abuse: Abuse) {
224 this.moderationCommentModal.openModal(abuse)
227 onModerationCommentUpdated () {
231 /* Table filter functions */
232 onAbuseSearch (event: Event) {
234 this.setQueryParams((event.target as HTMLInputElement).value)
237 setQueryParams (search: string) {
238 const queryParams: Params = {}
239 if (search) Object.assign(queryParams, { search })
241 this.router.navigate([ '/admin/moderation/abuses/list' ], { queryParams })
244 resetTableFilter () {
245 this.setTableFilter('')
246 this.setQueryParams('')
249 /* END Table filter functions */
251 isAbuseAccepted (abuse: Abuse) {
252 return abuse.state.id === AbuseState.ACCEPTED
255 isAbuseRejected (abuse: Abuse) {
256 return abuse.state.id === AbuseState.REJECTED
259 getVideoUrl (abuse: Abuse) {
260 return Video.buildClientUrl(abuse.video.uuid)
263 getCommentUrl (abuse: Abuse) {
264 return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
267 getVideoEmbed (abuse: Abuse) {
268 return buildVideoEmbed(
270 baseUrl: `${environment.embedUrl}/videos/embed/${abuse.video.uuid}`,
273 startTime: abuse.startAt,
274 stopTime: abuse.endAt
279 switchToDefaultAvatar ($event: Event) {
280 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
283 async removeAbuse (abuse: Abuse) {
284 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
285 if (res === false) return
287 this.abuseService.removeAbuse(abuse).subscribe(
289 this.notifier.success(this.i18n('Abuse deleted.'))
293 err => this.notifier.error(err.message)
297 updateAbuseState (abuse: Abuse, state: AbuseState) {
298 this.abuseService.updateAbuse(abuse, { state })
300 () => this.loadData(),
302 err => this.notifier.error(err.message)
306 protected loadData () {
307 return this.abuseService.getAbuses({
308 pagination: this.pagination,
312 async resultList => {
313 this.totalRecords = resultList.total
317 for (const a of resultList.data) {
318 const abuse = a as ProcessedAbuse
320 abuse.reasonHtml = await this.toHtml(abuse.reason)
321 abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)
324 abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
326 if (abuse.video.channel?.ownerAccount) {
327 abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
332 if (abuse.comment.deleted) {
333 abuse.truncatedCommentHtml = abuse.commentHtml = this.i18n('Deleted comment')
335 const truncated = truncate(abuse.comment.text, { length: 100 })
336 abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
337 abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
341 if (abuse.reporterAccount) {
342 abuse.reporterAccount = new Account(abuse.reporterAccount)
345 if (abuse.flaggedAccount) {
346 abuse.flaggedAccount = new Account(abuse.flaggedAccount)
349 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
351 this.abuses.push(abuse)
355 err => this.notifier.error(err.message)
359 private toHtml (text: string) {
360 return this.markdownRenderer.textMarkdownToHTML(text)