]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Add additional checks when importing a video
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-video-settings.component.ts
index 8b0eaea4f96856ff7e3e672e202b6f04828e3c34..7d6b69469c7c44e86d933586ae8059847a62df45 100644 (file)
@@ -1,12 +1,11 @@
 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 } 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'
-import { SelectOptionsItem } from '../../../types/select-options-item.model'
 
 @Component({
   selector: 'my-user-video-settings',
@@ -19,12 +18,9 @@ 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,
@@ -36,43 +32,31 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
   }
 
   ngOnInit () {
-    this.allLanguagesGroup = $localize`All languages`
-
     this.buildForm({
       nsfwPolicy: null,
-      webTorrentEnabled: null,
+      p2pEnabled: 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()
-    })
+    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 () {
@@ -80,31 +64,23 @@ 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 videoLanguagesForm = this.form.value['videoLanguages']
+    const videoLanguages = this.form.value['videoLanguages']
 
-    if (Array.isArray(videoLanguagesForm)) {
-      if (videoLanguagesForm.length > 20) {
+    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
       }
-
-      // 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,
+      p2pEnabled,
       autoPlayVideo,
       autoPlayNextVideo,
       videoLanguages
@@ -123,31 +99,6 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
     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 }
 
@@ -162,15 +113,16 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
   }
 
   private updateLoggedProfile (details: UserUpdateMe) {
-    this.userService.updateMyProfile(details).subscribe(
-      () => {
-        this.authService.refreshUserInformation()
+    this.userService.updateMyProfile(details)
+      .subscribe({
+        next: () => {
+          this.authService.refreshUserInformation()
 
-        if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
-      },
+          if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
+        },
 
-      err => this.notifier.error(err.message)
-    )
+        error: err => this.notifier.error(err.message)
+      })
   }
 
   private updateAnonymousProfile (details: UserUpdateMe) {