77febf1796e4796d80b081b6e9791c0b740e43c7
[oweals/peertube.git] /
1 import { Component, Input, OnInit } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { UserUpdateMe } from '../../../../../../shared'
4 import { AuthService } from '../../../core'
5 import { FormReactive, User, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { Subject } from 'rxjs'
9 import { SelectItem } from 'primeng/api'
10 import { switchMap } from 'rxjs/operators'
11
12 @Component({
13   selector: 'my-account-video-settings',
14   templateUrl: './my-account-video-settings.component.html',
15   styleUrls: [ './my-account-video-settings.component.scss' ]
16 })
17 export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit {
18   @Input() user: User = null
19   @Input() userInformationLoaded: Subject<any>
20
21   languageItems: SelectItem[] = []
22
23   constructor (
24     protected formValidatorService: FormValidatorService,
25     private authService: AuthService,
26     private notifier: Notifier,
27     private userService: UserService,
28     private serverService: ServerService,
29     private i18n: I18n
30   ) {
31     super()
32   }
33
34   ngOnInit () {
35     this.buildForm({
36       nsfwPolicy: null,
37       webTorrentEnabled: null,
38       autoPlayVideo: null,
39       videoLanguages: null
40     })
41
42     this.serverService.videoLanguagesLoaded
43         .pipe(switchMap(() => this.userInformationLoaded))
44         .subscribe(() => {
45           const languages = this.serverService.getVideoLanguages()
46
47           this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
48           this.languageItems = this.languageItems
49                                    .concat(languages.map(l => ({ label: l.label, value: l.id })))
50
51           const videoLanguages = this.user.videoLanguages
52             ? this.user.videoLanguages
53             : this.languageItems.map(l => l.value)
54
55           this.form.patchValue({
56             nsfwPolicy: this.user.nsfwPolicy,
57             webTorrentEnabled: this.user.webTorrentEnabled,
58             autoPlayVideo: this.user.autoPlayVideo === true,
59             videoLanguages
60           })
61         })
62   }
63
64   updateDetails () {
65     const nsfwPolicy = this.form.value['nsfwPolicy']
66     const webTorrentEnabled = this.form.value['webTorrentEnabled']
67     const autoPlayVideo = this.form.value['autoPlayVideo']
68
69     let videoLanguages: string[] = this.form.value['videoLanguages']
70     if (Array.isArray(videoLanguages)) {
71       if (videoLanguages.length === this.languageItems.length) {
72         videoLanguages = null // null means "All"
73       } else if (videoLanguages.length > 20) {
74         this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
75         return
76       } else if (videoLanguages.length === 0) {
77         this.notifier.error('You need to enabled at least 1 video language.')
78         return
79       }
80     }
81
82     const details: UserUpdateMe = {
83       nsfwPolicy,
84       webTorrentEnabled,
85       autoPlayVideo,
86       videoLanguages
87     }
88
89     this.userService.updateMyProfile(details).subscribe(
90       () => {
91         this.notifier.success(this.i18n('Video settings updated.'))
92
93         this.authService.refreshUserInformation()
94       },
95
96       err => this.notifier.error(err.message)
97     )
98   }
99
100   getDefaultVideoLanguageLabel () {
101     return this.i18n('No language')
102   }
103
104   getSelectedVideoLanguageLabel () {
105     return this.i18n('{{\'{0} languages selected')
106   }
107 }