]>
Commit | Line | Data |
---|---|---|
1 | import { pick } from 'lodash-es' | |
2 | import { forkJoin, Subject, Subscription } from 'rxjs' | |
3 | import { first } from 'rxjs/operators' | |
4 | import { Component, Input, OnDestroy, OnInit } from '@angular/core' | |
5 | import { AuthService, Notifier, ServerService, User, UserService } from '@app/core' | |
6 | import { FormReactive, FormValidatorService, ItemSelectCheckboxValue } from '@app/shared/shared-forms' | |
7 | import { UserUpdateMe } from '@shared/models' | |
8 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' | |
9 | import { SelectOptionsItem } from '../../../types/select-options-item.model' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-user-video-settings', | |
13 | templateUrl: './user-video-settings.component.html', | |
14 | styleUrls: [ './user-video-settings.component.scss' ] | |
15 | }) | |
16 | export 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 | ) { | |
35 | super() | |
36 | } | |
37 | ||
38 | ngOnInit () { | |
39 | this.allLanguagesGroup = $localize`All languages` | |
40 | ||
41 | let oldForm: any | |
42 | ||
43 | this.buildForm({ | |
44 | nsfwPolicy: null, | |
45 | webTorrentEnabled: null, | |
46 | autoPlayVideo: null, | |
47 | autoPlayNextVideo: null, | |
48 | videoLanguages: null | |
49 | }) | |
50 | ||
51 | forkJoin([ | |
52 | this.serverService.getVideoLanguages(), | |
53 | this.serverService.getConfig(), | |
54 | this.userInformationLoaded.pipe(first()) | |
55 | ]).subscribe(([ languages, config ]) => { | |
56 | const group = this.allLanguagesGroup | |
57 | ||
58 | this.languageItems = [ { label: $localize`Unknown language`, id: '_unknown', group } ] | |
59 | this.languageItems = this.languageItems | |
60 | .concat(languages.map(l => ({ label: l.label, id: l.id, group }))) | |
61 | ||
62 | const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages | |
63 | ? this.user.videoLanguages.map(l => ({ id: l })) | |
64 | : [ { group } ] | |
65 | ||
66 | this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy | |
67 | ||
68 | this.form.patchValue({ | |
69 | nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy, | |
70 | webTorrentEnabled: this.user.webTorrentEnabled, | |
71 | autoPlayVideo: this.user.autoPlayVideo === true, | |
72 | autoPlayNextVideo: this.user.autoPlayNextVideo, | |
73 | videoLanguages | |
74 | }) | |
75 | ||
76 | if (this.reactiveUpdate) { | |
77 | oldForm = { ...this.form.value } | |
78 | ||
79 | this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => { | |
80 | const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k]) | |
81 | oldForm = { ...this.form.value } | |
82 | ||
83 | this.updateDetails([ updatedKey ]) | |
84 | }) | |
85 | } | |
86 | }) | |
87 | } | |
88 | ||
89 | ngOnDestroy () { | |
90 | this.formValuesWatcher?.unsubscribe() | |
91 | } | |
92 | ||
93 | updateDetails (onlyKeys?: string[]) { | |
94 | const nsfwPolicy = this.form.value[ 'nsfwPolicy' ] | |
95 | const webTorrentEnabled = this.form.value['webTorrentEnabled'] | |
96 | const autoPlayVideo = this.form.value['autoPlayVideo'] | |
97 | const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] | |
98 | ||
99 | const videoLanguagesForm = this.form.value['videoLanguages'] | |
100 | ||
101 | if (Array.isArray(videoLanguagesForm)) { | |
102 | if (videoLanguagesForm.length > 20) { | |
103 | this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`) | |
104 | return | |
105 | } | |
106 | ||
107 | if (videoLanguagesForm.length === 0) { | |
108 | this.notifier.error($localize`You need to enable at least 1 video language.`) | |
109 | return | |
110 | } | |
111 | } | |
112 | ||
113 | const videoLanguages = this.getVideoLanguages(videoLanguagesForm) | |
114 | ||
115 | let details: UserUpdateMe = { | |
116 | nsfwPolicy, | |
117 | webTorrentEnabled, | |
118 | autoPlayVideo, | |
119 | autoPlayNextVideo, | |
120 | videoLanguages | |
121 | } | |
122 | ||
123 | if (videoLanguages) { | |
124 | details = Object.assign(details, videoLanguages) | |
125 | } | |
126 | ||
127 | if (onlyKeys) details = pick(details, onlyKeys) | |
128 | ||
129 | if (this.authService.isLoggedIn()) { | |
130 | this.userService.updateMyProfile(details).subscribe( | |
131 | () => { | |
132 | this.authService.refreshUserInformation() | |
133 | ||
134 | if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`) | |
135 | }, | |
136 | ||
137 | err => this.notifier.error(err.message) | |
138 | ) | |
139 | } else { | |
140 | this.userService.updateMyAnonymousProfile(details) | |
141 | if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`) | |
142 | } | |
143 | } | |
144 | ||
145 | private getVideoLanguages (videoLanguages: ItemSelectCheckboxValue[]) { | |
146 | if (!Array.isArray(videoLanguages)) return undefined | |
147 | ||
148 | // null means "All" | |
149 | if (videoLanguages.length === this.languageItems.length) return null | |
150 | ||
151 | if (videoLanguages.length === 1) { | |
152 | const videoLanguage = videoLanguages[0] | |
153 | ||
154 | if (typeof videoLanguage === 'string') { | |
155 | if (videoLanguage === this.allLanguagesGroup) return null | |
156 | } else { | |
157 | if (videoLanguage.group === this.allLanguagesGroup) return null | |
158 | } | |
159 | } | |
160 | ||
161 | return videoLanguages.map(l => { | |
162 | if (typeof l === 'string') return l | |
163 | ||
164 | if (l.group) return l.group | |
165 | ||
166 | return l.id + '' | |
167 | }) | |
168 | } | |
169 | } |