]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Fix video channel update with an admin account
[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
C
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { ActivatedRoute, Router } from '@angular/router'
68e24d72 5import { LoadingBarService } from '@ngx-loading-bar/core'
df98563e 6import { NotificationsService } from 'angular2-notifications'
63c4db6d 7import { VideoPrivacy } from '../../../../../shared/models/videos'
db7af09b 8import { ServerService } from '../../core'
15a7387d 9import { AuthService } from '../../core/auth'
4cc66133 10import { FormReactive } from '../../shared'
63c4db6d 11import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
202f6b6c 12import { VideoEdit } from '../../shared/video/video-edit.model'
4cc66133 13import { VideoService } from '../../shared/video/video.service'
6200d8d9 14import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
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
df98563e 25 form: FormGroup
ff249f49
C
26 formErrors: { [ id: string ]: string } = {}
27 validationMessages: ValidatorMessage = {}
28 videoPrivacies = []
15a7387d 29 userVideoChannels = []
dc8bc31b 30
df98563e 31 constructor (
4b2f33f3 32 private formBuilder: FormBuilder,
d8e689b8 33 private route: ActivatedRoute,
7ddd02c9 34 private router: Router,
6e07c3de 35 private notificationsService: NotificationsService,
db7af09b 36 private serverService: ServerService,
15a7387d 37 private videoService: VideoService,
68e24d72 38 private authService: AuthService,
6200d8d9
C
39 private loadingBar: LoadingBarService,
40 private videoChannelService: VideoChannelService
4b2f33f3 41 ) {
df98563e 42 super()
4b2f33f3 43 }
dc8bc31b 44
df98563e 45 buildForm () {
ff249f49 46 this.form = this.formBuilder.group({})
df98563e 47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
48 }
49
df98563e
C
50 ngOnInit () {
51 this.buildForm()
d8e689b8 52
15a7387d 53 this.serverService.videoPrivaciesLoaded
6de36768 54 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
15a7387d 55
db400f44 56 const uuid: string = this.route.snapshot.params[ 'uuid' ]
2de96f4d 57 this.videoService.getVideo(uuid)
db400f44
C
58 .pipe(
59 switchMap(video => {
60 return this.videoService
61 .loadCompleteDescription(video.descriptionPath)
62 .pipe(map(description => Object.assign(video, { description })))
6200d8d9
C
63 }),
64 switchMap(video => {
65 return this.videoChannelService
66 .listAccountVideoChannels(video.account.id)
67 .pipe(
68 map(result => result.data),
69 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName }))),
70 map(videoChannels => ({ video, videoChannels }))
71 )
db400f44
C
72 })
73 )
74 .subscribe(
6200d8d9 75 ({ video, videoChannels }) => {
db400f44 76 this.video = new VideoEdit(video)
6200d8d9 77 this.userVideoChannels = videoChannels
db400f44
C
78
79 // We cannot set private a video that was not private
80 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
81 const newVideoPrivacies = []
82 for (const p of this.videoPrivacies) {
83 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
84 }
85
86 this.videoPrivacies = newVideoPrivacies
fd45e8f4
C
87 }
88
db400f44
C
89 this.hydrateFormFromVideo()
90 },
2de96f4d 91
db400f44
C
92 err => {
93 console.error(err)
94 this.notificationsService.error('Error', err.message)
95 }
96 )
e822fdae
C
97 }
98
df98563e
C
99 checkForm () {
100 this.forceCheck()
c24ac1c1 101
df98563e 102 return this.form.valid
c24ac1c1
C
103 }
104
df98563e 105 update () {
c24ac1c1 106 if (this.checkForm() === false) {
df98563e 107 return
c24ac1c1
C
108 }
109
df98563e 110 this.video.patch(this.form.value)
d8e689b8 111
68e24d72
C
112 this.loadingBar.start()
113 this.isUpdatingVideo = true
d8e689b8
C
114 this.videoService.updateVideo(this.video)
115 .subscribe(
116 () => {
68e24d72
C
117 this.isUpdatingVideo = false
118 this.loadingBar.complete()
df98563e 119 this.notificationsService.success('Success', 'Video updated.')
0a6658fd 120 this.router.navigate([ '/videos/watch', this.video.uuid ])
d8e689b8
C
121 },
122
123 err => {
68e24d72 124 this.isUpdatingVideo = false
ce5496d6 125 this.notificationsService.error('Error', err.message)
df98563e 126 console.error(err)
d8e689b8 127 }
df98563e 128 )
e822fdae 129
dc8bc31b 130 }
e54163c2 131
df98563e
C
132 private hydrateFormFromVideo () {
133 this.form.patchValue(this.video.toJSON())
6de36768
C
134
135 const objects = [
136 {
137 url: 'thumbnailUrl',
138 name: 'thumbnailfile'
139 },
140 {
141 url: 'previewUrl',
142 name: 'previewfile'
143 }
144 ]
145
146 for (const obj of objects) {
147 fetch(this.video[obj.url])
148 .then(response => response.blob())
149 .then(data => {
150 this.form.patchValue({
151 [ obj.name ]: data
152 })
153 })
154 }
d8e689b8 155 }
dc8bc31b 156}