]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Node 12 is not supported anymore
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-video-settings.component.ts
index eb340e24dfcb904638723f12079a9ccaf96b4631..7d6b69469c7c44e86d933586ae8059847a62df45 100644 (file)
@@ -1,10 +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 { I18n } from '@ngx-translate/i18n-polyfill'
+import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
 import { UserUpdateMe } from '@shared/models'
 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
 
@@ -19,72 +18,45 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
   @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,
-    private i18n: I18n
+    private serverService: ServerService
   ) {
     super()
   }
 
   ngOnInit () {
-    this.allLanguagesGroup = this.i18n('All languages')
-
-    let oldForm: any
-
     this.buildForm({
       nsfwPolicy: null,
-      webTorrentEnabled: null,
+      p2pEnabled: null,
       autoPlayVideo: null,
       autoPlayNextVideo: null,
       videoLanguages: null
     })
 
-    forkJoin([
-      this.serverService.getVideoLanguages(),
-      this.serverService.getConfig(),
-      this.userInformationLoaded.pipe(first())
-    ]).subscribe(([ languages, config ]) => {
-      const group = this.allLanguagesGroup
-
-      this.languageItems = [ { label: this.i18n('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,
+            p2pEnabled: this.user.p2pEnabled,
+            autoPlayVideo: this.user.autoPlayVideo === true,
+            autoPlayNextVideo: this.user.autoPlayNextVideo,
+            videoLanguages: this.user.videoLanguages
+          })
+
+          if (this.reactiveUpdate) this.handleReactiveUpdate()
+        }
+      )
   }
 
   ngOnDestroy () {
@@ -92,63 +64,69 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
   }
 
   updateDetails (onlyKeys?: string[]) {
-    const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
-    const webTorrentEnabled = this.form.value['webTorrentEnabled']
+    const nsfwPolicy = this.form.value['nsfwPolicy']
+    const p2pEnabled = this.form.value['p2pEnabled']
     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(this.i18n('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.'))
-        return
-      }
-
-      if (videoLanguages.length === 0) {
-        this.notifier.error(this.i18n('You need to enable at least 1 video language.'))
+        this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`)
         return
       }
-
-      if (
-        videoLanguages.length === this.languageItems.length ||
-        (videoLanguages.length === 1 && videoLanguages[0] === this.allLanguagesGroup)
-      ) {
-        videoLanguages = null // null means "All"
-      }
     }
 
     let details: UserUpdateMe = {
       nsfwPolicy,
-      webTorrentEnabled,
+      p2pEnabled,
       autoPlayVideo,
       autoPlayNextVideo,
       videoLanguages
     }
 
+    if (videoLanguages) {
+      details = Object.assign(details, videoLanguages)
+    }
+
     if (onlyKeys) details = pick(details, onlyKeys)
 
     if (this.authService.isLoggedIn()) {
-      this.userService.updateMyProfile(details).subscribe(
-        () => {
-          this.authService.refreshUserInformation()
+      return this.updateLoggedProfile(details)
+    }
 
-          if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
-        },
+    return this.updateAnonymousProfile(details)
+  }
 
-        err => this.notifier.error(err.message)
-      )
-    } else {
-      this.userService.updateMyAnonymousProfile(details)
-      if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
-    }
+  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 ])
+    })
   }
 
-  getDefaultVideoLanguageLabel () {
-    return this.i18n('No language')
+  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)
+      })
   }
 
-  getSelectedVideoLanguageLabel () {
-    return this.i18n('{{\'{0} languages selected')
+  private updateAnonymousProfile (details: UserUpdateMe) {
+    this.userService.updateMyAnonymousProfile(details)
+    if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`)
   }
 }