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()
61 videoActions: DropdownAction<{ video: Video }>[][] = []
63 private loaded = false
66 private authService: AuthService,
67 private notifier: Notifier,
68 private confirmService: ConfirmService,
69 private videoBlacklistService: VideoBlacklistService,
70 private serverService: ServerService,
71 private screenService: ScreenService,
72 private videoService: VideoService,
73 private blocklistService: BlocklistService,
78 return this.authService.getUser()
84 this.playlistAdd.reload()
91 return this.authService.isLoggedIn()
94 loadDropdownInformation () {
95 if (!this.isUserLoggedIn() || this.loaded === true) return
99 if (this.displayOptions.playlist) this.playlistAdd.load()
104 showDownloadModal () {
105 this.videoDownloadModal.show(this.video as VideoDetails)
109 this.videoReportModal.show()
112 showBlacklistModal () {
113 this.videoBlacklistModal.show()
116 /* Actions checker */
118 isVideoUpdatable () {
119 return this.video.isUpdatableBy(this.user)
122 isVideoRemovable () {
123 return this.video.isRemovableBy(this.user)
126 isVideoBlacklistable () {
127 return this.video.isBlackistableBy(this.user)
130 isVideoUnblacklistable () {
131 return this.video.isUnblacklistableBy(this.user)
134 isVideoDownloadable () {
135 return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled
138 /* Action handlers */
140 async unblacklistVideo () {
141 const confirmMessage = this.i18n(
142 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
145 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
146 if (res === false) return
148 this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
150 this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
152 this.video.blacklisted = false
153 this.video.blacklistedReason = null
155 this.videoUnblacklisted.emit()
158 err => this.notifier.error(err.message)
162 async removeVideo () {
163 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
164 if (res === false) return
166 this.videoService.removeVideo(this.video.id)
169 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
171 this.videoRemoved.emit()
174 error => this.notifier.error(error.message)
178 onVideoBlacklisted () {
179 this.videoBlacklisted.emit()
182 getPlaylistDropdownPlacement () {
183 if (this.screenService.isInSmallView()) {
184 return 'bottom-right'
187 return 'bottom-left bottom-right'
190 private buildActions () {
191 this.videoActions = [
194 label: this.i18n('Save to playlist'),
195 handler: () => this.playlistDropdown.toggle(),
196 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
197 iconName: 'playlist-add'
202 label: this.i18n('Download'),
203 handler: () => this.showDownloadModal(),
204 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
208 label: this.i18n('Update'),
209 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
211 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
214 label: this.i18n('Blacklist'),
215 handler: () => this.showBlacklistModal(),
217 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlacklistable()
220 label: this.i18n('Unblacklist'),
221 handler: () => this.unblacklistVideo(),
223 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblacklistable()
226 label: this.i18n('Delete'),
227 handler: () => this.removeVideo(),
228 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
234 label: this.i18n('Report'),
235 handler: () => this.showReportModal(),
236 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,