1 import { SortMeta } from 'primeng/api'
2 import { filter, switchMap } from 'rxjs/operators'
3 import { AfterViewInit, Component, OnInit } from '@angular/core'
4 import { ActivatedRoute, Params, Router } from '@angular/router'
5 import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
6 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
7 import { VideoBlockService } from '@app/shared/shared-moderation'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { VideoBlacklist, VideoBlacklistType } from '@shared/models'
10 import { buildVideoOrPlaylistEmbed, buildVideoLink } from 'src/assets/player/utils'
11 import { environment } from 'src/environments/environment'
12 import { DomSanitizer } from '@angular/platform-browser'
15 selector: 'my-video-block-list',
16 templateUrl: './video-block-list.component.html',
17 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-block-list.component.scss' ]
19 export class VideoBlockListComponent extends RestTable implements OnInit, AfterViewInit {
20 blocklist: (VideoBlacklist & { reasonHtml?: string, embedHtml?: string })[] = []
22 sort: SortMeta = { field: 'createdAt', order: -1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24 blocklistTypeFilter: VideoBlacklistType = undefined
26 videoBlocklistActions: DropdownAction<VideoBlacklist>[][] = []
29 private notifier: Notifier,
30 private serverService: ServerService,
31 private confirmService: ConfirmService,
32 private videoBlocklistService: VideoBlockService,
33 private markdownRenderer: MarkdownService,
34 private sanitizer: DomSanitizer,
35 private videoService: VideoService,
36 private route: ActivatedRoute,
37 private router: Router,
42 this.videoBlocklistActions = [
45 label: this.i18n('Internal actions'),
47 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
50 label: this.i18n('Switch video block to manual'),
51 handler: videoBlock => {
52 this.videoBlocklistService.unblockVideo(videoBlock.video.id).pipe(
53 switchMap(_ => this.videoBlocklistService.blockVideo(videoBlock.video.id, undefined, true))
56 this.notifier.success(this.i18n('Video {{name}} switched to manual block.', { name: videoBlock.video.name }))
60 err => this.notifier.error(err.message)
63 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
68 label: this.i18n('Actions for the video'),
72 label: this.i18n('Unblock'),
73 handler: videoBlock => this.unblockVideo(videoBlock)
77 label: this.i18n('Delete'),
78 handler: async videoBlock => {
79 const res = await this.confirmService.confirm(
80 this.i18n('Do you really want to delete this video?'),
83 if (res === false) return
85 this.videoService.removeVideo(videoBlock.video.id)
88 this.notifier.success(this.i18n('Video deleted.'))
91 err => this.notifier.error(err.message)
100 this.serverService.getConfig()
101 .subscribe(config => {
102 // don't filter if auto-blacklist is not enabled as this will be the only list
103 if (config.autoBlacklist.videos.ofUsers.enabled) {
104 this.blocklistTypeFilter = VideoBlacklistType.MANUAL
110 this.route.queryParams
111 .pipe(filter(params => params.search !== undefined && params.search !== null))
112 .subscribe(params => {
113 this.search = params.search
114 this.setTableFilter(params.search)
120 if (this.search) this.setTableFilter(this.search)
123 /* Table filter functions */
124 onBlockSearch (event: Event) {
126 this.setQueryParams((event.target as HTMLInputElement).value)
129 setQueryParams (search: string) {
130 const queryParams: Params = {}
131 if (search) Object.assign(queryParams, { search })
132 this.router.navigate([ '/admin/moderation/video-blocks/list' ], { queryParams })
135 resetTableFilter () {
136 this.setTableFilter('')
137 this.setQueryParams('')
140 /* END Table filter functions */
143 return 'VideoBlockListComponent'
146 getVideoUrl (videoBlock: VideoBlacklist) {
147 return Video.buildClientUrl(videoBlock.video.uuid)
150 booleanToText (value: boolean) {
151 if (value === true) return this.i18n('yes')
153 return this.i18n('no')
156 toHtml (text: string) {
157 return this.markdownRenderer.textMarkdownToHTML(text)
160 async unblockVideo (entry: VideoBlacklist) {
161 const confirmMessage = this.i18n(
162 'Do you really want to unblock this video? It will be available again in the videos list.'
165 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblock'))
166 if (res === false) return
168 this.videoBlocklistService.unblockVideo(entry.video.id).subscribe(
170 this.notifier.success(this.i18n('Video {{name}} unblocked.', { name: entry.video.name }))
174 err => this.notifier.error(err.message)
178 getVideoEmbed (entry: VideoBlacklist) {
179 return buildVideoOrPlaylistEmbed(
181 baseUrl: `${environment.embedUrl}/videos/embed/${entry.video.uuid}`,
188 protected loadData () {
189 this.videoBlocklistService.listBlocks({
190 pagination: this.pagination,
195 async resultList => {
196 this.totalRecords = resultList.total
198 this.blocklist = resultList.data
200 for (const element of this.blocklist) {
201 Object.assign(element, {
202 reasonHtml: await this.toHtml(element.reason),
203 embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(element))
208 err => this.notifier.error(err.message)