]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/+admin/overview/videos/video-list.component.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / videos / video-list.component.ts
... / ...
CommitLineData
1import { SortMeta } from 'primeng/api'
2import { finalize } from 'rxjs/operators'
3import { Component, OnInit, ViewChild } from '@angular/core'
4import { ActivatedRoute, Router } from '@angular/router'
5import { AuthService, ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
6import { prepareIcu } from '@app/helpers'
7import { AdvancedInputFilter } from '@app/shared/shared-forms'
8import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9import { VideoBlockComponent, VideoBlockService } from '@app/shared/shared-moderation'
10import { VideoActionsDisplayType } from '@app/shared/shared-video-miniature'
11import { getAllFiles } from '@shared/core-utils'
12import { UserRight, VideoFile, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
13import { VideoAdminService } from './video-admin.service'
14
15@Component({
16 selector: 'my-video-list',
17 templateUrl: './video-list.component.html',
18 styleUrls: [ './video-list.component.scss' ]
19})
20export class VideoListComponent extends RestTable <Video> implements OnInit {
21 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
22
23 videos: Video[] = []
24
25 totalRecords = 0
26 sort: SortMeta = { field: 'publishedAt', order: -1 }
27 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
28
29 bulkActions: DropdownAction<Video[]>[][] = []
30
31 inputFilters: AdvancedInputFilter[]
32
33 videoActionsOptions: VideoActionsDisplayType = {
34 playlist: false,
35 download: false,
36 update: true,
37 blacklist: true,
38 delete: true,
39 report: false,
40 duplicate: true,
41 mute: true,
42 liveInfo: false,
43 removeFiles: true,
44 transcoding: true,
45 studio: true,
46 stats: true
47 }
48
49 loading = true
50
51 constructor (
52 protected route: ActivatedRoute,
53 protected router: Router,
54 private confirmService: ConfirmService,
55 private auth: AuthService,
56 private notifier: Notifier,
57 private videoService: VideoService,
58 private videoAdminService: VideoAdminService,
59 private videoBlockService: VideoBlockService
60 ) {
61 super()
62 }
63
64 get authUser () {
65 return this.auth.getUser()
66 }
67
68 ngOnInit () {
69 this.initialize()
70
71 this.inputFilters = this.videoAdminService.buildAdminInputFilter()
72
73 this.bulkActions = [
74 [
75 {
76 label: $localize`Delete`,
77 handler: videos => this.removeVideos(videos),
78 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO),
79 iconName: 'delete'
80 },
81 {
82 label: $localize`Block`,
83 handler: videos => this.videoBlockModal.show(videos),
84 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => !v.blacklisted),
85 iconName: 'no'
86 },
87 {
88 label: $localize`Unblock`,
89 handler: videos => this.unblockVideos(videos),
90 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => v.blacklisted),
91 iconName: 'undo'
92 }
93 ],
94 [
95 {
96 label: $localize`Run HLS transcoding`,
97 handler: videos => this.runTranscoding(videos, 'hls'),
98 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
99 iconName: 'cog'
100 },
101 {
102 label: $localize`Run WebTorrent transcoding`,
103 handler: videos => this.runTranscoding(videos, 'webtorrent'),
104 isDisplayed: videos => videos.every(v => v.canRunTranscoding(this.authUser)),
105 iconName: 'cog'
106 },
107 {
108 label: $localize`Delete HLS files`,
109 handler: videos => this.removeVideoFiles(videos, 'hls'),
110 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
111 iconName: 'delete'
112 },
113 {
114 label: $localize`Delete WebTorrent files`,
115 handler: videos => this.removeVideoFiles(videos, 'webtorrent'),
116 isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
117 iconName: 'delete'
118 }
119 ]
120 ]
121 }
122
123 getIdentifier () {
124 return 'VideoListComponent'
125 }
126
127 getPrivacyBadgeClass (video: Video) {
128 if (video.privacy.id === VideoPrivacy.PUBLIC) return 'badge-green'
129
130 return 'badge-yellow'
131 }
132
133 isUnpublished (video: Video) {
134 return video.state.id !== VideoState.LIVE_ENDED && video.state.id !== VideoState.PUBLISHED
135 }
136
137 isAccountBlocked (video: Video) {
138 return video.blockedOwner
139 }
140
141 isServerBlocked (video: Video) {
142 return video.blockedServer
143 }
144
145 isVideoBlocked (video: Video) {
146 return video.blacklisted
147 }
148
149 isImport (video: Video) {
150 return video.state.id === VideoState.TO_IMPORT
151 }
152
153 isHLS (video: Video) {
154 const p = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
155 if (!p) return false
156
157 return p.files.length !== 0
158 }
159
160 isWebTorrent (video: Video) {
161 return video.files.length !== 0
162 }
163
164 hasObjectStorage (video: Video) {
165 if (!video.isLocal) return false
166
167 const files = getAllFiles(video)
168
169 return files.some(f => !f.fileUrl.startsWith(window.location.origin))
170 }
171
172 canRemoveOneFile (video: Video) {
173 return video.canRemoveOneFile(this.authUser)
174 }
175
176 getFilesSize (video: Video) {
177 let files = video.files
178
179 if (this.isHLS(video)) {
180 files = files.concat(video.streamingPlaylists[0].files)
181 }
182
183 return files.reduce((p, f) => p += f.size, 0)
184 }
185
186 async removeVideoFile (video: Video, file: VideoFile, type: 'hls' | 'webtorrent') {
187 const message = $localize`Are you sure you want to delete this ${file.resolution.label} file?`
188 const res = await this.confirmService.confirm(message, $localize`Delete file`)
189 if (res === false) return
190
191 this.videoService.removeFile(video.uuid, file.id, type)
192 .subscribe({
193 next: () => {
194 this.notifier.success($localize`File removed.`)
195 this.reloadData()
196 },
197
198 error: err => this.notifier.error(err.message)
199 })
200 }
201
202 protected reloadDataInternal () {
203 this.loading = true
204
205 this.videoAdminService.getAdminVideos({
206 pagination: this.pagination,
207 sort: this.sort,
208 nsfw: 'both', // Always list NSFW video, overriding instance/user setting
209 search: this.search
210 }).pipe(finalize(() => this.loading = false))
211 .subscribe({
212 next: resultList => {
213 this.videos = resultList.data
214 this.totalRecords = resultList.total
215 },
216
217 error: err => this.notifier.error(err.message)
218 })
219 }
220
221 private async removeVideos (videos: Video[]) {
222 const message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {this video} other {these {count} videos}}?`)(
223 { count: videos.length },
224 $localize`Are you sure you want to delete these ${videos.length} videos?`
225 )
226
227 const res = await this.confirmService.confirm(message, $localize`Delete`)
228 if (res === false) return
229
230 this.videoService.removeVideo(videos.map(v => v.id))
231 .subscribe({
232 next: () => {
233 this.notifier.success(
234 prepareIcu($localize`Deleted {count, plural, =1 {1 video} other {{count} videos}}.`)(
235 { count: videos.length },
236 $localize`Deleted ${videos.length} videos.`
237 )
238 )
239
240 this.reloadData()
241 },
242
243 error: err => this.notifier.error(err.message)
244 })
245 }
246
247 private unblockVideos (videos: Video[]) {
248 this.videoBlockService.unblockVideo(videos.map(v => v.id))
249 .subscribe({
250 next: () => {
251 this.notifier.success(
252 prepareIcu($localize`Unblocked {count, plural, =1 {1 video} other {{count} videos}}.`)(
253 { count: videos.length },
254 $localize`Unblocked ${videos.length} videos.`
255 )
256 )
257
258 this.reloadData()
259 },
260
261 error: err => this.notifier.error(err.message)
262 })
263 }
264
265 private async removeVideoFiles (videos: Video[], type: 'hls' | 'webtorrent') {
266 let message: string
267
268 if (type === 'hls') {
269 // eslint-disable-next-line max-len
270 message = prepareIcu($localize`Are you sure you want to delete {count, plural, =1 {1 HLS streaming playlist} other {{count} HLS streaming playlists}}?`)(
271 { count: videos.length },
272 $localize`Are you sure you want to delete ${videos.length} HLS streaming playlists?`
273 )
274 } else {
275 // eslint-disable-next-line max-len
276 message = prepareIcu($localize`Are you sure you want to delete WebTorrent files of {count, plural, =1 {1 video} other {{count} videos}}?`)(
277 { count: videos.length },
278 $localize`Are you sure you want to delete WebTorrent files of ${videos.length} videos?`
279 )
280 }
281
282 const res = await this.confirmService.confirm(message, $localize`Delete`)
283 if (res === false) return
284
285 this.videoService.removeVideoFiles(videos.map(v => v.id), type)
286 .subscribe({
287 next: () => {
288 this.notifier.success($localize`Files were removed.`)
289 this.reloadData()
290 },
291
292 error: err => this.notifier.error(err.message)
293 })
294 }
295
296 private runTranscoding (videos: Video[], type: 'hls' | 'webtorrent') {
297 this.videoService.runTranscoding(videos.map(v => v.id), type)
298 .subscribe({
299 next: () => {
300 this.notifier.success($localize`Transcoding jobs created.`)
301
302 this.reloadData()
303 },
304
305 error: err => this.notifier.error(err.message)
306 })
307 }
308}