aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-user-settings/user-video-settings.component.ts
blob: 4aac60c2b4acb2624707e8c0f24e99cd7826691b (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { pick } from 'lodash-es'
import { forkJoin, Subject, Subscription } from 'rxjs'
import { first } from 'rxjs/operators'
import { Component, Input, OnDestroy, OnInit } from '@angular/core'
import { AuthService, Notifier, ServerService, User, UserService } from '@app/core'
import { FormReactive, FormValidatorService, ItemSelectCheckboxValue } from '@app/shared/shared-forms'
import { UserUpdateMe } from '@shared/models'
import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
import { SelectOptionsItem } from '../../../types/select-options-item.model'

@Component({
  selector: 'my-user-video-settings',
  templateUrl: './user-video-settings.component.html',
  styleUrls: [ './user-video-settings.component.scss' ]
})
export class UserVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
  @Input() user: User = null
  @Input() reactiveUpdate = false
  @Input() notifyOnUpdate = true
  @Input() userInformationLoaded: Subject<any>

  languageItems: SelectOptionsItem[] = []
  defaultNSFWPolicy: NSFWPolicyType
  formValuesWatcher: Subscription

  private allLanguagesGroup: string

  constructor (
    protected formValidatorService: FormValidatorService,
    private authService: AuthService,
    private notifier: Notifier,
    private userService: UserService,
    private serverService: ServerService
  ) {
    super()
  }

  ngOnInit () {
    this.allLanguagesGroup = $localize`All languages`

    this.buildForm({
      nsfwPolicy: null,
      webTorrentEnabled: null,
      autoPlayVideo: null,
      autoPlayNextVideo: null,
      videoLanguages: null
    })

    forkJoin([
      this.serverService.getVideoLanguages(),
      this.userInformationLoaded.pipe(first())
    ]).subscribe(([ languages ]) => {
      const group = this.allLanguagesGroup

      this.languageItems = [ { label: $localize`Unknown language`, id: '_unknown', group } ]
      this.languageItems = this.languageItems
                               .concat(languages.map(l => ({ label: l.label, id: l.id, group })))

      const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages
        ? this.user.videoLanguages.map(l => ({ id: l }))
        : [ { group } ]

      const serverConfig = this.serverService.getHTMLConfig()
      this.defaultNSFWPolicy = serverConfig.instance.defaultNSFWPolicy

      this.form.patchValue({
        nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
        webTorrentEnabled: this.user.webTorrentEnabled,
        autoPlayVideo: this.user.autoPlayVideo === true,
        autoPlayNextVideo: this.user.autoPlayNextVideo,
        videoLanguages
      })

      if (this.reactiveUpdate) this.handleReactiveUpdate()
    })
  }

  ngOnDestroy () {
    this.formValuesWatcher?.unsubscribe()
  }

  updateDetails (onlyKeys?: string[]) {
    const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
    const webTorrentEnabled = this.form.value['webTorrentEnabled']
    const autoPlayVideo = this.form.value['autoPlayVideo']
    const autoPlayNextVideo = this.form.value['autoPlayNextVideo']

    let videoLanguagesForm = this.form.value['videoLanguages']

    if (Array.isArray(videoLanguagesForm)) {
      if (videoLanguagesForm.length > 20) {
        this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`)
        return
      }

      // Automatically use "All languages" if the user did not select any language
      if (videoLanguagesForm.length === 0) {
        videoLanguagesForm = [ this.allLanguagesGroup ]
        this.form.patchValue({ videoLanguages: [ { group: this.allLanguagesGroup } ] })
      }
    }

    const videoLanguages = this.buildLanguagesFromForm(videoLanguagesForm)

    let details: UserUpdateMe = {
      nsfwPolicy,
      webTorrentEnabled,
      autoPlayVideo,
      autoPlayNextVideo,
      videoLanguages
    }

    if (videoLanguages) {
      details = Object.assign(details, videoLanguages)
    }

    if (onlyKeys) details = pick(details, onlyKeys)

    if (this.authService.isLoggedIn()) {
      return this.updateLoggedProfile(details)
    }

    return this.updateAnonymousProfile(details)
  }

  private buildLanguagesFromForm (videoLanguages: ItemSelectCheckboxValue[]) {
    if (!Array.isArray(videoLanguages)) return undefined

    // null means "All"
    if (videoLanguages.length === this.languageItems.length) return null

    if (videoLanguages.length === 1) {
      const videoLanguage = videoLanguages[0]

      if (typeof videoLanguage === 'string') {
        if (videoLanguage === this.allLanguagesGroup) return null
      } else {
        if (videoLanguage.group === this.allLanguagesGroup) return null
      }
    }

    return videoLanguages.map(l => {
      if (typeof l === 'string') return l

      if (l.group) return l.group

      return l.id + ''
    })
  }

  private handleReactiveUpdate () {
    let oldForm = { ...this.form.value }

    this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
      const updatedKey = Object.keys(formValue)
                               .find(k => formValue[k] !== oldForm[k])

      oldForm = { ...this.form.value }

      this.updateDetails([ updatedKey ])
    })
  }

  private updateLoggedProfile (details: UserUpdateMe) {
    this.userService.updateMyProfile(details)
      .subscribe({
        next: () => {
          this.authService.refreshUserInformation()

          if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
        },

        error: err => this.notifier.error(err.message)
      })
  }

  private updateAnonymousProfile (details: UserUpdateMe) {
    this.userService.updateMyAnonymousProfile(details)
    if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`)
  }
}