]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
CommitLineData
db400f44 1import { map, switchMap } from 'rxjs/operators'
db7af09b 2import { Component, OnInit } from '@angular/core'
df98563e 3import { ActivatedRoute, Router } from '@angular/router'
68e24d72 4import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 5import { NotificationsService } from 'angular2-notifications'
ef4c78da 6import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
db7af09b 7import { ServerService } from '../../core'
4cc66133 8import { FormReactive } from '../../shared'
202f6b6c 9import { VideoEdit } from '../../shared/video/video-edit.model'
4cc66133 10import { VideoService } from '../../shared/video/video.service'
b1d40cff 11import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 12import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
40e87e9e 13import { VideoCaptionService } from '@app/shared/video-caption'
ef4c78da 14import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
1553e15d 15
dc8bc31b 16@Component({
d8e689b8 17 selector: 'my-videos-update',
80958c78 18 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 19 templateUrl: './video-update.component.html'
dc8bc31b 20})
d8e689b8 21export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 22 video: VideoEdit
4b2f33f3 23
68e24d72 24 isUpdatingVideo = false
8cd7faaa 25 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
ef4c78da 26 userVideoChannels: { id: number, label: string, support: string }[] = []
bbe0f064 27 schedulePublicationPossible = false
ef4c78da 28 videoCaptions: VideoCaptionEdit[] = []
dc8bc31b 29
772d5642
C
30 private updateDone = false
31
df98563e 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
d8e689b8 34 private route: ActivatedRoute,
7ddd02c9 35 private router: Router,
6e07c3de 36 private notificationsService: NotificationsService,
db7af09b 37 private serverService: ServerService,
15a7387d 38 private videoService: VideoService,
6200d8d9 39 private loadingBar: LoadingBarService,
40e87e9e 40 private videoCaptionService: VideoCaptionService,
b1d40cff 41 private i18n: I18n
4b2f33f3 42 ) {
df98563e 43 super()
4b2f33f3 44 }
dc8bc31b 45
df98563e 46 ngOnInit () {
d18d6478 47 this.buildForm({})
d8e689b8 48
15a7387d 49 this.serverService.videoPrivaciesLoaded
308c4275 50 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
15a7387d 51
308c4275
C
52 this.route.data
53 .pipe(map(data => data.videoData))
54 .subscribe(({ video, videoChannels, videoCaptions }) => {
55 this.video = new VideoEdit(video)
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
db400f44 58
308c4275
C
59 // We cannot set private a video that was not private
60 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
8cd7faaa 61 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
308c4275
C
62 } else { // We can schedule video publication only if it it is private
63 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
64 }
fd45e8f4 65
8cd7faaa
C
66 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
67
772d5642 68 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
308c4275
C
69 setTimeout(() => this.hydrateFormFromVideo())
70 },
2de96f4d 71
308c4275
C
72 err => {
73 console.error(err)
74 this.notificationsService.error(this.i18n('Error'), err.message)
75 }
76 )
e822fdae
C
77 }
78
772d5642
C
79 canDeactivate () {
80 if (this.updateDone === true) return { canDeactivate: true }
81
82 for (const caption of this.videoCaptions) {
83 if (caption.action) return { canDeactivate: false }
84 }
85
86 return { canDeactivate: this.formChanged === false }
87 }
88
df98563e
C
89 checkForm () {
90 this.forceCheck()
c24ac1c1 91
df98563e 92 return this.form.valid
c24ac1c1
C
93 }
94
df98563e 95 update () {
8c2b9756
RK
96 if (this.checkForm() === false
97 || this.isUpdatingVideo === true) {
df98563e 98 return
c24ac1c1
C
99 }
100
df98563e 101 this.video.patch(this.form.value)
d8e689b8 102
68e24d72
C
103 this.loadingBar.start()
104 this.isUpdatingVideo = true
40e87e9e
C
105
106 // Update the video
d8e689b8 107 this.videoService.updateVideo(this.video)
40e87e9e
C
108 .pipe(
109 // Then update captions
110 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
111 )
112 .subscribe(
113 () => {
772d5642 114 this.updateDone = true
40e87e9e
C
115 this.isUpdatingVideo = false
116 this.loadingBar.complete()
117 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
118 this.router.navigate([ '/videos/watch', this.video.uuid ])
119 },
120
121 err => {
0f7fedc3 122 this.loadingBar.complete()
40e87e9e
C
123 this.isUpdatingVideo = false
124 this.notificationsService.error(this.i18n('Error'), err.message)
125 console.error(err)
126 }
127 )
dc8bc31b 128 }
e54163c2 129
df98563e 130 private hydrateFormFromVideo () {
bbe0f064 131 this.form.patchValue(this.video.toFormPatch())
6de36768
C
132
133 const objects = [
134 {
135 url: 'thumbnailUrl',
136 name: 'thumbnailfile'
137 },
138 {
139 url: 'previewUrl',
140 name: 'previewfile'
141 }
142 ]
143
144 for (const obj of objects) {
145 fetch(this.video[obj.url])
146 .then(response => response.blob())
147 .then(data => {
148 this.form.patchValue({
149 [ obj.name ]: data
150 })
151 })
152 }
d8e689b8 153 }
dc8bc31b 154}