]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/videos/video-list.component.ts
3c21adb44c8dd8bc346aff8b4cce4cd1ec7927cc
[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 { AdvancedInputFilter } from '@app/shared/shared-forms'
7 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
8 import { VideoBlockComponent, VideoBlockService } from '@app/shared/shared-moderation'
9 import { VideoActionsDisplayType } from '@app/shared/shared-video-miniature'
10 import { UserRight, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
11 import { VideoAdminService } from './video-admin.service'
12
13 @Component({
14 selector: 'my-video-list',
15 templateUrl: './video-list.component.html',
16 styleUrls: [ './video-list.component.scss' ]
17 })
18 export class VideoListComponent extends RestTable implements OnInit {
19 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
20
21 videos: Video[] = []
22
23 totalRecords = 0
24 sort: SortMeta = { field: 'publishedAt', order: -1 }
25 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
26
27 bulkVideoActions: DropdownAction<Video[]>[][] = []
28
29 selectedVideos: 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 }
45
46 loading = true
47
48 constructor (
49 protected route: ActivatedRoute,
50 protected router: Router,
51 private confirmService: ConfirmService,
52 private auth: AuthService,
53 private notifier: Notifier,
54 private videoService: VideoService,
55 private videoAdminService: VideoAdminService,
56 private videoBlockService: VideoBlockService
57 ) {
58 super()
59 }
60
61 get authUser () {
62 return this.auth.getUser()
63 }
64
65 ngOnInit () {
66 this.initialize()
67
68 this.inputFilters = this.videoAdminService.buildAdminInputFilter()
69
70 this.bulkVideoActions = [
71 [
72 {
73 label: $localize`Delete`,
74 handler: videos => this.removeVideos(videos),
75 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO),
76 iconName: 'delete'
77 },
78 {
79 label: $localize`Block`,
80 handler: videos => this.videoBlockModal.show(videos),
81 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => !v.blacklisted),
82 iconName: 'no'
83 },
84 {
85 label: $localize`Unblock`,
86 handler: videos => this.unblockVideos(videos),
87 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => v.blacklisted),
88 iconName: 'undo'
89 }
90 ],
91 [
92 {
93 label: $localize`Delete HLS files`,
94 handler: videos => this.removeVideoFiles(videos, 'hls'),
95 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_FILES) && videos.every(v => v.hasHLS() && v.hasWebTorrent()),
96 iconName: 'delete'
97 },
98 {
99 label: $localize`Delete WebTorrent files`,
100 handler: videos => this.removeVideoFiles(videos, 'webtorrent'),
101 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_FILES) && videos.every(v => v.hasHLS() && v.hasWebTorrent()),
102 iconName: 'delete'
103 }
104 ]
105 ]
106 }
107
108 getIdentifier () {
109 return 'VideoListComponent'
110 }
111
112 isInSelectionMode () {
113 return this.selectedVideos.length !== 0
114 }
115
116 getPrivacyBadgeClass (video: Video) {
117 if (video.privacy.id === VideoPrivacy.PUBLIC) return 'badge-green'
118
119 return 'badge-yellow'
120 }
121
122 isUnpublished (video: Video) {
123 return video.state.id !== VideoState.LIVE_ENDED && video.state.id !== VideoState.PUBLISHED
124 }
125
126 isAccountBlocked (video: Video) {
127 return video.blockedOwner
128 }
129
130 isServerBlocked (video: Video) {
131 return video.blockedServer
132 }
133
134 isVideoBlocked (video: Video) {
135 return video.blacklisted
136 }
137
138 isImport (video: Video) {
139 return video.state.id === VideoState.TO_IMPORT
140 }
141
142 isHLS (video: Video) {
143 const p = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
144 if (!p) return false
145
146 return p.files.length !== 0
147 }
148
149 isWebTorrent (video: Video) {
150 return video.files.length !== 0
151 }
152
153 getFilesSize (video: Video) {
154 let files = video.files
155
156 if (this.isHLS(video)) {
157 files = files.concat(video.streamingPlaylists[0].files)
158 }
159
160 return files.reduce((p, f) => p += f.size, 0)
161 }
162
163 reloadData () {
164 this.selectedVideos = []
165
166 this.loading = true
167
168 this.videoAdminService.getAdminVideos({
169 pagination: this.pagination,
170 sort: this.sort,
171 search: this.search
172 }).pipe(finalize(() => this.loading = false))
173 .subscribe({
174 next: resultList => {
175 this.videos = resultList.data
176 this.totalRecords = resultList.total
177 },
178
179 error: err => this.notifier.error(err.message)
180 })
181 }
182
183 private async removeVideos (videos: Video[]) {
184 const message = $localize`Are you sure you want to delete these ${videos.length} videos?`
185 const res = await this.confirmService.confirm(message, $localize`Delete`)
186 if (res === false) return
187
188 this.videoService.removeVideo(videos.map(v => v.id))
189 .subscribe({
190 next: () => {
191 this.notifier.success($localize`Deleted ${videos.length} videos.`)
192 this.reloadData()
193 },
194
195 error: err => this.notifier.error(err.message)
196 })
197 }
198
199 private unblockVideos (videos: Video[]) {
200 this.videoBlockService.unblockVideo(videos.map(v => v.id))
201 .subscribe({
202 next: () => {
203 this.notifier.success($localize`Unblocked ${videos.length} videos.`)
204 this.reloadData()
205 },
206
207 error: err => this.notifier.error(err.message)
208 })
209 }
210
211 private async removeVideoFiles (videos: Video[], type: 'hls' | 'webtorrent') {
212 const message = type === 'hls'
213 ? $localize`Are you sure you want to delete ${videos.length} HLS streaming playlists?`
214 : $localize`Are you sure you want to delete WebTorrent files of ${videos.length} videos?`
215
216 const res = await this.confirmService.confirm(message, $localize`Delete`)
217 if (res === false) return
218
219 this.videoService.removeVideoFiles(videos.map(v => v.id), type)
220 .subscribe({
221 next: () => {
222 this.notifier.success($localize`Files were removed.`)
223 this.reloadData()
224 },
225
226 error: err => this.notifier.error(err.message)
227 })
228 }
229 }