1 import { 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') playlistDropdown: NgbDropdown
33 @ViewChild('playlistAdd') playlistAdd: VideoAddToPlaylistComponent
35 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
36 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
37 @ViewChild('videoBlacklistModal') 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()
86 return this.authService.isLoggedIn()
89 loadDropdownInformation () {
90 if (!this.isUserLoggedIn() || this.loaded === true) return
94 if (this.displayOptions.playlist) this.playlistAdd.load()
99 showDownloadModal () {
100 this.videoDownloadModal.show(this.video as VideoDetails)
104 this.videoReportModal.show()
107 showBlacklistModal () {
108 this.videoBlacklistModal.show()
111 /* Actions checker */
113 isVideoUpdatable () {
114 return this.video.isUpdatableBy(this.user)
117 isVideoRemovable () {
118 return this.video.isRemovableBy(this.user)
121 isVideoBlacklistable () {
122 return this.video.isBlackistableBy(this.user)
125 isVideoUnblacklistable () {
126 return this.video.isUnblacklistableBy(this.user)
129 /* Action handlers */
131 async unblacklistVideo () {
132 const confirmMessage = this.i18n(
133 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
136 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
137 if (res === false) return
139 this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
141 this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
143 this.video.blacklisted = false
144 this.video.blacklistedReason = null
146 this.videoUnblacklisted.emit()
149 err => this.notifier.error(err.message)
153 async removeVideo () {
154 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
155 if (res === false) return
157 this.videoService.removeVideo(this.video.id)
160 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
162 this.videoRemoved.emit()
165 error => this.notifier.error(error.message)
169 onVideoBlacklisted () {
170 this.videoBlacklisted.emit()
173 getPlaylistDropdownPlacement () {
174 if (this.screenService.isInSmallView()) {
175 return 'bottom-right'
178 return 'bottom-left bottom-right'
181 private buildActions () {
182 this.videoActions = []
184 if (this.authService.isLoggedIn()) {
185 this.videoActions.push([
187 label: this.i18n('Save to playlist'),
188 handler: () => this.playlistDropdown.toggle(),
189 isDisplayed: () => this.displayOptions.playlist,
190 iconName: 'playlist-add'
194 this.videoActions.push([
196 label: this.i18n('Download'),
197 handler: () => this.showDownloadModal(),
198 isDisplayed: () => this.displayOptions.download,
202 label: this.i18n('Update'),
203 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
205 isDisplayed: () => this.displayOptions.update && this.isVideoUpdatable()
208 label: this.i18n('Blacklist'),
209 handler: () => this.showBlacklistModal(),
211 isDisplayed: () => this.displayOptions.blacklist && this.isVideoBlacklistable()
214 label: this.i18n('Unblacklist'),
215 handler: () => this.unblacklistVideo(),
217 isDisplayed: () => this.displayOptions.blacklist && this.isVideoUnblacklistable()
220 label: this.i18n('Delete'),
221 handler: () => this.removeVideo(),
222 isDisplayed: () => this.displayOptions.delete && this.isVideoRemovable(),
227 this.videoActions.push([
229 label: this.i18n('Report'),
230 handler: () => this.showReportModal(),
231 isDisplayed: () => this.displayOptions.report,