]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/videos/video-list.component.ts
Add ability to bulk block videos
[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 }
44
45 loading = true
46
47 constructor (
48 protected route: ActivatedRoute,
49 protected router: Router,
50 private confirmService: ConfirmService,
51 private auth: AuthService,
52 private notifier: Notifier,
53 private videoService: VideoService,
54 private videoAdminService: VideoAdminService,
55 private videoBlockService: VideoBlockService
56 ) {
57 super()
58 }
59
60 get authUser () {
61 return this.auth.getUser()
62 }
63
64 ngOnInit () {
65 this.initialize()
66
67 this.inputFilters = this.videoAdminService.buildAdminInputFilter()
68
69 this.bulkVideoActions = [
70 [
71 {
72 label: $localize`Delete`,
73 handler: videos => this.removeVideos(videos),
74 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO)
75 },
76 {
77 label: $localize`Block`,
78 handler: videos => this.videoBlockModal.show(videos),
79 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => !v.blacklisted)
80 },
81 {
82 label: $localize`Unblock`,
83 handler: videos => this.unblockVideos(videos),
84 isDisplayed: videos => this.authUser.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) && videos.every(v => v.blacklisted)
85 }
86 ]
87 ]
88 }
89
90 getIdentifier () {
91 return 'VideoListComponent'
92 }
93
94 isInSelectionMode () {
95 return this.selectedVideos.length !== 0
96 }
97
98 onVideoRemoved () {
99 this.reloadData()
100 }
101
102 getPrivacyBadgeClass (video: Video) {
103 if (video.privacy.id === VideoPrivacy.PUBLIC) return 'badge-green'
104
105 return 'badge-yellow'
106 }
107
108 isUnpublished (video: Video) {
109 return video.state.id !== VideoState.LIVE_ENDED && video.state.id !== VideoState.PUBLISHED
110 }
111
112 isAccountBlocked (video: Video) {
113 return video.blockedOwner
114 }
115
116 isServerBlocked (video: Video) {
117 return video.blockedServer
118 }
119
120 isVideoBlocked (video: Video) {
121 return video.blacklisted
122 }
123
124 isImport (video: Video) {
125 return video.state.id === VideoState.TO_IMPORT
126 }
127
128 isHLS (video: Video) {
129 const p = video.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
130 if (!p) return false
131
132 return p.files.length !== 0
133 }
134
135 isWebTorrent (video: Video) {
136 return video.files.length !== 0
137 }
138
139 getFilesSize (video: Video) {
140 let files = video.files
141
142 if (this.isHLS(video)) {
143 files = files.concat(video.streamingPlaylists[0].files)
144 }
145
146 return files.reduce((p, f) => p += f.size, 0)
147 }
148
149 onVideoBlocked () {
150 this.reloadData()
151 }
152
153 protected reloadData () {
154 this.selectedVideos = []
155
156 this.loading = true
157
158 this.videoAdminService.getAdminVideos({
159 pagination: this.pagination,
160 sort: this.sort,
161 search: this.search
162 }).pipe(finalize(() => this.loading = false))
163 .subscribe({
164 next: resultList => {
165 this.videos = resultList.data
166 this.totalRecords = resultList.total
167 },
168
169 error: err => this.notifier.error(err.message)
170 })
171 }
172
173 private async removeVideos (videos: Video[]) {
174 const message = $localize`Are you sure you want to delete these ${videos.length} videos?`
175 const res = await this.confirmService.confirm(message, $localize`Delete`)
176 if (res === false) return
177
178 this.videoService.removeVideo(videos.map(v => v.id))
179 .subscribe({
180 next: () => {
181 this.notifier.success($localize`Deleted ${videos.length} videos.`)
182 this.reloadData()
183 },
184
185 error: err => this.notifier.error(err.message)
186 })
187 }
188
189 private unblockVideos (videos: Video[]) {
190 this.videoBlockService.unblockVideo(videos.map(v => v.id))
191 .subscribe({
192 next: () => {
193 this.notifier.success($localize`Unblocked ${videos.length} videos.`)
194 this.reloadData()
195 },
196
197 error: err => this.notifier.error(err.message)
198 })
199 }
200 }