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