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