]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.ts
Add ability to remove hls/webtorrent files
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-actions-dropdown.component.ts
... / ...
CommitLineData
1import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2import { AuthService, ConfirmService, Notifier, ScreenService } from '@app/core'
3import { BlocklistService, VideoBlockComponent, VideoBlockService, VideoReportComponent } from '@app/shared/shared-moderation'
4import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
5import { UserRight, VideoCaption } from '@shared/models'
6import {
7 Actor,
8 DropdownAction,
9 DropdownButtonSize,
10 DropdownDirection,
11 RedundancyService,
12 Video,
13 VideoDetails,
14 VideoService
15} from '../shared-main'
16import { LiveStreamInformationComponent } from '../shared-video-live'
17import { VideoAddToPlaylistComponent } from '../shared-video-playlist'
18import { VideoDownloadComponent } from './video-download.component'
19
20export type VideoActionsDisplayType = {
21 playlist?: boolean
22 download?: boolean
23 update?: boolean
24 blacklist?: boolean
25 delete?: boolean
26 report?: boolean
27 duplicate?: boolean
28 mute?: boolean
29 liveInfo?: boolean
30 removeFiles?: boolean
31}
32
33@Component({
34 selector: 'my-video-actions-dropdown',
35 templateUrl: './video-actions-dropdown.component.html',
36 styleUrls: [ './video-actions-dropdown.component.scss' ]
37})
38export class VideoActionsDropdownComponent implements OnChanges {
39 @ViewChild('playlistDropdown') playlistDropdown: NgbDropdown
40 @ViewChild('playlistAdd') playlistAdd: VideoAddToPlaylistComponent
41
42 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
43 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
44 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
45 @ViewChild('liveStreamInformationModal') liveStreamInformationModal: LiveStreamInformationComponent
46
47 @Input() video: Video | VideoDetails
48 @Input() videoCaptions: VideoCaption[] = []
49
50 @Input() displayOptions: VideoActionsDisplayType = {
51 playlist: false,
52 download: true,
53 update: true,
54 blacklist: true,
55 delete: true,
56 report: true,
57 duplicate: true,
58 mute: true,
59 liveInfo: false
60 }
61 @Input() placement = 'left'
62
63 @Input() label: string
64
65 @Input() buttonStyled = false
66 @Input() buttonSize: DropdownButtonSize = 'normal'
67 @Input() buttonDirection: DropdownDirection = 'vertical'
68
69 @Output() videoFilesRemoved = new EventEmitter()
70 @Output() videoRemoved = new EventEmitter()
71 @Output() videoUnblocked = new EventEmitter()
72 @Output() videoBlocked = new EventEmitter()
73 @Output() videoAccountMuted = new EventEmitter()
74 @Output() modalOpened = new EventEmitter()
75
76 videoActions: DropdownAction<{ video: Video }>[][] = []
77
78 private loaded = false
79
80 constructor (
81 private authService: AuthService,
82 private notifier: Notifier,
83 private confirmService: ConfirmService,
84 private blocklistService: BlocklistService,
85 private videoBlocklistService: VideoBlockService,
86 private screenService: ScreenService,
87 private videoService: VideoService,
88 private redundancyService: RedundancyService
89 ) { }
90
91 get user () {
92 return this.authService.getUser()
93 }
94
95 ngOnChanges () {
96 if (this.loaded) {
97 this.loaded = false
98 if (this.playlistAdd) this.playlistAdd.reload()
99 }
100
101 this.buildActions()
102 }
103
104 isUserLoggedIn () {
105 return this.authService.isLoggedIn()
106 }
107
108 loadDropdownInformation () {
109 if (!this.isUserLoggedIn() || this.loaded === true) return
110
111 this.loaded = true
112
113 if (this.displayOptions.playlist) this.playlistAdd.load()
114 }
115
116 /* Show modals */
117
118 showDownloadModal () {
119 this.modalOpened.emit()
120
121 this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions)
122 }
123
124 showReportModal () {
125 this.modalOpened.emit()
126
127 this.videoReportModal.show()
128 }
129
130 showBlockModal () {
131 this.modalOpened.emit()
132
133 this.videoBlockModal.show([ this.video ])
134 }
135
136 showLiveInfoModal (video: Video) {
137 this.modalOpened.emit()
138
139 this.liveStreamInformationModal.show(video)
140 }
141
142 /* Actions checker */
143
144 isVideoUpdatable () {
145 return this.video.isUpdatableBy(this.user)
146 }
147
148 isVideoRemovable () {
149 return this.video.isRemovableBy(this.user)
150 }
151
152 isVideoBlockable () {
153 return this.video.isBlockableBy(this.user)
154 }
155
156 isVideoUnblockable () {
157 return this.video.isUnblockableBy(this.user)
158 }
159
160 isVideoLiveInfoAvailable () {
161 return this.video.isLiveInfoAvailableBy(this.user)
162 }
163
164 isVideoDownloadable () {
165 return this.video &&
166 this.video.isLive !== true &&
167 this.video instanceof VideoDetails &&
168 this.video.downloadEnabled
169 }
170
171 canVideoBeDuplicated () {
172 return !this.video.isLive && this.video.canBeDuplicatedBy(this.user)
173 }
174
175 isVideoAccountMutable () {
176 return this.video.account.id !== this.user.account.id
177 }
178
179 canRemoveVideoFiles () {
180 return this.user.hasRight(UserRight.MANAGE_VIDEO_FILES) && this.video.hasHLS() && this.video.hasWebTorrent()
181 }
182
183 /* Action handlers */
184
185 async unblockVideo () {
186 const confirmMessage = $localize`Do you really want to unblock ${this.video.name}? It will be available again in the videos list.`
187
188 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock ${this.video.name}`)
189 if (res === false) return
190
191 this.videoBlocklistService.unblockVideo(this.video.id)
192 .subscribe({
193 next: () => {
194 this.notifier.success($localize`Video ${this.video.name} unblocked.`)
195
196 this.video.blacklisted = false
197 this.video.blacklistedReason = null
198
199 this.videoUnblocked.emit()
200 },
201
202 error: err => this.notifier.error(err.message)
203 })
204 }
205
206 async removeVideo () {
207 this.modalOpened.emit()
208
209 let message = $localize`Do you really want to delete ${this.video.name}?`
210 if (this.video.isLive) {
211 message += ' ' + $localize`The live stream will be automatically terminated.`
212 }
213
214 const res = await this.confirmService.confirm(message, $localize`Delete ${this.video.name}`)
215 if (res === false) return
216
217 this.videoService.removeVideo(this.video.id)
218 .subscribe({
219 next: () => {
220 this.notifier.success($localize`Video ${this.video.name} deleted.`)
221 this.videoRemoved.emit()
222 },
223
224 error: err => this.notifier.error(err.message)
225 })
226 }
227
228 duplicateVideo () {
229 this.redundancyService.addVideoRedundancy(this.video)
230 .subscribe({
231 next: () => {
232 const message = $localize`${this.video.name} will be duplicated by your instance.`
233 this.notifier.success(message)
234 },
235
236 error: err => this.notifier.error(err.message)
237 })
238 }
239
240 muteVideoAccount () {
241 const params = { nameWithHost: Actor.CREATE_BY_STRING(this.video.account.name, this.video.account.host) }
242
243 this.blocklistService.blockAccountByUser(params)
244 .subscribe({
245 next: () => {
246 this.notifier.success($localize`Account ${params.nameWithHost} muted.`)
247 this.videoAccountMuted.emit()
248 },
249
250 error: err => this.notifier.error(err.message)
251 })
252 }
253
254 async removeVideoFiles (video: Video, type: 'hls' | 'webtorrent') {
255 const confirmMessage = $localize`Do you really want to remove "${this.video.name}" files?`
256
257 const res = await this.confirmService.confirm(confirmMessage, $localize`Remove "${this.video.name}" files`)
258 if (res === false) return
259
260 this.videoService.removeVideoFiles([ video.id ], type)
261 .subscribe({
262 next: () => {
263 this.notifier.success($localize`Removed files of ${video.name}.`)
264 this.videoFilesRemoved.emit()
265 },
266
267 error: err => this.notifier.error(err.message)
268 })
269 }
270
271 onVideoBlocked () {
272 this.videoBlocked.emit()
273 }
274
275 getPlaylistDropdownPlacement () {
276 if (this.screenService.isInSmallView()) {
277 return 'bottom-right'
278 }
279
280 return 'bottom-left bottom-right'
281 }
282
283 private buildActions () {
284 this.videoActions = [
285 [
286 {
287 label: $localize`Save to playlist`,
288 handler: () => this.playlistDropdown.toggle(),
289 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
290 iconName: 'playlist-add'
291 }
292 ],
293 [ // actions regarding the video
294 {
295 label: $localize`Download`,
296 handler: () => this.showDownloadModal(),
297 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
298 iconName: 'download'
299 },
300 {
301 label: $localize`Display live information`,
302 handler: ({ video }) => this.showLiveInfoModal(video),
303 isDisplayed: () => this.displayOptions.liveInfo && this.isVideoLiveInfoAvailable(),
304 iconName: 'live'
305 },
306 {
307 label: $localize`Update`,
308 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
309 iconName: 'edit',
310 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
311 },
312 {
313 label: $localize`Block`,
314 handler: () => this.showBlockModal(),
315 iconName: 'no',
316 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlockable()
317 },
318 {
319 label: $localize`Unblock`,
320 handler: () => this.unblockVideo(),
321 iconName: 'undo',
322 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblockable()
323 },
324 {
325 label: $localize`Mirror`,
326 handler: () => this.duplicateVideo(),
327 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
328 iconName: 'cloud-download'
329 },
330 {
331 label: $localize`Delete`,
332 handler: () => this.removeVideo(),
333 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
334 iconName: 'delete'
335 },
336 {
337 label: $localize`Report`,
338 handler: () => this.showReportModal(),
339 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
340 iconName: 'flag'
341 }
342 ],
343 [
344 {
345 label: $localize`Delete HLS files`,
346 handler: ({ video }) => this.removeVideoFiles(video, 'hls'),
347 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
348 iconName: 'delete'
349 },
350 {
351 label: $localize`Delete WebTorrent files`,
352 handler: ({ video }) => this.removeVideoFiles(video, 'webtorrent'),
353 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
354 iconName: 'delete'
355 }
356 ],
357 [ // actions regarding the account/its server
358 {
359 label: $localize`Mute account`,
360 handler: () => this.muteVideoAccount(),
361 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.mute && this.isVideoAccountMutable(),
362 iconName: 'no'
363 }
364 ]
365 ]
366 }
367}