]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/moderation/video-block-list/video-block-list.component.ts
Migrate client to eslint
[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 next: () => {
67 this.notifier.success($localize`Video ${videoBlock.video.name} switched to manual block.`)
68 this.reloadData()
69 },
70
71 error: 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 next: () => this.notifier.success($localize`Video deleted.`),
99
100 error: err => this.notifier.error(err.message)
101 })
102 }
103 }
104 ]
105 ]
106 }
107
108 ngOnInit () {
109 const serverConfig = this.serverService.getHTMLConfig()
110
111 // Don't filter if auto-blacklist is not enabled as this will be the only list
112 if (serverConfig.autoBlacklist.videos.ofUsers.enabled) {
113 this.blocklistTypeFilter = VideoBlacklistType.MANUAL
114 }
115
116 this.initialize()
117 }
118
119 getIdentifier () {
120 return 'VideoBlockListComponent'
121 }
122
123 getVideoUrl (videoBlock: VideoBlacklist) {
124 return Video.buildWatchUrl(videoBlock.video)
125 }
126
127 toHtml (text: string) {
128 return this.markdownRenderer.textMarkdownToHTML(text)
129 }
130
131 async unblockVideo (entry: VideoBlacklist) {
132 const confirmMessage = $localize`Do you really want to unblock this video? It will be available again in the videos list.`
133
134 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock`)
135 if (res === false) return
136
137 this.videoBlocklistService.unblockVideo(entry.video.id)
138 .subscribe({
139 next: () => {
140 this.notifier.success($localize`Video ${entry.video.name} unblocked.`)
141 this.reloadData()
142 },
143
144 error: err => this.notifier.error(err.message)
145 })
146 }
147
148 getVideoEmbed (entry: VideoBlacklist) {
149 return buildVideoOrPlaylistEmbed(
150 decorateVideoLink({
151 url: buildVideoEmbedLink(entry.video, environment.originServerUrl),
152
153 title: false,
154 warningTitle: false
155 }),
156 entry.video.name
157 )
158 }
159
160 protected reloadData () {
161 this.videoBlocklistService.listBlocks({
162 pagination: this.pagination,
163 sort: this.sort,
164 search: this.search
165 })
166 .subscribe({
167 next: async resultList => {
168 this.totalRecords = resultList.total
169
170 this.blocklist = resultList.data
171
172 for (const element of this.blocklist) {
173 Object.assign(element, {
174 reasonHtml: await this.toHtml(element.reason),
175 embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(element))
176 })
177 }
178 },
179
180 error: err => this.notifier.error(err.message)
181 })
182 }
183 }