1 import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { AuthService, ConfirmService, Notifier, ScreenService } from '@app/core'
3 import { BlocklistService, VideoBlockComponent, VideoBlockService, VideoReportComponent } from '@app/shared/shared-moderation'
4 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
5 import { UserRight, VideoCaption, VideoState } from '@shared/models'
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'
20 export type VideoActionsDisplayType = {
35 selector: 'my-video-actions-dropdown',
36 templateUrl: './video-actions-dropdown.component.html',
37 styleUrls: [ './video-actions-dropdown.component.scss' ]
39 export class VideoActionsDropdownComponent implements OnChanges {
40 @ViewChild('playlistDropdown') playlistDropdown: NgbDropdown
41 @ViewChild('playlistAdd') playlistAdd: VideoAddToPlaylistComponent
43 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
44 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
45 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
46 @ViewChild('liveStreamInformationModal') liveStreamInformationModal: LiveStreamInformationComponent
48 @Input() video: Video | VideoDetails
49 @Input() videoCaptions: VideoCaption[] = []
51 @Input() displayOptions: VideoActionsDisplayType = {
64 @Input() placement = 'left'
66 @Input() label: string
68 @Input() buttonStyled = false
69 @Input() buttonSize: DropdownButtonSize = 'normal'
70 @Input() buttonDirection: DropdownDirection = 'vertical'
72 @Output() videoFilesRemoved = new EventEmitter()
73 @Output() videoRemoved = new EventEmitter()
74 @Output() videoUnblocked = new EventEmitter()
75 @Output() videoBlocked = new EventEmitter()
76 @Output() videoAccountMuted = new EventEmitter()
77 @Output() transcodingCreated = new EventEmitter()
78 @Output() modalOpened = new EventEmitter()
80 videoActions: DropdownAction<{ video: Video }>[][] = []
82 private loaded = false
85 private authService: AuthService,
86 private notifier: Notifier,
87 private confirmService: ConfirmService,
88 private blocklistService: BlocklistService,
89 private videoBlocklistService: VideoBlockService,
90 private screenService: ScreenService,
91 private videoService: VideoService,
92 private redundancyService: RedundancyService
96 return this.authService.getUser()
102 if (this.playlistAdd) this.playlistAdd.reload()
109 return this.authService.isLoggedIn()
112 loadDropdownInformation () {
113 if (!this.isUserLoggedIn() || this.loaded === true) return
117 if (this.displayOptions.playlist) this.playlistAdd.load()
122 showDownloadModal () {
123 this.modalOpened.emit()
125 this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions)
129 this.modalOpened.emit()
131 this.videoReportModal.show()
135 this.modalOpened.emit()
137 this.videoBlockModal.show([ this.video ])
140 showLiveInfoModal (video: Video) {
141 this.modalOpened.emit()
143 this.liveStreamInformationModal.show(video)
146 /* Actions checker */
148 isVideoUpdatable () {
149 return this.video.isUpdatableBy(this.user)
152 isVideoRemovable () {
153 return this.video.isRemovableBy(this.user)
156 isVideoBlockable () {
157 return this.video.isBlockableBy(this.user)
160 isVideoUnblockable () {
161 return this.video.isUnblockableBy(this.user)
164 isVideoLiveInfoAvailable () {
165 return this.video.isLiveInfoAvailableBy(this.user)
168 isVideoDownloadable () {
170 this.video.isLive !== true &&
171 this.video instanceof VideoDetails &&
172 this.video.downloadEnabled
175 canVideoBeDuplicated () {
176 return !this.video.isLive && this.video.canBeDuplicatedBy(this.user)
179 isVideoAccountMutable () {
180 return this.video.account.id !== this.user.account.id
183 canRemoveVideoFiles () {
184 return this.video.canRemoveFiles(this.user)
187 canRunTranscoding () {
188 return this.video.canRunTranscoding(this.user)
191 /* Action handlers */
193 async unblockVideo () {
194 const confirmMessage = $localize`Do you really want to unblock ${this.video.name}? It will be available again in the videos list.`
196 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock ${this.video.name}`)
197 if (res === false) return
199 this.videoBlocklistService.unblockVideo(this.video.id)
202 this.notifier.success($localize`Video ${this.video.name} unblocked.`)
204 this.video.blacklisted = false
205 this.video.blacklistedReason = null
207 this.videoUnblocked.emit()
210 error: err => this.notifier.error(err.message)
214 async removeVideo () {
215 this.modalOpened.emit()
217 let message = $localize`Do you really want to delete ${this.video.name}?`
218 if (this.video.isLive) {
219 message += ' ' + $localize`The live stream will be automatically terminated.`
222 const res = await this.confirmService.confirm(message, $localize`Delete ${this.video.name}`)
223 if (res === false) return
225 this.videoService.removeVideo(this.video.id)
228 this.notifier.success($localize`Video ${this.video.name} deleted.`)
229 this.videoRemoved.emit()
232 error: err => this.notifier.error(err.message)
237 this.redundancyService.addVideoRedundancy(this.video)
240 const message = $localize`${this.video.name} will be duplicated by your instance.`
241 this.notifier.success(message)
244 error: err => this.notifier.error(err.message)
248 muteVideoAccount () {
249 const params = { nameWithHost: Actor.CREATE_BY_STRING(this.video.account.name, this.video.account.host) }
251 this.blocklistService.blockAccountByUser(params)
254 this.notifier.success($localize`Account ${params.nameWithHost} muted.`)
255 this.videoAccountMuted.emit()
258 error: err => this.notifier.error(err.message)
262 async removeVideoFiles (video: Video, type: 'hls' | 'webtorrent') {
263 const confirmMessage = $localize`Do you really want to remove "${this.video.name}" files?`
265 const res = await this.confirmService.confirm(confirmMessage, $localize`Remove "${this.video.name}" files`)
266 if (res === false) return
268 this.videoService.removeVideoFiles([ video.id ], type)
271 this.notifier.success($localize`Removed files of ${video.name}.`)
272 this.videoFilesRemoved.emit()
275 error: err => this.notifier.error(err.message)
279 runTranscoding (video: Video, type: 'hls' | 'webtorrent') {
280 this.videoService.runTranscoding([ video.id ], type)
283 this.notifier.success($localize`Transcoding jobs created for ${video.name}.`)
284 this.transcodingCreated.emit()
287 error: err => this.notifier.error(err.message)
292 this.videoBlocked.emit()
295 getPlaylistDropdownPlacement () {
296 if (this.screenService.isInSmallView()) {
297 return 'bottom-right'
300 return 'bottom-left bottom-right'
303 private buildActions () {
304 this.videoActions = [
307 label: $localize`Save to playlist`,
308 handler: () => this.playlistDropdown.toggle(),
309 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
310 iconName: 'playlist-add'
313 [ // actions regarding the video
315 label: $localize`Download`,
316 handler: () => this.showDownloadModal(),
317 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
321 label: $localize`Display live information`,
322 handler: ({ video }) => this.showLiveInfoModal(video),
323 isDisplayed: () => this.displayOptions.liveInfo && this.isVideoLiveInfoAvailable(),
327 label: $localize`Update`,
328 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
330 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
333 label: $localize`Block`,
334 handler: () => this.showBlockModal(),
336 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlockable()
339 label: $localize`Unblock`,
340 handler: () => this.unblockVideo(),
342 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblockable()
345 label: $localize`Mirror`,
346 handler: () => this.duplicateVideo(),
347 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
348 iconName: 'cloud-download'
351 label: $localize`Delete`,
352 handler: () => this.removeVideo(),
353 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
357 label: $localize`Report`,
358 handler: () => this.showReportModal(),
359 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
365 label: $localize`Run HLS transcoding`,
366 handler: ({ video }) => this.runTranscoding(video, 'hls'),
367 isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),
371 label: $localize`Run WebTorrent transcoding`,
372 handler: ({ video }) => this.runTranscoding(video, 'webtorrent'),
373 isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),
377 label: $localize`Delete HLS files`,
378 handler: ({ video }) => this.removeVideoFiles(video, 'hls'),
379 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
383 label: $localize`Delete WebTorrent files`,
384 handler: ({ video }) => this.removeVideoFiles(video, 'webtorrent'),
385 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
389 [ // actions regarding the account/its server
391 label: $localize`Mute account`,
392 handler: () => this.muteVideoAccount(),
393 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.mute && this.isVideoAccountMutable(),