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 { VideoCaption } from '@shared/models'
15 } from '../shared-main'
16 import { VideoAddToPlaylistComponent } from '../shared-video-playlist'
17 import { VideoDownloadComponent } from './video-download.component'
19 export type VideoActionsDisplayType = {
31 selector: 'my-video-actions-dropdown',
32 templateUrl: './video-actions-dropdown.component.html',
33 styleUrls: [ './video-actions-dropdown.component.scss' ]
35 export class VideoActionsDropdownComponent implements OnChanges {
36 @ViewChild('playlistDropdown') playlistDropdown: NgbDropdown
37 @ViewChild('playlistAdd') playlistAdd: VideoAddToPlaylistComponent
39 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
40 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
41 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
43 @Input() video: Video | VideoDetails
44 @Input() videoCaptions: VideoCaption[] = []
46 @Input() displayOptions: VideoActionsDisplayType = {
56 @Input() placement = 'left'
58 @Input() label: string
60 @Input() buttonStyled = false
61 @Input() buttonSize: DropdownButtonSize = 'normal'
62 @Input() buttonDirection: DropdownDirection = 'vertical'
64 @Output() videoRemoved = new EventEmitter()
65 @Output() videoUnblocked = new EventEmitter()
66 @Output() videoBlocked = new EventEmitter()
67 @Output() videoAccountMuted = new EventEmitter()
68 @Output() modalOpened = new EventEmitter()
70 videoActions: DropdownAction<{ video: Video }>[][] = []
72 private loaded = false
75 private authService: AuthService,
76 private notifier: Notifier,
77 private confirmService: ConfirmService,
78 private blocklistService: BlocklistService,
79 private videoBlocklistService: VideoBlockService,
80 private screenService: ScreenService,
81 private videoService: VideoService,
82 private redundancyService: RedundancyService
86 return this.authService.getUser()
92 this.playlistAdd.reload()
99 return this.authService.isLoggedIn()
102 loadDropdownInformation () {
103 if (!this.isUserLoggedIn() || this.loaded === true) return
107 if (this.displayOptions.playlist) this.playlistAdd.load()
112 showDownloadModal () {
113 this.modalOpened.emit()
115 this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions)
119 this.modalOpened.emit()
121 this.videoReportModal.show()
125 this.modalOpened.emit()
127 this.videoBlockModal.show()
130 /* Actions checker */
132 isVideoUpdatable () {
133 return this.video.isUpdatableBy(this.user)
136 isVideoRemovable () {
137 return this.video.isRemovableBy(this.user)
140 isVideoBlockable () {
141 return this.video.isBlockableBy(this.user)
144 isVideoUnblockable () {
145 return this.video.isUnblockableBy(this.user)
148 isVideoDownloadable () {
149 return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled
152 canVideoBeDuplicated () {
153 return this.video.canBeDuplicatedBy(this.user)
156 isVideoAccountMutable () {
157 return this.video.account.id !== this.user.account.id
160 /* Action handlers */
162 async unblockVideo () {
163 const confirmMessage = $localize`Do you really want to unblock this video? It will be available again in the videos list.`
165 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock`)
166 if (res === false) return
168 this.videoBlocklistService.unblockVideo(this.video.id)
171 this.notifier.success($localize`Video ${this.video.name} unblocked.`)
173 this.video.blacklisted = false
174 this.video.blockedReason = null
176 this.videoUnblocked.emit()
179 err => this.notifier.error(err.message)
183 async removeVideo () {
184 this.modalOpened.emit()
186 const res = await this.confirmService.confirm($localize`Do you really want to delete this video?`, $localize`Delete`)
187 if (res === false) return
189 this.videoService.removeVideo(this.video.id)
192 this.notifier.success($localize`Video ${this.video.name} deleted.`)
193 this.videoRemoved.emit()
196 error => this.notifier.error(error.message)
201 this.redundancyService.addVideoRedundancy(this.video)
204 const message = $localize`This video will be duplicated by your instance.`
205 this.notifier.success(message)
208 err => this.notifier.error(err.message)
212 muteVideoAccount () {
213 const params = { nameWithHost: Actor.CREATE_BY_STRING(this.video.account.name, this.video.account.host) }
215 this.blocklistService.blockAccountByUser(params)
218 this.notifier.success($localize`Account ${params.nameWithHost} muted.`)
219 this.videoAccountMuted.emit()
222 err => this.notifier.error(err.message)
227 this.videoBlocked.emit()
230 getPlaylistDropdownPlacement () {
231 if (this.screenService.isInSmallView()) {
232 return 'bottom-right'
235 return 'bottom-left bottom-right'
238 private buildActions () {
239 this.videoActions = [
242 label: $localize`Save to playlist`,
243 handler: () => this.playlistDropdown.toggle(),
244 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
245 iconName: 'playlist-add'
248 [ // actions regarding the video
250 label: $localize`Download`,
251 handler: () => this.showDownloadModal(),
252 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
256 label: $localize`Update`,
257 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
259 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
262 label: $localize`Block`,
263 handler: () => this.showBlockModal(),
265 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlockable()
268 label: $localize`Unblock`,
269 handler: () => this.unblockVideo(),
271 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblockable()
274 label: $localize`Mirror`,
275 handler: () => this.duplicateVideo(),
276 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
277 iconName: 'cloud-download'
280 label: $localize`Delete`,
281 handler: () => this.removeVideo(),
282 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
286 label: $localize`Report`,
287 handler: () => this.showReportModal(),
288 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
292 [ // actions regarding the account/its server
294 label: $localize`Mute account`,
295 handler: () => this.muteVideoAccount(),
296 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.mute && this.isVideoAccountMutable(),