]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Use a resolver when updating the video
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
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'
17
18 @Component({
19 selector: 'my-videos-update',
20 styleUrls: [ './shared/video-edit.component.scss' ],
21 templateUrl: './video-update.component.html'
22 })
23 export class VideoUpdateComponent extends FormReactive implements OnInit {
24 video: VideoEdit
25
26 isUpdatingVideo = false
27 videoPrivacies: VideoConstant<string>[] = []
28 userVideoChannels: { id: number, label: string, support: string }[] = []
29 schedulePublicationPossible = false
30 videoCaptions: VideoCaptionEdit[] = []
31
32 constructor (
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,
43 private i18n: I18n
44 ) {
45 super()
46 }
47
48 ngOnInit () {
49 this.buildForm({})
50
51 this.serverService.videoPrivaciesLoaded
52 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
53
54 this.route.data
55 .pipe(map(data => data.videoData))
56 .subscribe(({ video, videoChannels, videoCaptions }) => {
57 this.video = new VideoEdit(video)
58 this.userVideoChannels = videoChannels
59 this.videoCaptions = videoCaptions
60
61 // We cannot set private a video that was not private
62 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
63 this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString())
64 } else { // We can schedule video publication only if it it is private
65 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
66 }
67
68 // FIXME: Angular does not detec
69 setTimeout(() => this.hydrateFormFromVideo())
70 },
71
72 err => {
73 console.error(err)
74 this.notificationsService.error(this.i18n('Error'), err.message)
75 }
76 )
77 }
78
79 checkForm () {
80 this.forceCheck()
81
82 return this.form.valid
83 }
84
85 update () {
86 if (this.checkForm() === false) {
87 return
88 }
89
90 this.video.patch(this.form.value)
91
92 this.loadingBar.start()
93 this.isUpdatingVideo = true
94
95 // Update the video
96 this.videoService.updateVideo(this.video)
97 .pipe(
98 // Then update captions
99 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
100 )
101 .subscribe(
102 () => {
103 this.isUpdatingVideo = false
104 this.loadingBar.complete()
105 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
106 this.router.navigate([ '/videos/watch', this.video.uuid ])
107 },
108
109 err => {
110 this.isUpdatingVideo = false
111 this.notificationsService.error(this.i18n('Error'), err.message)
112 console.error(err)
113 }
114 )
115
116 }
117
118 private hydrateFormFromVideo () {
119 this.form.patchValue(this.video.toFormPatch())
120
121 const objects = [
122 {
123 url: 'thumbnailUrl',
124 name: 'thumbnailfile'
125 },
126 {
127 url: 'previewUrl',
128 name: 'previewfile'
129 }
130 ]
131
132 for (const obj of objects) {
133 fetch(this.video[obj.url])
134 .then(response => response.blob())
135 .then(data => {
136 this.form.patchValue({
137 [ obj.name ]: data
138 })
139 })
140 }
141 }
142 }