94479321d42ca9746601839f2e3164c45b9980b4
[oweals/peertube.git] / client / src / app / videos / +video-edit / video-add-components / video-send.ts
1 import { catchError, switchMap, tap } from 'rxjs/operators'
2 import { EventEmitter, OnInit } from '@angular/core'
3 import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
4 import { populateAsyncUserVideoChannels } from '@app/helpers'
5 import { FormReactive } from '@app/shared/shared-forms'
6 import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { ServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'
9
10 export abstract class VideoSend extends FormReactive implements OnInit {
11   userVideoChannels: { id: number, label: string, support: string }[] = []
12   videoPrivacies: VideoConstant<VideoPrivacy>[] = []
13   videoCaptions: VideoCaptionEdit[] = []
14
15   firstStepPrivacyId = 0
16   firstStepChannelId = 0
17
18   abstract firstStepDone: EventEmitter<string>
19   abstract firstStepError: EventEmitter<void>
20   protected abstract readonly DEFAULT_VIDEO_PRIVACY: VideoPrivacy
21
22   protected loadingBar: LoadingBarService
23   protected notifier: Notifier
24   protected authService: AuthService
25   protected serverService: ServerService
26   protected videoService: VideoService
27   protected videoCaptionService: VideoCaptionService
28   protected serverConfig: ServerConfig
29
30   abstract canDeactivate (): CanComponentDeactivateResult
31
32   ngOnInit () {
33     this.buildForm({})
34
35     populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
36       .then(() => this.firstStepChannelId = this.userVideoChannels[ 0 ].id)
37
38     this.serverConfig = this.serverService.getTmpConfig()
39     this.serverService.getConfig()
40         .subscribe(config => this.serverConfig = config)
41
42     this.serverService.getVideoPrivacies()
43         .subscribe(
44           privacies => {
45             this.videoPrivacies = privacies
46
47             this.firstStepPrivacyId = this.DEFAULT_VIDEO_PRIVACY
48           })
49   }
50
51   checkForm () {
52     this.forceCheck()
53
54     return this.form.valid
55   }
56
57   protected updateVideoAndCaptions (video: VideoEdit) {
58     this.loadingBar.start()
59
60     return this.videoService.updateVideo(video)
61         .pipe(
62           // Then update captions
63           switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
64           tap(() => this.loadingBar.complete()),
65           catchError(err => {
66             this.loadingBar.complete()
67             throw err
68           })
69         )
70   }
71 }