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 { 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'
18 selector: 'my-videos-update',
19 styleUrls: [ './shared/video-edit.component.scss' ],
20 templateUrl: './video-update.component.html'
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
25 isUpdatingVideo = false
27 userVideoChannels = []
28 schedulePublicationPossible = false
32 protected formValidatorService: FormValidatorService,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notificationsService: NotificationsService,
36 private serverService: ServerService,
37 private videoService: VideoService,
38 private authService: AuthService,
39 private loadingBar: LoadingBarService,
40 private videoChannelService: VideoChannelService,
41 private videoCaptionService: VideoCaptionService,
50 this.serverService.videoPrivaciesLoaded
51 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
53 const uuid: string = this.route.snapshot.params[ 'uuid' ]
54 this.videoService.getVideo(uuid)
57 return this.videoService
58 .loadCompleteDescription(video.descriptionPath)
59 .pipe(map(description => Object.assign(video, { description })))
62 return this.videoChannelService
63 .listAccountVideoChannels(video.account)
65 map(result => result.data),
66 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
67 map(videoChannels => ({ video, videoChannels }))
70 switchMap(({ video, videoChannels }) => {
71 return this.videoCaptionService
72 .listCaptions(video.id)
74 map(result => result.data),
75 map(videoCaptions => ({ video, videoChannels, videoCaptions }))
80 ({ video, videoChannels, videoCaptions }) => {
81 this.video = new VideoEdit(video)
82 this.userVideoChannels = videoChannels
83 this.videoCaptions = videoCaptions
85 // We cannot set private a video that was not private
86 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
87 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
88 } else { // We can schedule video publication only if it it is private
89 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
92 this.hydrateFormFromVideo()
97 this.notificationsService.error(this.i18n('Error'), err.message)
105 return this.form.valid
109 if (this.checkForm() === false) {
113 this.video.patch(this.form.value)
115 this.loadingBar.start()
116 this.isUpdatingVideo = true
119 this.videoService.updateVideo(this.video)
121 // Then update captions
122 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
126 this.isUpdatingVideo = false
127 this.loadingBar.complete()
128 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
129 this.router.navigate([ '/videos/watch', this.video.uuid ])
133 this.isUpdatingVideo = false
134 this.notificationsService.error(this.i18n('Error'), err.message)
141 private hydrateFormFromVideo () {
142 this.form.patchValue(this.video.toFormPatch())
147 name: 'thumbnailfile'
155 for (const obj of objects) {
156 fetch(this.video[obj.url])
157 .then(response => response.blob())
159 this.form.patchValue({