5443d816d8bda9d851864565012c5e4e3533eff0
[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/components/common/sortmeta'
3 import { Notifier } from '@app/core'
4 import { ConfirmService } from '../../../core'
5 import { RestPagination, RestTable, VideoBlacklistService } from '../../../shared'
6 import { VideoBlacklist } 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   rowsPerPage = 10
21   sort: SortMeta = { field: 'createdAt', order: 1 }
22   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23
24   videoBlacklistActions: DropdownAction<VideoBlacklist>[] = []
25
26   constructor (
27     private notifier: Notifier,
28     private confirmService: ConfirmService,
29     private videoBlacklistService: VideoBlacklistService,
30     private markdownRenderer: MarkdownService,
31     private i18n: I18n
32   ) {
33     super()
34
35     this.videoBlacklistActions = [
36       {
37         label: this.i18n('Unblacklist'),
38         handler: videoBlacklist => this.removeVideoFromBlacklist(videoBlacklist)
39       }
40     ]
41   }
42
43   ngOnInit () {
44     this.initialize()
45   }
46
47   getVideoUrl (videoBlacklist: VideoBlacklist) {
48     return Video.buildClientUrl(videoBlacklist.video.uuid)
49   }
50
51   booleanToText (value: boolean) {
52     if (value === true) return this.i18n('yes')
53
54     return this.i18n('no')
55   }
56
57   toHtml (text: string) {
58     return this.markdownRenderer.textMarkdownToHTML(text)
59   }
60
61   async removeVideoFromBlacklist (entry: VideoBlacklist) {
62     const confirmMessage = this.i18n(
63       'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
64     )
65
66     const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
67     if (res === false) return
68
69     this.videoBlacklistService.removeVideoFromBlacklist(entry.video.id).subscribe(
70       () => {
71         this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: entry.video.name }))
72         this.loadData()
73       },
74
75       err => this.notifier.error(err.message)
76     )
77   }
78
79   protected loadData () {
80     this.videoBlacklistService.listBlacklist(this.pagination, this.sort)
81       .subscribe(
82         async resultList => {
83           this.totalRecords = resultList.total
84
85           this.blacklist = resultList.data
86
87           for (const element of this.blacklist) {
88             Object.assign(element, { reasonHtml: await this.toHtml(element.reason) })
89           }
90         },
91
92         err => this.notifier.error(err.message)
93       )
94   }
95 }