aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+videos/+video-edit/video-add-components/video-send.ts
blob: ce8de049d85d0b5a51c755ee36c76b3da758434c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { catchError, switchMap, tap } from 'rxjs/operators'
import { SelectChannelItem } from 'src/types/select-options-item.model'
import { Directive, EventEmitter, OnInit } from '@angular/core'
import { AuthService, CanComponentDeactivateResult, Notifier, ServerService } from '@app/core'
import { listUserChannels } from '@app/helpers'
import { FormReactive } from '@app/shared/shared-forms'
import { VideoCaptionEdit, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
import { LoadingBarService } from '@ngx-loading-bar/core'
import { HTMLServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'

@Directive()
// tslint:disable-next-line: directive-class-suffix
export abstract class VideoSend extends FormReactive implements OnInit {
  userVideoChannels: SelectChannelItem[] = []
  videoPrivacies: VideoConstant<VideoPrivacy>[] = []
  videoCaptions: VideoCaptionEdit[] = []

  firstStepPrivacyId: VideoPrivacy
  firstStepChannelId: number

  abstract firstStepDone: EventEmitter<string>
  abstract firstStepError: EventEmitter<void>

  protected loadingBar: LoadingBarService
  protected notifier: Notifier
  protected authService: AuthService

  protected serverService: ServerService
  protected videoService: VideoService
  protected videoCaptionService: VideoCaptionService

  protected serverConfig: HTMLServerConfig

  protected highestPrivacy: VideoPrivacy

  abstract canDeactivate (): CanComponentDeactivateResult

  ngOnInit () {
    this.buildForm({})

    listUserChannels(this.authService)
      .subscribe(channels => {
        this.userVideoChannels = channels
        this.firstStepChannelId = this.userVideoChannels[0].id
      })

    this.serverConfig = this.serverService.getHTMLConfig()

    this.serverService.getVideoPrivacies()
        .subscribe(
          privacies => {
            const { videoPrivacies, defaultPrivacyId } = this.videoService.explainedPrivacyLabels(privacies)

            this.videoPrivacies = videoPrivacies
            this.firstStepPrivacyId = defaultPrivacyId

            this.highestPrivacy = this.videoService.getHighestAvailablePrivacy(privacies)
          })
  }

  checkForm () {
    this.forceCheck()

    return this.form.valid
  }

  protected updateVideoAndCaptions (video: VideoEdit) {
    this.loadingBar.useRef().start()

    return this.videoService.updateVideo(video)
        .pipe(
          // Then update captions
          switchMap(() => this.videoCaptionService.updateCaptions(video.id, this.videoCaptions)),
          tap(() => this.loadingBar.useRef().complete()),
          catchError(err => {
            this.loadingBar.useRef().complete()
            throw err
          })
        )
  }
}