]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Don't leak unlisted videos
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import 'rxjs/add/observable/forkJoin'
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 { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
11 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
14
15 @Component({
16 selector: 'my-videos-update',
17 styleUrls: [ './shared/video-edit.component.scss' ],
18 templateUrl: './video-update.component.html'
19 })
20
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
22 video: VideoEdit
23
24 form: FormGroup
25 formErrors: { [ id: string ]: string } = {}
26 validationMessages: ValidatorMessage = {}
27 videoPrivacies = []
28 userVideoChannels = []
29
30 constructor (
31 private formBuilder: FormBuilder,
32 private route: ActivatedRoute,
33 private router: Router,
34 private notificationsService: NotificationsService,
35 private serverService: ServerService,
36 private videoService: VideoService,
37 private authService: AuthService
38 ) {
39 super()
40 }
41
42 buildForm () {
43 this.form = this.formBuilder.group({})
44 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
45 }
46
47 ngOnInit () {
48 this.buildForm()
49
50 this.serverService.videoPrivaciesLoaded
51 .subscribe(
52 () => this.videoPrivacies = this.serverService.getVideoPrivacies()
53 )
54
55 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
56
57 const uuid: string = this.route.snapshot.params['uuid']
58 this.videoService.getVideo(uuid)
59 .switchMap(video => {
60 return this.videoService
61 .loadCompleteDescription(video.descriptionPath)
62 .do(description => video.description = description)
63 .map(() => video)
64 })
65 .subscribe(
66 video => {
67 this.video = new VideoEdit(video)
68
69 // We cannot set private a video that was not private
70 if (video.privacy !== VideoPrivacy.PRIVATE) {
71 const newVideoPrivacies = []
72 for (const p of this.videoPrivacies) {
73 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
74 }
75
76 this.videoPrivacies = newVideoPrivacies
77 }
78
79 this.hydrateFormFromVideo()
80 },
81
82 err => {
83 console.error(err)
84 this.notificationsService.error('Error', err.message)
85 }
86 )
87 }
88
89 checkForm () {
90 this.forceCheck()
91
92 return this.form.valid
93 }
94
95 update () {
96 if (this.checkForm() === false) {
97 return
98 }
99
100 this.video.patch(this.form.value)
101
102 this.videoService.updateVideo(this.video)
103 .subscribe(
104 () => {
105 this.notificationsService.success('Success', 'Video updated.')
106 this.router.navigate([ '/videos/watch', this.video.uuid ])
107 },
108
109 err => {
110 this.notificationsService.error('Error', err.message)
111 console.error(err)
112 }
113 )
114
115 }
116
117 private hydrateFormFromVideo () {
118 this.form.patchValue(this.video.toJSON())
119 }
120 }