1 import { SortMeta } from 'primeng/api'
2 import { filter } from 'rxjs/operators'
3 import { buildVideoEmbed, buildVideoLink } from 'src/assets/player/utils'
4 import { environment } from 'src/environments/environment'
5 import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'
6 import { DomSanitizer } from '@angular/platform-browser'
7 import { ActivatedRoute, Params, Router } from '@angular/router'
8 import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core'
9 import { Account, Actor, DropdownAction, Video, VideoService } from '@app/shared/shared-main'
10 import { BlocklistService, VideoAbuseService, VideoBlockService } from '@app/shared/shared-moderation'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { VideoAbuse, VideoAbuseState } from '@shared/models'
13 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
15 export type ProcessedVideoAbuse = VideoAbuse & {
16 moderationCommentHtml?: string,
20 // override bare server-side definitions with rich client-side definitions
21 reporterAccount: Account
22 video: VideoAbuse['video'] & {
23 channel: VideoAbuse['video']['channel'] & {
30 selector: 'my-video-abuse-list',
31 templateUrl: './video-abuse-list.component.html',
32 styleUrls: [ '../moderation.component.scss', './video-abuse-list.component.scss' ]
34 export class VideoAbuseListComponent extends RestTable implements OnInit, AfterViewInit {
35 @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
37 videoAbuses: ProcessedVideoAbuse[] = []
39 sort: SortMeta = { field: 'createdAt', order: 1 }
40 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
42 videoAbuseActions: DropdownAction<VideoAbuse>[][] = []
45 private notifier: Notifier,
46 private videoAbuseService: VideoAbuseService,
47 private blocklistService: BlocklistService,
48 private videoService: VideoService,
49 private videoBlocklistService: VideoBlockService,
50 private confirmService: ConfirmService,
52 private markdownRenderer: MarkdownService,
53 private sanitizer: DomSanitizer,
54 private route: ActivatedRoute,
55 private router: Router
59 this.videoAbuseActions = [
62 label: this.i18n('Internal actions'),
66 label: this.i18n('Delete report'),
67 handler: videoAbuse => this.removeVideoAbuse(videoAbuse)
70 label: this.i18n('Add note'),
71 handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
72 isDisplayed: videoAbuse => !videoAbuse.moderationComment
75 label: this.i18n('Update note'),
76 handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
77 isDisplayed: videoAbuse => !!videoAbuse.moderationComment
80 label: this.i18n('Mark as accepted'),
81 handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED),
82 isDisplayed: videoAbuse => !this.isVideoAbuseAccepted(videoAbuse)
85 label: this.i18n('Mark as rejected'),
86 handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.REJECTED),
87 isDisplayed: videoAbuse => !this.isVideoAbuseRejected(videoAbuse)
92 label: this.i18n('Actions for the video'),
94 isDisplayed: videoAbuse => !videoAbuse.video.deleted
97 label: this.i18n('Block video'),
98 isDisplayed: videoAbuse => !videoAbuse.video.deleted && !videoAbuse.video.blacklisted,
99 handler: videoAbuse => {
100 this.videoBlocklistService.blockVideo(videoAbuse.video.id, undefined, true)
103 this.notifier.success(this.i18n('Video blocked.'))
105 this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
108 err => this.notifier.error(err.message)
113 label: this.i18n('Unblock video'),
114 isDisplayed: videoAbuse => !videoAbuse.video.deleted && videoAbuse.video.blacklisted,
115 handler: videoAbuse => {
116 this.videoBlocklistService.unblockVideo(videoAbuse.video.id)
119 this.notifier.success(this.i18n('Video unblocked.'))
121 this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
124 err => this.notifier.error(err.message)
129 label: this.i18n('Delete video'),
130 isDisplayed: videoAbuse => !videoAbuse.video.deleted,
131 handler: async videoAbuse => {
132 const res = await this.confirmService.confirm(
133 this.i18n('Do you really want to delete this video?'),
136 if (res === false) return
138 this.videoService.removeVideo(videoAbuse.video.id)
141 this.notifier.success(this.i18n('Video deleted.'))
143 this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
146 err => this.notifier.error(err.message)
153 label: this.i18n('Actions for the reporter'),
157 label: this.i18n('Mute reporter'),
158 handler: async videoAbuse => {
159 const account = videoAbuse.reporterAccount as Account
161 this.blocklistService.blockAccountByInstance(account)
164 this.notifier.success(
165 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
168 account.mutedByInstance = true
171 err => this.notifier.error(err.message)
176 label: this.i18n('Mute server'),
177 isDisplayed: videoAbuse => !videoAbuse.reporterAccount.userId,
178 handler: async videoAbuse => {
179 this.blocklistService.blockServerByInstance(videoAbuse.reporterAccount.host)
182 this.notifier.success(
183 this.i18n('Server {{host}} muted by the instance.', { host: videoAbuse.reporterAccount.host })
187 err => this.notifier.error(err.message)
198 this.route.queryParams
199 .pipe(filter(params => params.search !== undefined && params.search !== null))
200 .subscribe(params => {
201 this.search = params.search
202 this.setTableFilter(params.search)
208 if (this.search) this.setTableFilter(this.search)
212 return 'VideoAbuseListComponent'
215 openModerationCommentModal (videoAbuse: VideoAbuse) {
216 this.moderationCommentModal.openModal(videoAbuse)
219 onModerationCommentUpdated () {
223 /* Table filter functions */
224 onAbuseSearch (event: Event) {
226 this.setQueryParams((event.target as HTMLInputElement).value)
229 setQueryParams (search: string) {
230 const queryParams: Params = {}
231 if (search) Object.assign(queryParams, { search })
232 this.router.navigate([ '/admin/moderation/video-abuses/list' ], { queryParams })
235 resetTableFilter () {
236 this.setTableFilter('')
237 this.setQueryParams('')
240 /* END Table filter functions */
242 isVideoAbuseAccepted (videoAbuse: VideoAbuse) {
243 return videoAbuse.state.id === VideoAbuseState.ACCEPTED
246 isVideoAbuseRejected (videoAbuse: VideoAbuse) {
247 return videoAbuse.state.id === VideoAbuseState.REJECTED
250 getVideoUrl (videoAbuse: VideoAbuse) {
251 return Video.buildClientUrl(videoAbuse.video.uuid)
254 getVideoEmbed (videoAbuse: VideoAbuse) {
255 return buildVideoEmbed(
257 baseUrl: `${environment.embedUrl}/videos/embed/${videoAbuse.video.uuid}`,
260 startTime: videoAbuse.startAt,
261 stopTime: videoAbuse.endAt
266 switchToDefaultAvatar ($event: Event) {
267 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
270 async removeVideoAbuse (videoAbuse: VideoAbuse) {
271 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
272 if (res === false) return
274 this.videoAbuseService.removeVideoAbuse(videoAbuse).subscribe(
276 this.notifier.success(this.i18n('Abuse deleted.'))
280 err => this.notifier.error(err.message)
284 updateVideoAbuseState (videoAbuse: VideoAbuse, state: VideoAbuseState) {
285 this.videoAbuseService.updateVideoAbuse(videoAbuse, { state })
287 () => this.loadData(),
289 err => this.notifier.error(err.message)
293 protected loadData () {
294 return this.videoAbuseService.getVideoAbuses({
295 pagination: this.pagination,
299 async resultList => {
300 this.totalRecords = resultList.total
301 const videoAbuses = []
303 for (const abuse of resultList.data) {
304 Object.assign(abuse, {
305 reasonHtml: await this.toHtml(abuse.reason),
306 moderationCommentHtml: await this.toHtml(abuse.moderationComment),
307 embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)),
308 reporterAccount: new Account(abuse.reporterAccount)
311 if (abuse.video.channel?.ownerAccount) abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
312 if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
314 videoAbuses.push(abuse as ProcessedVideoAbuse)
317 this.videoAbuses = videoAbuses
320 err => this.notifier.error(err.message)
324 private toHtml (text: string) {
325 return this.markdownRenderer.textMarkdownToHTML(text)