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