X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-user-settings%2Fuser-video-settings.component.ts;h=0cd889a8a3a27d885c6310070ec01ab2a5aadf26;hb=dd24f1bb0a4b252e5342b251ba36853364da7b8e;hp=ab77f6f9c3c9ba0eabd519d2e34d4e1113996830;hpb=66357162f8e1227495f09bd4f68446aad7071c6d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts b/client/src/app/shared/shared-user-settings/user-video-settings.component.ts index ab77f6f9c..0cd889a8a 100644 --- a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts +++ b/client/src/app/shared/shared-user-settings/user-video-settings.component.ts @@ -1,9 +1,9 @@ import { pick } from 'lodash-es' -import { forkJoin, Subject, Subscription } from 'rxjs' +import { 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, SelectOptionsItem } from '@app/shared/shared-forms' +import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' import { UserUpdateMe } from '@shared/models' import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' @@ -18,12 +18,9 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit, @Input() notifyOnUpdate = true @Input() userInformationLoaded: Subject - languageItems: SelectOptionsItem[] = [] defaultNSFWPolicy: NSFWPolicyType formValuesWatcher: Subscription - private allLanguagesGroup: string - constructor ( protected formValidatorService: FormValidatorService, private authService: AuthService, @@ -35,10 +32,6 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit, } ngOnInit () { - this.allLanguagesGroup = $localize`All languages` - - let oldForm: any - this.buildForm({ nsfwPolicy: null, webTorrentEnabled: null, @@ -47,42 +40,23 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit, videoLanguages: null }) - forkJoin([ - this.serverService.getVideoLanguages(), - this.serverService.getConfig(), - this.userInformationLoaded.pipe(first()) - ]).subscribe(([ languages, config ]) => { - 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 } ] - - this.defaultNSFWPolicy = config.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) { - 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 ]) - }) - } - }) + this.userInformationLoaded.pipe(first()) + .subscribe( + () => { + 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: this.user.videoLanguages + }) + + if (this.reactiveUpdate) this.handleReactiveUpdate() + } + ) } ngOnDestroy () { @@ -90,30 +64,18 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit, } updateDetails (onlyKeys?: string[]) { - const nsfwPolicy = this.form.value[ 'nsfwPolicy' ] + 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 videoLanguages: string[] = this.form.value['videoLanguages'] + const videoLanguages = this.form.value['videoLanguages'] if (Array.isArray(videoLanguages)) { if (videoLanguages.length > 20) { this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`) return } - - if (videoLanguages.length === 0) { - this.notifier.error($localize`You need to enable at least 1 video language.`) - return - } - - if ( - videoLanguages.length === this.languageItems.length || - (videoLanguages.length === 1 && videoLanguages[0] === this.allLanguagesGroup) - ) { - videoLanguages = null // null means "All" - } } let details: UserUpdateMe = { @@ -124,21 +86,47 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit, videoLanguages } + if (videoLanguages) { + details = Object.assign(details, videoLanguages) + } + if (onlyKeys) details = pick(details, onlyKeys) if (this.authService.isLoggedIn()) { - this.userService.updateMyProfile(details).subscribe( - () => { + return this.updateLoggedProfile(details) + } + + return this.updateAnonymousProfile(details) + } + + 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.`) }, - err => this.notifier.error(err.message) - ) - } else { - this.userService.updateMyAnonymousProfile(details) - if (this.notifyOnUpdate) this.notifier.success($localize`Display/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.`) } }