63ecdeb9ff84d30b7eed1c697c8ced73bf347b68
[oweals/peertube.git] / client / src / app / +admin / moderation / video-blacklist-list / video-blacklist-list.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { SortMeta } from 'primeng/api'
3 import { Notifier, ServerService } from '@app/core'
4 import { ConfirmService } from '../../../core'
5 import { RestPagination, RestTable, VideoBlacklistService } from '../../../shared'
6 import { VideoBlacklist, VideoBlacklistType } from '../../../../../../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { DropdownAction } from '../../../shared/buttons/action-dropdown.component'
9 import { Video } from '../../../shared/video/video.model'
10 import { MarkdownService } from '@app/shared/renderer'
11
12 @Component({
13   selector: 'my-video-blacklist-list',
14   templateUrl: './video-blacklist-list.component.html',
15   styleUrls: [ '../moderation.component.scss' ]
16 })
17 export class VideoBlacklistListComponent extends RestTable implements OnInit {
18   blacklist: (VideoBlacklist & { reasonHtml?: string })[] = []
19   totalRecords = 0
20   sort: SortMeta = { field: 'createdAt', order: -1 }
21   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
22   listBlacklistTypeFilter: VideoBlacklistType = undefined
23
24   videoBlacklistActions: DropdownAction<VideoBlacklist>[] = []
25
26   constructor (
27     private notifier: Notifier,
28     private serverService: ServerService,
29     private confirmService: ConfirmService,
30     private videoBlacklistService: VideoBlacklistService,
31     private markdownRenderer: MarkdownService,
32     private i18n: I18n
33   ) {
34     super()
35   }
36
37   ngOnInit () {
38     this.serverService.getConfig()
39         .subscribe(config => {
40           // don't filter if auto-blacklist is not enabled as this will be the only list
41           if (config.autoBlacklist.videos.ofUsers.enabled) {
42             this.listBlacklistTypeFilter = VideoBlacklistType.MANUAL
43           }
44         })
45
46     this.initialize()
47
48     this.videoBlacklistActions = [
49       {
50         label: this.i18n('Unblacklist'),
51         handler: videoBlacklist => this.removeVideoFromBlacklist(videoBlacklist)
52       }
53     ]
54   }
55
56   getIdentifier () {
57     return 'VideoBlacklistListComponent'
58   }
59
60   getVideoUrl (videoBlacklist: VideoBlacklist) {
61     return Video.buildClientUrl(videoBlacklist.video.uuid)
62   }
63
64   booleanToText (value: boolean) {
65     if (value === true) return this.i18n('yes')
66
67     return this.i18n('no')
68   }
69
70   toHtml (text: string) {
71     return this.markdownRenderer.textMarkdownToHTML(text)
72   }
73
74   async removeVideoFromBlacklist (entry: VideoBlacklist) {
75     const confirmMessage = this.i18n(
76       'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
77     )
78
79     const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
80     if (res === false) return
81
82     this.videoBlacklistService.removeVideoFromBlacklist(entry.video.id).subscribe(
83       () => {
84         this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: entry.video.name }))
85         this.loadData()
86       },
87
88       err => this.notifier.error(err.message)
89     )
90   }
91
92   protected loadData () {
93     this.videoBlacklistService.listBlacklist({
94       pagination: this.pagination,
95       sort: this.sort,
96       search: this.search,
97       type: this.listBlacklistTypeFilter
98     })
99       .subscribe(
100         async resultList => {
101           this.totalRecords = resultList.total
102
103           this.blacklist = resultList.data
104
105           for (const element of this.blacklist) {
106             Object.assign(element, { reasonHtml: await this.toHtml(element.reason) })
107           }
108         },
109
110         err => this.notifier.error(err.message)
111       )
112   }
113 }