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 () {
150 this.video.isLive !== true &&
151 this.video instanceof VideoDetails &&
152 this.video.downloadEnabled
155 canVideoBeDuplicated () {
156 return this.video.canBeDuplicatedBy(this.user)
159 isVideoAccountMutable () {
160 return this.video.account.id !== this.user.account.id
163 /* Action handlers */
165 async unblockVideo () {
166 const confirmMessage = $localize`Do you really want to unblock this video? It will be available again in the videos list.`
168 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock`)
169 if (res === false) return
171 this.videoBlocklistService.unblockVideo(this.video.id)
174 this.notifier.success($localize`Video ${this.video.name} unblocked.`)
176 this.video.blacklisted = false
177 this.video.blockedReason = null
179 this.videoUnblocked.emit()
182 err => this.notifier.error(err.message)
186 async removeVideo () {
187 this.modalOpened.emit()
189 let message = $localize`Do you really want to delete this video?`
190 if (this.video.isLive) {
191 message += ' ' + $localize`The live stream will be automatically terminated.`
194 const res = await this.confirmService.confirm(message, $localize`Delete`)
195 if (res === false) return
197 this.videoService.removeVideo(this.video.id)
200 this.notifier.success($localize`Video ${this.video.name} deleted.`)
201 this.videoRemoved.emit()
204 error => this.notifier.error(error.message)
209 this.redundancyService.addVideoRedundancy(this.video)
212 const message = $localize`This video will be duplicated by your instance.`
213 this.notifier.success(message)
216 err => this.notifier.error(err.message)
220 muteVideoAccount () {
221 const params = { nameWithHost: Actor.CREATE_BY_STRING(this.video.account.name, this.video.account.host) }
223 this.blocklistService.blockAccountByUser(params)
226 this.notifier.success($localize`Account ${params.nameWithHost} muted.`)
227 this.videoAccountMuted.emit()
230 err => this.notifier.error(err.message)
235 this.videoBlocked.emit()
238 getPlaylistDropdownPlacement () {
239 if (this.screenService.isInSmallView()) {
240 return 'bottom-right'
243 return 'bottom-left bottom-right'
246 private buildActions () {
247 this.videoActions = [
250 label: $localize`Save to playlist`,
251 handler: () => this.playlistDropdown.toggle(),
252 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
253 iconName: 'playlist-add'
256 [ // actions regarding the video
258 label: $localize`Download`,
259 handler: () => this.showDownloadModal(),
260 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
264 label: $localize`Update`,
265 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
267 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
270 label: $localize`Block`,
271 handler: () => this.showBlockModal(),
273 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlockable()
276 label: $localize`Unblock`,
277 handler: () => this.unblockVideo(),
279 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblockable()
282 label: $localize`Mirror`,
283 handler: () => this.duplicateVideo(),
284 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
285 iconName: 'cloud-download'
288 label: $localize`Delete`,
289 handler: () => this.removeVideo(),
290 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
294 label: $localize`Report`,
295 handler: () => this.showReportModal(),
296 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
300 [ // actions regarding the account/its server
302 label: $localize`Mute account`,
303 handler: () => this.muteVideoAccount(),
304 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.mute && this.isVideoAccountMutable(),