1 import { catchError, switchMap, tap } from 'rxjs/operators'
2 import { SelectChannelItem } from 'src/types/select-options-item.model'
3 import { Directive, EventEmitter, OnInit } from '@angular/core'
4 import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
5 import { listUserChannels } from '@app/helpers'
6 import { FormReactive } from '@app/shared/shared-forms'
7 import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { HTMLServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'
12 // eslint-disable-next-line @angular-eslint/directive-class-suffix
13 export abstract class VideoSend extends FormReactive implements OnInit {
14 userVideoChannels: SelectChannelItem[] = []
15 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
16 videoCaptions: VideoCaptionEdit[] = []
18 firstStepPrivacyId: VideoPrivacy
19 firstStepChannelId: number
21 abstract firstStepDone: EventEmitter<string>
22 abstract firstStepError: EventEmitter<void>
24 protected loadingBar: LoadingBarService
25 protected notifier: Notifier
26 protected authService: AuthService
28 protected serverService: ServerService
29 protected videoService: VideoService
30 protected videoCaptionService: VideoCaptionService
32 protected serverConfig: HTMLServerConfig
34 protected highestPrivacy: VideoPrivacy
36 abstract canDeactivate (): CanComponentDeactivateResult
41 listUserChannels(this.authService)
42 .subscribe(channels => {
43 this.userVideoChannels = channels
44 this.firstStepChannelId = this.userVideoChannels[0].id
47 this.serverConfig = this.serverService.getHTMLConfig()
49 this.serverService.getVideoPrivacies()
52 const defaultPrivacy = this.serverConfig.defaults.publish.privacy
54 const { videoPrivacies, defaultPrivacyId } = this.videoService.explainedPrivacyLabels(privacies, defaultPrivacy)
56 this.videoPrivacies = videoPrivacies
57 this.firstStepPrivacyId = defaultPrivacyId
59 this.highestPrivacy = this.videoService.getHighestAvailablePrivacy(privacies)
63 protected updateVideoAndCaptions (video: VideoEdit) {
64 this.loadingBar.useRef().start()
66 return this.videoService.updateVideo(video)
68 // Then update captions
69 switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
70 tap(() => this.loadingBar.useRef().complete()),
72 this.loadingBar.useRef().complete()
78 protected async isFormValid () {
79 await this.waitPendingCheck()
82 return this.form.valid