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