]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.ts
Added filter to sort videos by name (alphabetical order)
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-actions-dropdown.component.ts
CommitLineData
54e78847 1import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
c729caf6 2import { AuthService, ConfirmService, Notifier, ScreenService, ServerService } from '@app/core'
66357162 3import { BlocklistService, VideoBlockComponent, VideoBlockService, VideoReportComponent } from '@app/shared/shared-moderation'
3a0fb65c 4import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
22e90922 5import { VideoCaption } from '@shared/models'
66357162
C
6import {
7 Actor,
8 DropdownAction,
9 DropdownButtonSize,
10 DropdownDirection,
11 RedundancyService,
12 Video,
13 VideoDetails,
14 VideoService
15} from '../shared-main'
f8c00564 16import { LiveStreamInformationComponent } from '../shared-video-live'
67ed6552
C
17import { VideoAddToPlaylistComponent } from '../shared-video-playlist'
18import { VideoDownloadComponent } from './video-download.component'
3a0fb65c
C
19
20export type VideoActionsDisplayType = {
21 playlist?: boolean
22 download?: boolean
23 update?: boolean
24 blacklist?: boolean
25 delete?: boolean
26 report?: boolean
b764380a 27 duplicate?: boolean
d473fd94 28 mute?: boolean
f8c00564 29 liveInfo?: boolean
b46cf4b9 30 removeFiles?: boolean
ad5db104 31 transcoding?: boolean
92e66e04 32 studio?: boolean
384ba8b7 33 stats?: boolean
3a0fb65c
C
34}
35
36@Component({
37 selector: 'my-video-actions-dropdown',
38 templateUrl: './video-actions-dropdown.component.html',
39 styleUrls: [ './video-actions-dropdown.component.scss' ]
40})
4da22f64 41export class VideoActionsDropdownComponent implements OnChanges {
2f5d2ec5
C
42 @ViewChild('playlistDropdown') playlistDropdown: NgbDropdown
43 @ViewChild('playlistAdd') playlistAdd: VideoAddToPlaylistComponent
3a0fb65c 44
2f5d2ec5
C
45 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
46 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
5baee5fc 47 @ViewChild('videoBlockModal') videoBlockModal: VideoBlockComponent
f8c00564 48 @ViewChild('liveStreamInformationModal') liveStreamInformationModal: LiveStreamInformationComponent
3a0fb65c
C
49
50 @Input() video: Video | VideoDetails
8ba9c205 51 @Input() videoCaptions: VideoCaption[] = []
3a0fb65c
C
52
53 @Input() displayOptions: VideoActionsDisplayType = {
54 playlist: false,
55 download: true,
56 update: true,
57 blacklist: true,
58 delete: true,
b764380a 59 report: true,
d473fd94 60 duplicate: true,
f8c00564 61 mute: true,
ad5db104
C
62 liveInfo: false,
63 removeFiles: false,
c729caf6 64 transcoding: false,
384ba8b7
C
65 studio: true,
66 stats: true
3a0fb65c 67 }
8dfceec4 68 @Input() placement = 'left'
384ba8b7 69 @Input() moreActions: DropdownAction<{ video: Video }>[][] = []
3a0fb65c
C
70
71 @Input() label: string
72
73 @Input() buttonStyled = false
74 @Input() buttonSize: DropdownButtonSize = 'normal'
75 @Input() buttonDirection: DropdownDirection = 'vertical'
76
b46cf4b9 77 @Output() videoFilesRemoved = new EventEmitter()
3a0fb65c 78 @Output() videoRemoved = new EventEmitter()
5baee5fc
RK
79 @Output() videoUnblocked = new EventEmitter()
80 @Output() videoBlocked = new EventEmitter()
d473fd94 81 @Output() videoAccountMuted = new EventEmitter()
ad5db104 82 @Output() transcodingCreated = new EventEmitter()
689a4f69 83 @Output() modalOpened = new EventEmitter()
3a0fb65c
C
84
85 videoActions: DropdownAction<{ video: Video }>[][] = []
86
87 private loaded = false
88
89 constructor (
90 private authService: AuthService,
91 private notifier: Notifier,
92 private confirmService: ConfirmService,
d473fd94 93 private blocklistService: BlocklistService,
5baee5fc 94 private videoBlocklistService: VideoBlockService,
3a0fb65c
C
95 private screenService: ScreenService,
96 private videoService: VideoService,
c729caf6
C
97 private redundancyService: RedundancyService,
98 private serverService: ServerService
3a0fb65c
C
99 ) { }
100
101 get user () {
102 return this.authService.getUser()
103 }
104
105 ngOnChanges () {
1c8ddbfa
C
106 if (this.loaded) {
107 this.loaded = false
3bc68dfd 108 if (this.playlistAdd) this.playlistAdd.reload()
1c8ddbfa
C
109 }
110
3a0fb65c
C
111 this.buildActions()
112 }
113
114 isUserLoggedIn () {
115 return this.authService.isLoggedIn()
116 }
117
118 loadDropdownInformation () {
119 if (!this.isUserLoggedIn() || this.loaded === true) return
120
121 this.loaded = true
122
123 if (this.displayOptions.playlist) this.playlistAdd.load()
124 }
125
126 /* Show modals */
127
128 showDownloadModal () {
689a4f69
C
129 this.modalOpened.emit()
130
8ba9c205 131 this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions)
3a0fb65c
C
132 }
133
134 showReportModal () {
689a4f69
C
135 this.modalOpened.emit()
136
3a0fb65c
C
137 this.videoReportModal.show()
138 }
139
5baee5fc 140 showBlockModal () {
689a4f69
C
141 this.modalOpened.emit()
142
3cfa8176 143 this.videoBlockModal.show([ this.video ])
3a0fb65c
C
144 }
145
f8c00564
C
146 showLiveInfoModal (video: Video) {
147 this.modalOpened.emit()
148
149 this.liveStreamInformationModal.show(video)
150 }
151
3a0fb65c
C
152 /* Actions checker */
153
154 isVideoUpdatable () {
155 return this.video.isUpdatableBy(this.user)
156 }
157
c729caf6 158 isVideoEditable () {
92e66e04 159 return this.video.isEditableBy(this.user, this.serverService.getHTMLConfig().videoStudio.enabled)
c729caf6
C
160 }
161
384ba8b7
C
162 isVideoStatsAvailable () {
163 return this.video.canSeeStats(this.user)
164 }
165
3a0fb65c
C
166 isVideoRemovable () {
167 return this.video.isRemovableBy(this.user)
168 }
169
5baee5fc
RK
170 isVideoBlockable () {
171 return this.video.isBlockableBy(this.user)
3a0fb65c
C
172 }
173
5baee5fc
RK
174 isVideoUnblockable () {
175 return this.video.isUnblockableBy(this.user)
3a0fb65c
C
176 }
177
f8c00564
C
178 isVideoLiveInfoAvailable () {
179 return this.video.isLiveInfoAvailableBy(this.user)
180 }
181
72675ebe 182 isVideoDownloadable () {
a5cf76af
C
183 return this.video &&
184 this.video.isLive !== true &&
185 this.video instanceof VideoDetails &&
186 this.video.downloadEnabled
72675ebe
C
187 }
188
b764380a 189 canVideoBeDuplicated () {
17b7d4b3 190 return !this.video.isLive && this.video.canBeDuplicatedBy(this.user)
b764380a
C
191 }
192
d473fd94
RK
193 isVideoAccountMutable () {
194 return this.video.account.id !== this.user.account.id
195 }
196
b46cf4b9 197 canRemoveVideoFiles () {
ad5db104
C
198 return this.video.canRemoveFiles(this.user)
199 }
200
201 canRunTranscoding () {
202 return this.video.canRunTranscoding(this.user)
b46cf4b9
C
203 }
204
3a0fb65c
C
205 /* Action handlers */
206
5baee5fc 207 async unblockVideo () {
bc844338 208 const confirmMessage = $localize`Do you really want to unblock ${this.video.name}? It will be available again in the videos list.`
3a0fb65c 209
bc844338 210 const res = await this.confirmService.confirm(confirmMessage, $localize`Unblock ${this.video.name}`)
3a0fb65c
C
211 if (res === false) return
212
d473fd94 213 this.videoBlocklistService.unblockVideo(this.video.id)
1378c0d3
C
214 .subscribe({
215 next: () => {
66357162 216 this.notifier.success($localize`Video ${this.video.name} unblocked.`)
3a0fb65c 217
d473fd94 218 this.video.blacklisted = false
2760b454 219 this.video.blacklistedReason = null
3a0fb65c 220
d473fd94
RK
221 this.videoUnblocked.emit()
222 },
3a0fb65c 223
1378c0d3
C
224 error: err => this.notifier.error(err.message)
225 })
3a0fb65c
C
226 }
227
228 async removeVideo () {
689a4f69
C
229 this.modalOpened.emit()
230
bc844338 231 let message = $localize`Do you really want to delete ${this.video.name}?`
d846d99c 232 if (this.video.isLive) {
c8fa571f 233 message += ' ' + $localize`The live stream will be automatically terminated and replays won't be saved.`
d846d99c
C
234 }
235
bc844338 236 const res = await this.confirmService.confirm(message, $localize`Delete ${this.video.name}`)
3a0fb65c
C
237 if (res === false) return
238
239 this.videoService.removeVideo(this.video.id)
1378c0d3
C
240 .subscribe({
241 next: () => {
66357162 242 this.notifier.success($localize`Video ${this.video.name} deleted.`)
3a0fb65c
C
243 this.videoRemoved.emit()
244 },
245
1378c0d3
C
246 error: err => this.notifier.error(err.message)
247 })
3a0fb65c
C
248 }
249
b764380a
C
250 duplicateVideo () {
251 this.redundancyService.addVideoRedundancy(this.video)
1378c0d3
C
252 .subscribe({
253 next: () => {
bc844338 254 const message = $localize`${this.video.name} will be duplicated by your instance.`
d473fd94
RK
255 this.notifier.success(message)
256 },
b764380a 257
1378c0d3
C
258 error: err => this.notifier.error(err.message)
259 })
d473fd94
RK
260 }
261
262 muteVideoAccount () {
263 const params = { nameWithHost: Actor.CREATE_BY_STRING(this.video.account.name, this.video.account.host) }
264
265 this.blocklistService.blockAccountByUser(params)
1378c0d3
C
266 .subscribe({
267 next: () => {
66357162 268 this.notifier.success($localize`Account ${params.nameWithHost} muted.`)
d473fd94
RK
269 this.videoAccountMuted.emit()
270 },
271
1378c0d3
C
272 error: err => this.notifier.error(err.message)
273 })
b764380a
C
274 }
275
b46cf4b9
C
276 async removeVideoFiles (video: Video, type: 'hls' | 'webtorrent') {
277 const confirmMessage = $localize`Do you really want to remove "${this.video.name}" files?`
278
279 const res = await this.confirmService.confirm(confirmMessage, $localize`Remove "${this.video.name}" files`)
280 if (res === false) return
281
282 this.videoService.removeVideoFiles([ video.id ], type)
283 .subscribe({
284 next: () => {
285 this.notifier.success($localize`Removed files of ${video.name}.`)
286 this.videoFilesRemoved.emit()
287 },
288
289 error: err => this.notifier.error(err.message)
290 })
291 }
292
ad5db104
C
293 runTranscoding (video: Video, type: 'hls' | 'webtorrent') {
294 this.videoService.runTranscoding([ video.id ], type)
295 .subscribe({
296 next: () => {
297 this.notifier.success($localize`Transcoding jobs created for ${video.name}.`)
298 this.transcodingCreated.emit()
299 },
300
301 error: err => this.notifier.error(err.message)
302 })
303 }
304
5baee5fc
RK
305 onVideoBlocked () {
306 this.videoBlocked.emit()
3a0fb65c
C
307 }
308
309 getPlaylistDropdownPlacement () {
310 if (this.screenService.isInSmallView()) {
311 return 'bottom-right'
312 }
313
314 return 'bottom-left bottom-right'
315 }
316
317 private buildActions () {
f238aec5
C
318 this.videoActions = [
319 [
3a0fb65c 320 {
66357162 321 label: $localize`Save to playlist`,
3a0fb65c 322 handler: () => this.playlistDropdown.toggle(),
f238aec5 323 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
3a0fb65c
C
324 iconName: 'playlist-add'
325 }
f238aec5 326 ],
d473fd94 327 [ // actions regarding the video
3a0fb65c 328 {
66357162 329 label: $localize`Download`,
3a0fb65c 330 handler: () => this.showDownloadModal(),
72675ebe 331 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
3a0fb65c
C
332 iconName: 'download'
333 },
f8c00564
C
334 {
335 label: $localize`Display live information`,
336 handler: ({ video }) => this.showLiveInfoModal(video),
3bc68dfd 337 isDisplayed: () => this.displayOptions.liveInfo && this.isVideoLiveInfoAvailable(),
f8c00564
C
338 iconName: 'live'
339 },
3a0fb65c 340 {
66357162 341 label: $localize`Update`,
3a0fb65c
C
342 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
343 iconName: 'edit',
f238aec5 344 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
3a0fb65c 345 },
c729caf6 346 {
92e66e04
C
347 label: $localize`Studio`,
348 linkBuilder: ({ video }) => [ '/studio/edit', video.uuid ],
c729caf6 349 iconName: 'film',
92e66e04 350 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.studio && this.isVideoEditable()
c729caf6 351 },
384ba8b7
C
352 {
353 label: $localize`Stats`,
354 linkBuilder: ({ video }) => [ '/stats/videos', video.uuid ],
355 iconName: 'stats',
356 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.stats && this.isVideoStatsAvailable()
357 },
3a0fb65c 358 {
66357162 359 label: $localize`Block`,
5baee5fc 360 handler: () => this.showBlockModal(),
3a0fb65c 361 iconName: 'no',
5baee5fc 362 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlockable()
3a0fb65c
C
363 },
364 {
66357162 365 label: $localize`Unblock`,
5baee5fc 366 handler: () => this.unblockVideo(),
3a0fb65c 367 iconName: 'undo',
5baee5fc 368 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblockable()
3a0fb65c 369 },
b764380a 370 {
66357162 371 label: $localize`Mirror`,
b764380a
C
372 handler: () => this.duplicateVideo(),
373 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
374 iconName: 'cloud-download'
375 },
3a0fb65c 376 {
66357162 377 label: $localize`Delete`,
3a0fb65c 378 handler: () => this.removeVideo(),
f238aec5 379 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
3a0fb65c 380 iconName: 'delete'
d473fd94 381 },
3a0fb65c 382 {
66357162 383 label: $localize`Report`,
3a0fb65c 384 handler: () => this.showReportModal(),
f238aec5 385 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
c41c0e28 386 iconName: 'flag'
3a0fb65c 387 }
d473fd94 388 ],
b46cf4b9 389 [
ad5db104
C
390 {
391 label: $localize`Run HLS transcoding`,
392 handler: ({ video }) => this.runTranscoding(video, 'hls'),
393 isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),
394 iconName: 'cog'
395 },
396 {
397 label: $localize`Run WebTorrent transcoding`,
398 handler: ({ video }) => this.runTranscoding(video, 'webtorrent'),
399 isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),
400 iconName: 'cog'
401 },
b46cf4b9
C
402 {
403 label: $localize`Delete HLS files`,
404 handler: ({ video }) => this.removeVideoFiles(video, 'hls'),
405 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
406 iconName: 'delete'
407 },
408 {
409 label: $localize`Delete WebTorrent files`,
410 handler: ({ video }) => this.removeVideoFiles(video, 'webtorrent'),
411 isDisplayed: () => this.displayOptions.removeFiles && this.canRemoveVideoFiles(),
412 iconName: 'delete'
413 }
414 ],
d473fd94
RK
415 [ // actions regarding the account/its server
416 {
66357162 417 label: $localize`Mute account`,
d473fd94
RK
418 handler: () => this.muteVideoAccount(),
419 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.mute && this.isVideoAccountMutable(),
420 iconName: 'no'
421 }
f238aec5
C
422 ]
423 ]
384ba8b7
C
424
425 this.videoActions = this.videoActions.concat(this.moreActions)
3a0fb65c
C
426 }
427}