Fix sort in admin tables
[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   rowsPerPage = 10
21   sort: SortMeta = { field: 'createdAt', order: 1 }
22   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23   listBlacklistTypeFilter: VideoBlacklistType = undefined
24
25   videoBlacklistActions: DropdownAction<VideoBlacklist>[] = []
26
27   constructor (
28     private notifier: Notifier,
29     private serverService: ServerService,
30     private confirmService: ConfirmService,
31     private videoBlacklistService: VideoBlacklistService,
32     private markdownRenderer: MarkdownService,
33     private i18n: I18n
34   ) {
35     super()
36   }
37
38   ngOnInit () {
39     this.serverService.getConfig()
40         .subscribe(config => {
41           // don't filter if auto-blacklist not enabled as this will be only list
42           if (config.autoBlacklist.videos.ofUsers.enabled) {
43             this.listBlacklistTypeFilter = VideoBlacklistType.MANUAL
44           }
45         })
46
47     this.initialize()
48
49     this.videoBlacklistActions = [
50       {
51         label: this.i18n('Unblacklist'),
52         handler: videoBlacklist => this.removeVideoFromBlacklist(videoBlacklist)
53       }
54     ]
55   }
56
57   getIdentifier () {
58     return 'VideoBlacklistListComponent'
59   }
60
61   getVideoUrl (videoBlacklist: VideoBlacklist) {
62     return Video.buildClientUrl(videoBlacklist.video.uuid)
63   }
64
65   booleanToText (value: boolean) {
66     if (value === true) return this.i18n('yes')
67
68     return this.i18n('no')
69   }
70
71   toHtml (text: string) {
72     return this.markdownRenderer.textMarkdownToHTML(text)
73   }
74
75   async removeVideoFromBlacklist (entry: VideoBlacklist) {
76     const confirmMessage = this.i18n(
77       'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
78     )
79
80     const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
81     if (res === false) return
82
83     this.videoBlacklistService.removeVideoFromBlacklist(entry.video.id).subscribe(
84       () => {
85         this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: entry.video.name }))
86         this.loadData()
87       },
88
89       err => this.notifier.error(err.message)
90     )
91   }
92
93   protected loadData () {
94     this.videoBlacklistService.listBlacklist(this.pagination, this.sort, this.listBlacklistTypeFilter)
95       .subscribe(
96         async resultList => {
97           this.totalRecords = resultList.total
98
99           this.blacklist = resultList.data
100
101           for (const element of this.blacklist) {
102             Object.assign(element, { reasonHtml: await this.toHtml(element.reason) })
103           }
104         },
105
106         err => this.notifier.error(err.message)
107       )
108   }
109 }