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