1 import { SortMeta } from 'primeng/api'
2 import { switchMap } from 'rxjs/operators'
3 import { buildVideoLink, buildVideoOrPlaylistEmbed } from 'src/assets/player/utils'
4 import { environment } from 'src/environments/environment'
5 import { AfterViewInit, Component, OnInit } 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, ServerService } from '@app/core'
9 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
10 import { VideoBlockService } from '@app/shared/shared-moderation'
11 import { VideoBlacklist, VideoBlacklistType } from '@shared/models'
14 selector: 'my-video-block-list',
15 templateUrl: './video-block-list.component.html',
16 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-block-list.component.scss' ]
18 export class VideoBlockListComponent extends RestTable implements OnInit, AfterViewInit {
19 blocklist: (VideoBlacklist & { reasonHtml?: string, embedHtml?: string })[] = []
21 sort: SortMeta = { field: 'createdAt', order: -1 }
22 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23 blocklistTypeFilter: VideoBlacklistType = undefined
25 videoBlocklistActions: DropdownAction<VideoBlacklist>[][] = []
28 protected route: ActivatedRoute,
29 protected router: Router,
30 private notifier: Notifier,
31 private serverService: ServerService,
32 private confirmService: ConfirmService,
33 private videoBlocklistService: VideoBlockService,
34 private markdownRenderer: MarkdownService,
35 private sanitizer: DomSanitizer,
36 private videoService: VideoService
40 this.videoBlocklistActions = [
43 label: $localize`Internal actions`,
45 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
48 label: $localize`Switch video block to manual`,
49 handler: videoBlock => {
50 this.videoBlocklistService.unblockVideo(videoBlock.video.id).pipe(
51 switchMap(_ => this.videoBlocklistService.blockVideo(videoBlock.video.id, undefined, true))
54 this.notifier.success($localize`Video ${videoBlock.video.name} switched to manual block.`)
58 err => this.notifier.error(err.message)
61 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
66 label: $localize`Actions for the video`,
70 label: $localize`Unblock`,
71 handler: videoBlock => this.unblockVideo(videoBlock)
75 label: $localize`Delete`,
76 handler: async videoBlock => {
77 const res = await this.confirmService.confirm(
78 $localize`Do you really want to delete this video?`,
81 if (res === false) return
83 this.videoService.removeVideo(videoBlock.video.id)
86 this.notifier.success($localize`Video deleted.`)
89 err => this.notifier.error(err.message)
98 this.serverService.getConfig()
99 .subscribe(config => {
100 // don't filter if auto-blacklist is not enabled as this will be the only list
101 if (config.autoBlacklist.videos.ofUsers.enabled) {
102 this.blocklistTypeFilter = VideoBlacklistType.MANUAL
107 this.listenToSearchChange()
111 if (this.search) this.setTableFilter(this.search, false)
114 /* Table filter functions */
115 onBlockSearch (event: Event) {
117 this.setQueryParams((event.target as HTMLInputElement).value)
120 setQueryParams (search: string) {
121 const queryParams: Params = {}
122 if (search) Object.assign(queryParams, { search })
123 this.router.navigate([ '/admin/moderation/video-blocks/list' ], { queryParams })
126 resetTableFilter () {
127 this.setTableFilter('')
128 this.setQueryParams('')
131 /* END Table filter functions */
134 return 'VideoBlockListComponent'
137 getVideoUrl (videoBlock: VideoBlacklist) {
138 return Video.buildClientUrl(videoBlock.video.uuid)
141 toHtml (text: string) {
142 return this.markdownRenderer.textMarkdownToHTML(text)
145 async unblockVideo (entry: VideoBlacklist) {
146 const confirmMessage = $localize`Do you really want to unblock this video? It will be available again in the videos list.`
148 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock`)
149 if (res === false) return
151 this.videoBlocklistService.unblockVideo(entry.video.id).subscribe(
153 this.notifier.success($localize`Video ${entry.video.name} unblocked.`)
157 err => this.notifier.error(err.message)
161 getVideoEmbed (entry: VideoBlacklist) {
162 return buildVideoOrPlaylistEmbed(
164 baseUrl: `${environment.originServerUrl}/videos/embed/${entry.video.uuid}`,
172 protected loadData () {
173 this.videoBlocklistService.listBlocks({
174 pagination: this.pagination,
179 async resultList => {
180 this.totalRecords = resultList.total
182 this.blocklist = resultList.data
184 for (const element of this.blocklist) {
185 Object.assign(element, {
186 reasonHtml: await this.toHtml(element.reason),
187 embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(element))
192 err => this.notifier.error(err.message)