]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
adapt my-select-checkbox placeholder to its context
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-video-settings.component.ts
... / ...
CommitLineData
1import { pick } from 'lodash-es'
2import { forkJoin, Subject, Subscription } from 'rxjs'
3import { first } from 'rxjs/operators'
4import { Component, Input, OnDestroy, OnInit } from '@angular/core'
5import { AuthService, Notifier, ServerService, User, UserService } from '@app/core'
6import { FormReactive, FormValidatorService, ItemSelectCheckboxValue, SelectOptionsItem } from '@app/shared/shared-forms'
7import { I18n } from '@ngx-translate/i18n-polyfill'
8import { UserUpdateMe } from '@shared/models'
9import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
10
11@Component({
12 selector: 'my-user-video-settings',
13 templateUrl: './user-video-settings.component.html',
14 styleUrls: [ './user-video-settings.component.scss' ]
15})
16export class UserVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
17 @Input() user: User = null
18 @Input() reactiveUpdate = false
19 @Input() notifyOnUpdate = true
20 @Input() userInformationLoaded: Subject<any>
21
22 languageItems: SelectOptionsItem[] = []
23 defaultNSFWPolicy: NSFWPolicyType
24 formValuesWatcher: Subscription
25
26 private allLanguagesGroup: string
27
28 constructor (
29 protected formValidatorService: FormValidatorService,
30 private authService: AuthService,
31 private notifier: Notifier,
32 private userService: UserService,
33 private serverService: ServerService,
34 private i18n: I18n
35 ) {
36 super()
37 }
38
39 ngOnInit () {
40 this.allLanguagesGroup = this.i18n('All languages')
41
42 let oldForm: any
43
44 this.buildForm({
45 nsfwPolicy: null,
46 webTorrentEnabled: null,
47 autoPlayVideo: null,
48 autoPlayNextVideo: null,
49 videoLanguages: null
50 })
51
52 forkJoin([
53 this.serverService.getVideoLanguages(),
54 this.serverService.getConfig(),
55 this.userInformationLoaded.pipe(first())
56 ]).subscribe(([ languages, config ]) => {
57 const group = this.allLanguagesGroup
58
59 this.languageItems = [ { label: this.i18n('Unknown language'), id: '_unknown', group } ]
60 this.languageItems = this.languageItems
61 .concat(languages.map(l => ({ label: l.label, id: l.id, group })))
62
63 const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages
64 ? this.user.videoLanguages.map(l => ({ id: l }))
65 : [ { group } ]
66
67 this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
68
69 this.form.patchValue({
70 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
71 webTorrentEnabled: this.user.webTorrentEnabled,
72 autoPlayVideo: this.user.autoPlayVideo === true,
73 autoPlayNextVideo: this.user.autoPlayNextVideo,
74 videoLanguages
75 })
76
77 if (this.reactiveUpdate) {
78 oldForm = { ...this.form.value }
79
80 this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
81 const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
82 oldForm = { ...this.form.value }
83
84 this.updateDetails([ updatedKey ])
85 })
86 }
87 })
88 }
89
90 ngOnDestroy () {
91 this.formValuesWatcher?.unsubscribe()
92 }
93
94 updateDetails (onlyKeys?: string[]) {
95 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
96 const webTorrentEnabled = this.form.value['webTorrentEnabled']
97 const autoPlayVideo = this.form.value['autoPlayVideo']
98 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
99
100 let videoLanguages: string[] = this.form.value['videoLanguages']
101
102 if (Array.isArray(videoLanguages)) {
103 if (videoLanguages.length > 20) {
104 this.notifier.error(this.i18n('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.'))
105 return
106 }
107
108 if (videoLanguages.length === 0) {
109 this.notifier.error(this.i18n('You need to enable at least 1 video language.'))
110 return
111 }
112
113 if (
114 videoLanguages.length === this.languageItems.length ||
115 (videoLanguages.length === 1 && videoLanguages[0] === this.allLanguagesGroup)
116 ) {
117 videoLanguages = null // null means "All"
118 }
119 }
120
121 let details: UserUpdateMe = {
122 nsfwPolicy,
123 webTorrentEnabled,
124 autoPlayVideo,
125 autoPlayNextVideo,
126 videoLanguages
127 }
128
129 if (onlyKeys) details = pick(details, onlyKeys)
130
131 if (this.authService.isLoggedIn()) {
132 this.userService.updateMyProfile(details).subscribe(
133 () => {
134 this.authService.refreshUserInformation()
135
136 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.'))
137 },
138
139 err => this.notifier.error(err.message)
140 )
141 } else {
142 this.userService.updateMyAnonymousProfile(details)
143 if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.'))
144 }
145 }
146
147 getDefaultVideoLanguageLabel () {
148 return this.i18n('No language')
149 }
150
151 getSelectedVideoLanguageLabel () {
152 return this.i18n('{{\'{0} languages selected')
153 }
154}