1 import { Component, Input, OnInit, OnDestroy } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { UserUpdateMe } from '../../../../../../shared/models/users'
4 import { User, UserService } from '@app/shared/users'
5 import { AuthService } from '../../../core'
6 import { FormReactive } from '@app/shared/forms/form-reactive'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { forkJoin, Subject, Subscription } from 'rxjs'
10 import { SelectItem } from 'primeng/api'
11 import { first } from 'rxjs/operators'
12 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
13 import { pick } from 'lodash-es'
16 selector: 'my-account-video-settings',
17 templateUrl: './my-account-video-settings.component.html',
18 styleUrls: [ './my-account-video-settings.component.scss' ]
20 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
21 @Input() user: User = null
22 @Input() reactiveUpdate = false
23 @Input() notifyOnUpdate = true
24 @Input() userInformationLoaded: Subject<any>
26 languageItems: SelectItem[] = []
27 defaultNSFWPolicy: NSFWPolicyType
28 formValuesWatcher: Subscription
31 protected formValidatorService: FormValidatorService,
32 private authService: AuthService,
33 private notifier: Notifier,
34 private userService: UserService,
35 private serverService: ServerService,
46 webTorrentEnabled: null,
48 autoPlayNextVideo: null,
53 this.serverService.getVideoLanguages(),
54 this.serverService.getConfig(),
55 this.userInformationLoaded.pipe(first())
56 ]).subscribe(([ languages, config ]) => {
57 this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
58 this.languageItems = this.languageItems
59 .concat(languages.map(l => ({ label: l.label, value: l.id })))
61 const videoLanguages = this.user.videoLanguages
62 ? this.user.videoLanguages
63 : this.languageItems.map(l => l.value)
65 this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
67 this.form.patchValue({
68 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
69 webTorrentEnabled: this.user.webTorrentEnabled,
70 autoPlayVideo: this.user.autoPlayVideo === true,
71 autoPlayNextVideo: this.user.autoPlayNextVideo,
75 if (this.reactiveUpdate) {
76 oldForm = { ...this.form.value }
77 this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
78 const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
79 oldForm = { ...this.form.value }
80 this.updateDetails([updatedKey])
87 this.formValuesWatcher?.unsubscribe()
90 updateDetails (onlyKeys?: string[]) {
91 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
92 const webTorrentEnabled = this.form.value['webTorrentEnabled']
93 const autoPlayVideo = this.form.value['autoPlayVideo']
94 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
96 let videoLanguages: string[] = this.form.value['videoLanguages']
97 if (Array.isArray(videoLanguages)) {
98 if (videoLanguages.length === this.languageItems.length) {
99 videoLanguages = null // null means "All"
100 } else if (videoLanguages.length > 20) {
101 this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
103 } else if (videoLanguages.length === 0) {
104 this.notifier.error('You need to enabled at least 1 video language.')
109 let details: UserUpdateMe = {
117 if (onlyKeys) details = pick(details, onlyKeys)
119 if (this.authService.isLoggedIn()) {
120 this.userService.updateMyProfile(details).subscribe(
122 this.authService.refreshUserInformation()
124 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
127 err => this.notifier.error(err.message)
130 this.userService.updateMyAnonymousProfile(details)
131 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
135 getDefaultVideoLanguageLabel () {
136 return this.i18n('No language')
139 getSelectedVideoLanguageLabel () {
140 return this.i18n('{{\'{0} languages selected')