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