]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add.component.ts
Add ability to disable video comments
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { AuthService, ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
10 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
11 import { VideoEdit } from '../../shared/video/video-edit.model'
12 import { VideoService } from '../../shared/video/video.service'
13
14 @Component({
15 selector: 'my-videos-add',
16 templateUrl: './video-add.component.html',
17 styleUrls: [
18 './shared/video-edit.component.scss',
19 './video-add.component.scss'
20 ]
21 })
22
23 export class VideoAddComponent extends FormReactive implements OnInit {
24 @ViewChild('videofileInput') videofileInput
25
26 isUploadingVideo = false
27 videoUploaded = false
28 videoUploadPercents = 0
29 videoUploadedIds = {
30 id: 0,
31 uuid: ''
32 }
33
34 error: string = null
35 form: FormGroup
36 formErrors: { [ id: string ]: string } = {}
37 validationMessages: ValidatorMessage = {}
38
39 userVideoChannels = []
40 videoPrivacies = []
41 firstStepPrivacyId = 0
42 firstStepChannelId = 0
43
44 constructor (
45 private formBuilder: FormBuilder,
46 private router: Router,
47 private notificationsService: NotificationsService,
48 private authService: AuthService,
49 private serverService: ServerService,
50 private videoService: VideoService
51 ) {
52 super()
53 }
54
55 buildForm () {
56 this.form = this.formBuilder.group({})
57 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
58 }
59
60 ngOnInit () {
61 this.buildForm()
62
63 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
64 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
65
66 this.serverService.videoPrivaciesLoaded
67 .subscribe(
68 () => {
69 this.videoPrivacies = this.serverService.getVideoPrivacies()
70
71 // Public by default
72 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
73 })
74 }
75
76 fileChange () {
77 this.uploadFirstStep()
78 }
79
80 checkForm () {
81 this.forceCheck()
82
83 return this.form.valid
84 }
85
86 uploadFirstStep () {
87 const videofile = this.videofileInput.nativeElement.files[0]
88 const name = videofile.name.replace(/\.[^/.]+$/, '')
89 const privacy = this.firstStepPrivacyId.toString()
90 const nsfw = false
91 const commentsEnabled = true
92 const channelId = this.firstStepChannelId.toString()
93
94 const formData = new FormData()
95 formData.append('name', name)
96 // Put the video "private" -> we wait he validates the second step
97 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
98 formData.append('nsfw', '' + nsfw)
99 formData.append('commentsEnabled', '' + commentsEnabled)
100 formData.append('channelId', '' + channelId)
101 formData.append('videofile', videofile)
102
103 this.isUploadingVideo = true
104 this.form.patchValue({
105 name,
106 privacy,
107 nsfw,
108 channelId
109 })
110
111 this.videoService.uploadVideo(formData).subscribe(
112 event => {
113 if (event.type === HttpEventType.UploadProgress) {
114 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
115 } else if (event instanceof HttpResponse) {
116 console.log('Video uploaded.')
117
118 this.videoUploaded = true
119
120 this.videoUploadedIds = event.body.video
121 }
122 },
123
124 err => {
125 // Reset progress
126 this.videoUploadPercents = 0
127 this.error = err.message
128 }
129 )
130 }
131
132 updateSecondStep () {
133 if (this.checkForm() === false) {
134 return
135 }
136
137 const video = new VideoEdit()
138 video.patch(this.form.value)
139 video.channel = this.firstStepChannelId
140 video.id = this.videoUploadedIds.id
141 video.uuid = this.videoUploadedIds.uuid
142
143 this.videoService.updateVideo(video)
144 .subscribe(
145 () => {
146 this.notificationsService.success('Success', 'Video published.')
147 this.router.navigate([ '/videos/watch', video.uuid ])
148 },
149
150 err => {
151 this.error = 'Cannot update the video.'
152 console.error(err)
153 }
154 )
155
156 }
157 }