1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
15 import { VideoCaptionService } from '@app/shared/video-caption'
16 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
19 selector: 'my-videos-update',
20 styleUrls: [ './shared/video-edit.component.scss' ],
21 templateUrl: './video-update.component.html'
23 export class VideoUpdateComponent extends FormReactive implements OnInit {
26 isUpdatingVideo = false
27 videoPrivacies: VideoConstant<string>[] = []
28 userVideoChannels: { id: number, label: string, support: string }[] = []
29 schedulePublicationPossible = false
30 videoCaptions: VideoCaptionEdit[] = []
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private authService: AuthService,
40 private loadingBar: LoadingBarService,
41 private videoChannelService: VideoChannelService,
42 private videoCaptionService: VideoCaptionService,
51 this.serverService.videoPrivaciesLoaded
52 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
54 const uuid: string = this.route.snapshot.params[ 'uuid' ]
55 this.videoService.getVideo(uuid)
58 return this.videoService
59 .loadCompleteDescription(video.descriptionPath)
60 .pipe(map(description => Object.assign(video, { description })))
63 return this.videoChannelService
64 .listAccountVideoChannels(video.account)
66 map(result => result.data),
67 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
68 map(videoChannels => ({ video, videoChannels }))
71 switchMap(({ video, videoChannels }) => {
72 return this.videoCaptionService
73 .listCaptions(video.id)
75 map(result => result.data),
76 map(videoCaptions => ({ video, videoChannels, videoCaptions }))
81 ({ video, videoChannels, videoCaptions }) => {
82 this.video = new VideoEdit(video)
83 this.userVideoChannels = videoChannels
84 this.videoCaptions = videoCaptions
86 // We cannot set private a video that was not private
87 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
88 this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString())
89 } else { // We can schedule video publication only if it it is private
90 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
93 this.hydrateFormFromVideo()
98 this.notificationsService.error(this.i18n('Error'), err.message)
106 return this.form.valid
110 if (this.checkForm() === false) {
114 this.video.patch(this.form.value)
116 this.loadingBar.start()
117 this.isUpdatingVideo = true
120 this.videoService.updateVideo(this.video)
122 // Then update captions
123 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
127 this.isUpdatingVideo = false
128 this.loadingBar.complete()
129 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
130 this.router.navigate([ '/videos/watch', this.video.uuid ])
134 this.isUpdatingVideo = false
135 this.notificationsService.error(this.i18n('Error'), err.message)
142 private hydrateFormFromVideo () {
143 this.form.patchValue(this.video.toFormPatch())
148 name: 'thumbnailfile'
156 for (const obj of objects) {
157 fetch(this.video[obj.url])
158 .then(response => response.blob())
160 this.form.patchValue({