1 import { AfterViewInit, Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { I18n } from '@ngx-translate/i18n-polyfill'
3 import { DropdownAction, DropdownButtonSize, DropdownDirection } from '@app/shared/buttons/action-dropdown.component'
4 import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
5 import { BlocklistService } from '@app/shared/blocklist'
6 import { Video } from '@app/shared/video/video.model'
7 import { VideoService } from '@app/shared/video/video.service'
8 import { VideoDetails } from '@app/shared/video/video-details.model'
9 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
10 import { VideoAddToPlaylistComponent } from '@app/shared/video-playlist/video-add-to-playlist.component'
11 import { VideoDownloadComponent } from '@app/shared/video/modals/video-download.component'
12 import { VideoReportComponent } from '@app/shared/video/modals/video-report.component'
13 import { VideoBlacklistComponent } from '@app/shared/video/modals/video-blacklist.component'
14 import { VideoBlacklistService } from '@app/shared/video-blacklist'
15 import { ScreenService } from '@app/shared/misc/screen.service'
17 export type VideoActionsDisplayType = {
27 selector: 'my-video-actions-dropdown',
28 templateUrl: './video-actions-dropdown.component.html',
29 styleUrls: [ './video-actions-dropdown.component.scss' ]
31 export class VideoActionsDropdownComponent implements OnChanges {
32 @ViewChild('playlistDropdown', { static: false }) playlistDropdown: NgbDropdown
33 @ViewChild('playlistAdd', { static: false }) playlistAdd: VideoAddToPlaylistComponent
35 @ViewChild('videoDownloadModal', { static: false }) videoDownloadModal: VideoDownloadComponent
36 @ViewChild('videoReportModal', { static: false }) videoReportModal: VideoReportComponent
37 @ViewChild('videoBlacklistModal', { static: false }) videoBlacklistModal: VideoBlacklistComponent
39 @Input() video: Video | VideoDetails
41 @Input() displayOptions: VideoActionsDisplayType = {
49 @Input() placement = 'left'
51 @Input() label: string
53 @Input() buttonStyled = false
54 @Input() buttonSize: DropdownButtonSize = 'normal'
55 @Input() buttonDirection: DropdownDirection = 'vertical'
57 @Output() videoRemoved = new EventEmitter()
58 @Output() videoUnblacklisted = new EventEmitter()
59 @Output() videoBlacklisted = new EventEmitter()
60 @Output() modalOpened = new EventEmitter()
62 videoActions: DropdownAction<{ video: Video }>[][] = []
64 private loaded = false
67 private authService: AuthService,
68 private notifier: Notifier,
69 private confirmService: ConfirmService,
70 private videoBlacklistService: VideoBlacklistService,
71 private serverService: ServerService,
72 private screenService: ScreenService,
73 private videoService: VideoService,
74 private blocklistService: BlocklistService,
79 return this.authService.getUser()
85 this.playlistAdd.reload()
92 return this.authService.isLoggedIn()
95 loadDropdownInformation () {
96 if (!this.isUserLoggedIn() || this.loaded === true) return
100 if (this.displayOptions.playlist) this.playlistAdd.load()
105 showDownloadModal () {
106 this.modalOpened.emit()
108 this.videoDownloadModal.show(this.video as VideoDetails)
112 this.modalOpened.emit()
114 this.videoReportModal.show()
117 showBlacklistModal () {
118 this.modalOpened.emit()
120 this.videoBlacklistModal.show()
123 /* Actions checker */
125 isVideoUpdatable () {
126 return this.video.isUpdatableBy(this.user)
129 isVideoRemovable () {
130 return this.video.isRemovableBy(this.user)
133 isVideoBlacklistable () {
134 return this.video.isBlackistableBy(this.user)
137 isVideoUnblacklistable () {
138 return this.video.isUnblacklistableBy(this.user)
141 isVideoDownloadable () {
142 return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled
145 /* Action handlers */
147 async unblacklistVideo () {
148 const confirmMessage = this.i18n(
149 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
152 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
153 if (res === false) return
155 this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
157 this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
159 this.video.blacklisted = false
160 this.video.blacklistedReason = null
162 this.videoUnblacklisted.emit()
165 err => this.notifier.error(err.message)
169 async removeVideo () {
170 this.modalOpened.emit()
172 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
173 if (res === false) return
175 this.videoService.removeVideo(this.video.id)
178 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
180 this.videoRemoved.emit()
183 error => this.notifier.error(error.message)
187 onVideoBlacklisted () {
188 this.videoBlacklisted.emit()
191 getPlaylistDropdownPlacement () {
192 if (this.screenService.isInSmallView()) {
193 return 'bottom-right'
196 return 'bottom-left bottom-right'
199 private buildActions () {
200 this.videoActions = [
203 label: this.i18n('Save to playlist'),
204 handler: () => this.playlistDropdown.toggle(),
205 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
206 iconName: 'playlist-add'
211 label: this.i18n('Download'),
212 handler: () => this.showDownloadModal(),
213 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
217 label: this.i18n('Update'),
218 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
220 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
223 label: this.i18n('Blacklist'),
224 handler: () => this.showBlacklistModal(),
226 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlacklistable()
229 label: this.i18n('Unblacklist'),
230 handler: () => this.unblacklistVideo(),
232 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblacklistable()
235 label: this.i18n('Delete'),
236 handler: () => this.removeVideo(),
237 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
243 label: this.i18n('Report'),
244 handler: () => this.showReportModal(),
245 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,