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