]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Improve video edit/update/add typings
[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
ef4c78da
C
27 videoPrivacies: VideoConstant<string>[] = []
28 userVideoChannels: { id: number, label: string, support: string }[] = []
bbe0f064 29 schedulePublicationPossible = false
ef4c78da 30 videoCaptions: VideoCaptionEdit[] = []
dc8bc31b 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,
68e24d72 39 private authService: AuthService,
6200d8d9 40 private loadingBar: LoadingBarService,
b1d40cff 41 private videoChannelService: VideoChannelService,
40e87e9e 42 private videoCaptionService: VideoCaptionService,
b1d40cff 43 private i18n: I18n
4b2f33f3 44 ) {
df98563e 45 super()
4b2f33f3 46 }
dc8bc31b 47
df98563e 48 ngOnInit () {
d18d6478 49 this.buildForm({})
d8e689b8 50
15a7387d 51 this.serverService.videoPrivaciesLoaded
6de36768 52 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
15a7387d 53
db400f44 54 const uuid: string = this.route.snapshot.params[ 'uuid' ]
2de96f4d 55 this.videoService.getVideo(uuid)
db400f44
C
56 .pipe(
57 switchMap(video => {
58 return this.videoService
59 .loadCompleteDescription(video.descriptionPath)
60 .pipe(map(description => Object.assign(video, { description })))
6200d8d9
C
61 }),
62 switchMap(video => {
63 return this.videoChannelService
ad9e39fb 64 .listAccountVideoChannels(video.account)
6200d8d9
C
65 .pipe(
66 map(result => result.data),
74af5145 67 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
6200d8d9
C
68 map(videoChannels => ({ video, videoChannels }))
69 )
40e87e9e
C
70 }),
71 switchMap(({ video, videoChannels }) => {
72 return this.videoCaptionService
73 .listCaptions(video.id)
74 .pipe(
75 map(result => result.data),
76 map(videoCaptions => ({ video, videoChannels, videoCaptions }))
77 )
db400f44
C
78 })
79 )
80 .subscribe(
40e87e9e 81 ({ video, videoChannels, videoCaptions }) => {
db400f44 82 this.video = new VideoEdit(video)
6200d8d9 83 this.userVideoChannels = videoChannels
40e87e9e 84 this.videoCaptions = videoCaptions
db400f44
C
85
86 // We cannot set private a video that was not private
bbe0f064 87 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
329d9086 88 this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString())
bbe0f064
C
89 } else { // We can schedule video publication only if it it is private
90 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
fd45e8f4
C
91 }
92
db400f44
C
93 this.hydrateFormFromVideo()
94 },
2de96f4d 95
db400f44
C
96 err => {
97 console.error(err)
b1d40cff 98 this.notificationsService.error(this.i18n('Error'), err.message)
db400f44
C
99 }
100 )
e822fdae
C
101 }
102
df98563e
C
103 checkForm () {
104 this.forceCheck()
c24ac1c1 105
df98563e 106 return this.form.valid
c24ac1c1
C
107 }
108
df98563e 109 update () {
c24ac1c1 110 if (this.checkForm() === false) {
df98563e 111 return
c24ac1c1
C
112 }
113
df98563e 114 this.video.patch(this.form.value)
d8e689b8 115
68e24d72
C
116 this.loadingBar.start()
117 this.isUpdatingVideo = true
40e87e9e
C
118
119 // Update the video
d8e689b8 120 this.videoService.updateVideo(this.video)
40e87e9e
C
121 .pipe(
122 // Then update captions
123 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
124 )
125 .subscribe(
126 () => {
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 ])
131 },
132
133 err => {
134 this.isUpdatingVideo = false
135 this.notificationsService.error(this.i18n('Error'), err.message)
136 console.error(err)
137 }
138 )
e822fdae 139
dc8bc31b 140 }
e54163c2 141
df98563e 142 private hydrateFormFromVideo () {
bbe0f064 143 this.form.patchValue(this.video.toFormPatch())
6de36768
C
144
145 const objects = [
146 {
147 url: 'thumbnailUrl',
148 name: 'thumbnailfile'
149 },
150 {
151 url: 'previewUrl',
152 name: 'previewfile'
153 }
154 ]
155
156 for (const obj of objects) {
157 fetch(this.video[obj.url])
158 .then(response => response.blob())
159 .then(data => {
160 this.form.patchValue({
161 [ obj.name ]: data
162 })
163 })
164 }
d8e689b8 165 }
dc8bc31b 166}