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'
12 selector: 'my-video-block-list',
13 templateUrl: './video-block-list.component.html',
14 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-block-list.component.scss' ]
16 export class VideoBlockListComponent extends RestTable implements OnInit, AfterViewInit {
17 blocklist: (VideoBlacklist & { reasonHtml?: string })[] = []
19 sort: SortMeta = { field: 'createdAt', order: -1 }
20 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21 blocklistTypeFilter: VideoBlacklistType = undefined
23 videoBlocklistActions: DropdownAction<VideoBlacklist>[][] = []
26 private notifier: Notifier,
27 private serverService: ServerService,
28 private confirmService: ConfirmService,
29 private videoBlocklistService: VideoBlockService,
30 private markdownRenderer: MarkdownService,
31 private videoService: VideoService,
32 private route: ActivatedRoute,
33 private router: Router,
38 this.videoBlocklistActions = [
41 label: this.i18n('Internal actions'),
43 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
46 label: this.i18n('Switch video block to manual'),
47 handler: videoBlock => {
48 this.videoBlocklistService.unblockVideo(videoBlock.video.id).pipe(
49 switchMap(_ => this.videoBlocklistService.blockVideo(videoBlock.video.id, undefined, true))
52 this.notifier.success(this.i18n('Video {{name}} switched to manual block.', { name: videoBlock.video.name }))
56 err => this.notifier.error(err.message)
59 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
64 label: this.i18n('Actions for the video'),
68 label: this.i18n('Unblock'),
69 handler: videoBlock => this.unblockVideo(videoBlock)
73 label: this.i18n('Delete'),
74 handler: async videoBlock => {
75 const res = await this.confirmService.confirm(
76 this.i18n('Do you really want to delete this video?'),
79 if (res === false) return
81 this.videoService.removeVideo(videoBlock.video.id)
84 this.notifier.success(this.i18n('Video deleted.'))
87 err => this.notifier.error(err.message)
96 this.serverService.getConfig()
97 .subscribe(config => {
98 // don't filter if auto-blacklist is not enabled as this will be the only list
99 if (config.autoBlacklist.videos.ofUsers.enabled) {
100 this.blocklistTypeFilter = VideoBlacklistType.MANUAL
106 this.route.queryParams
107 .pipe(filter(params => params.search !== undefined && params.search !== null))
108 .subscribe(params => {
109 this.search = params.search
110 this.setTableFilter(params.search)
116 if (this.search) this.setTableFilter(this.search)
119 /* Table filter functions */
120 onBlockSearch (event: Event) {
122 this.setQueryParams((event.target as HTMLInputElement).value)
125 setQueryParams (search: string) {
126 const queryParams: Params = {}
127 if (search) Object.assign(queryParams, { search })
128 this.router.navigate([ '/admin/moderation/video-blocks/list' ], { queryParams })
131 resetTableFilter () {
132 this.setTableFilter('')
133 this.setQueryParams('')
136 /* END Table filter functions */
139 return 'VideoBlockListComponent'
142 getVideoUrl (videoBlock: VideoBlacklist) {
143 return Video.buildClientUrl(videoBlock.video.uuid)
146 booleanToText (value: boolean) {
147 if (value === true) return this.i18n('yes')
149 return this.i18n('no')
152 toHtml (text: string) {
153 return this.markdownRenderer.textMarkdownToHTML(text)
156 async unblockVideo (entry: VideoBlacklist) {
157 const confirmMessage = this.i18n(
158 'Do you really want to unblock this video? It will be available again in the videos list.'
161 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblock'))
162 if (res === false) return
164 this.videoBlocklistService.unblockVideo(entry.video.id).subscribe(
166 this.notifier.success(this.i18n('Video {{name}} unblocked.', { name: entry.video.name }))
170 err => this.notifier.error(err.message)
174 protected loadData () {
175 this.videoBlocklistService.listBlocks({
176 pagination: this.pagination,
181 async resultList => {
182 this.totalRecords = resultList.total
184 this.blocklist = resultList.data
186 for (const element of this.blocklist) {
187 Object.assign(element, { reasonHtml: await this.toHtml(element.reason) })
191 err => this.notifier.error(err.message)