99eee23b8d6fe53bbcf6b4e938af5c477d27d680
[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 { forkJoin, Subject } from 'rxjs'
9 import { SelectItem } from 'primeng/api'
10 import { first } 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       autoPlayNextVideo: null,
40       videoLanguages: null
41     })
42
43     forkJoin([
44       this.serverService.videoLanguagesLoaded.pipe(first()),
45       this.userInformationLoaded.pipe(first())
46     ]).subscribe(() => {
47       const languages = this.serverService.getVideoLanguages()
48
49       this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ]
50       this.languageItems = this.languageItems
51                                .concat(languages.map(l => ({ label: l.label, value: l.id })))
52
53       const videoLanguages = this.user.videoLanguages
54         ? this.user.videoLanguages
55         : this.languageItems.map(l => l.value)
56
57       this.form.patchValue({
58         nsfwPolicy: this.user.nsfwPolicy,
59         webTorrentEnabled: this.user.webTorrentEnabled,
60         autoPlayVideo: this.user.autoPlayVideo === true,
61         autoPlayNextVideo: this.user.autoPlayNextVideo,
62         videoLanguages
63       })
64     })
65   }
66
67   updateDetails () {
68     const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
69     const webTorrentEnabled = this.form.value['webTorrentEnabled']
70     const autoPlayVideo = this.form.value['autoPlayVideo']
71     const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
72
73     let videoLanguages: string[] = this.form.value['videoLanguages']
74     if (Array.isArray(videoLanguages)) {
75       if (videoLanguages.length === this.languageItems.length) {
76         videoLanguages = null // null means "All"
77       } else if (videoLanguages.length > 20) {
78         this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.')
79         return
80       } else if (videoLanguages.length === 0) {
81         this.notifier.error('You need to enabled at least 1 video language.')
82         return
83       }
84     }
85
86     const details: UserUpdateMe = {
87       nsfwPolicy,
88       webTorrentEnabled,
89       autoPlayVideo,
90       autoPlayNextVideo,
91       videoLanguages
92     }
93
94     this.userService.updateMyProfile(details).subscribe(
95       () => {
96         this.notifier.success(this.i18n('Video settings updated.'))
97
98         this.authService.refreshUserInformation()
99       },
100
101       err => this.notifier.error(err.message)
102     )
103   }
104
105   getDefaultVideoLanguageLabel () {
106     return this.i18n('No language')
107   }
108
109   getSelectedVideoLanguageLabel () {
110     return this.i18n('{{\'{0} languages selected')
111   }
112 }