]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/moderation/video-block-list/video-block-list.component.ts
Fix admin table filters
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / moderation / video-block-list / video-block-list.component.ts
CommitLineData
5baee5fc 1import { SortMeta } from 'primeng/api'
5ed46c1b 2import { switchMap } from 'rxjs/operators'
66357162
C
3import { buildVideoLink, buildVideoOrPlaylistEmbed } from 'src/assets/player/utils'
4import { environment } from 'src/environments/environment'
67ed6552 5import { AfterViewInit, Component, OnInit } from '@angular/core'
66357162 6import { DomSanitizer } from '@angular/platform-browser'
67ed6552
C
7import { ActivatedRoute, Params, Router } from '@angular/router'
8import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
9import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
10import { VideoBlockService } from '@app/shared/shared-moderation'
67ed6552 11import { VideoBlacklist, VideoBlacklistType } from '@shared/models'
5baee5fc
RK
12
13@Component({
14 selector: 'my-video-block-list',
15 templateUrl: './video-block-list.component.html',
4504f09f 16 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-block-list.component.scss' ]
5baee5fc 17})
8b381422 18export class VideoBlockListComponent extends RestTable implements OnInit, AfterViewInit {
71ab65d0 19 blocklist: (VideoBlacklist & { reasonHtml?: string, embedHtml?: string })[] = []
5baee5fc
RK
20 totalRecords = 0
21 sort: SortMeta = { field: 'createdAt', order: -1 }
22 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
3487330d 23 blocklistTypeFilter: VideoBlacklistType = undefined
5baee5fc 24
3487330d 25 videoBlocklistActions: DropdownAction<VideoBlacklist>[][] = []
5baee5fc
RK
26
27 constructor (
5ed46c1b
C
28 protected route: ActivatedRoute,
29 protected router: Router,
5baee5fc
RK
30 private notifier: Notifier,
31 private serverService: ServerService,
32 private confirmService: ConfirmService,
33 private videoBlocklistService: VideoBlockService,
34 private markdownRenderer: MarkdownService,
71ab65d0 35 private sanitizer: DomSanitizer,
5ed46c1b
C
36 private videoService: VideoService
37 ) {
5baee5fc
RK
38 super()
39
40 this.videoBlocklistActions = [
41 [
42 {
66357162 43 label: $localize`Internal actions`,
4ee63ec6
RK
44 isHeader: true,
45 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
5baee5fc
RK
46 },
47 {
66357162 48 label: $localize`Switch video block to manual`,
5baee5fc
RK
49 handler: videoBlock => {
50 this.videoBlocklistService.unblockVideo(videoBlock.video.id).pipe(
51 switchMap(_ => this.videoBlocklistService.blockVideo(videoBlock.video.id, undefined, true))
52 ).subscribe(
53 () => {
66357162 54 this.notifier.success($localize`Video ${videoBlock.video.name} switched to manual block.`)
5baee5fc
RK
55 this.loadData()
56 },
8b381422 57
5baee5fc
RK
58 err => this.notifier.error(err.message)
59 )
4ee63ec6
RK
60 },
61 isDisplayed: videoBlock => videoBlock.type === VideoBlacklistType.AUTO_BEFORE_PUBLISHED
5baee5fc
RK
62 }
63 ],
64 [
65 {
66357162 66 label: $localize`Actions for the video`,
8b381422 67 isHeader: true
5baee5fc
RK
68 },
69 {
66357162 70 label: $localize`Unblock`,
5baee5fc
RK
71 handler: videoBlock => this.unblockVideo(videoBlock)
72 },
73
74 {
66357162 75 label: $localize`Delete`,
5baee5fc
RK
76 handler: async videoBlock => {
77 const res = await this.confirmService.confirm(
66357162
C
78 $localize`Do you really want to delete this video?`,
79 $localize`Delete`
5baee5fc
RK
80 )
81 if (res === false) return
82
83 this.videoService.removeVideo(videoBlock.video.id)
84 .subscribe(
85 () => {
66357162 86 this.notifier.success($localize`Video deleted.`)
5baee5fc
RK
87 },
88
89 err => this.notifier.error(err.message)
90 )
91 }
92 }
93 ]
94 ]
95 }
96
97 ngOnInit () {
98 this.serverService.getConfig()
99 .subscribe(config => {
100 // don't filter if auto-blacklist is not enabled as this will be the only list
101 if (config.autoBlacklist.videos.ofUsers.enabled) {
3487330d 102 this.blocklistTypeFilter = VideoBlacklistType.MANUAL
5baee5fc
RK
103 }
104 })
105
106 this.initialize()
5ed46c1b 107 this.listenToSearchChange()
5baee5fc
RK
108 }
109
110 ngAfterViewInit () {
3f6441e0 111 if (this.search) this.setTableFilter(this.search, false)
5baee5fc
RK
112 }
113
114 /* Table filter functions */
115 onBlockSearch (event: Event) {
116 this.onSearch(event)
117 this.setQueryParams((event.target as HTMLInputElement).value)
118 }
119
120 setQueryParams (search: string) {
121 const queryParams: Params = {}
122 if (search) Object.assign(queryParams, { search })
123 this.router.navigate([ '/admin/moderation/video-blocks/list' ], { queryParams })
124 }
125
126 resetTableFilter () {
127 this.setTableFilter('')
128 this.setQueryParams('')
129 this.resetSearch()
130 }
131 /* END Table filter functions */
132
133 getIdentifier () {
134 return 'VideoBlockListComponent'
135 }
136
3487330d 137 getVideoUrl (videoBlock: VideoBlacklist) {
5baee5fc
RK
138 return Video.buildClientUrl(videoBlock.video.uuid)
139 }
140
5baee5fc
RK
141 toHtml (text: string) {
142 return this.markdownRenderer.textMarkdownToHTML(text)
143 }
144
3487330d 145 async unblockVideo (entry: VideoBlacklist) {
66357162 146 const confirmMessage = $localize`Do you really want to unblock this video? It will be available again in the videos list.`
5baee5fc 147
66357162 148 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock`)
5baee5fc
RK
149 if (res === false) return
150
151 this.videoBlocklistService.unblockVideo(entry.video.id).subscribe(
152 () => {
66357162 153 this.notifier.success($localize`Video ${entry.video.name} unblocked.`)
5baee5fc
RK
154 this.loadData()
155 },
156
157 err => this.notifier.error(err.message)
158 )
159 }
160
71ab65d0 161 getVideoEmbed (entry: VideoBlacklist) {
951b582f 162 return buildVideoOrPlaylistEmbed(
71ab65d0 163 buildVideoLink({
5beb89f2 164 baseUrl: `${environment.originServerUrl}/videos/embed/${entry.video.uuid}`,
71ab65d0
RK
165 title: false,
166 warningTitle: false
167 })
168 )
169 }
170
5baee5fc
RK
171 protected loadData () {
172 this.videoBlocklistService.listBlocks({
173 pagination: this.pagination,
174 sort: this.sort,
8b381422 175 search: this.search
5baee5fc
RK
176 })
177 .subscribe(
178 async resultList => {
179 this.totalRecords = resultList.total
180
181 this.blocklist = resultList.data
182
183 for (const element of this.blocklist) {
71ab65d0
RK
184 Object.assign(element, {
185 reasonHtml: await this.toHtml(element.reason),
186 embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(element))
187 })
5baee5fc
RK
188 }
189 },
190
191 err => this.notifier.error(err.message)
192 )
193 }
194}