]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { pick } from 'lodash-es' |
67ed6552 | 2 | import { forkJoin, Subject, Subscription } from 'rxjs' |
ccc00cb2 | 3 | import { first } from 'rxjs/operators' |
67ed6552 C |
4 | import { Component, Input, OnDestroy, OnInit } from '@angular/core' |
5 | import { AuthService, Notifier, ServerService, User, UserService } from '@app/core' | |
52c4976f | 6 | import { FormReactive, FormValidatorService, ItemSelectCheckboxValue, SelectOptionsItem } from '@app/shared/shared-forms' |
67ed6552 C |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
8 | import { UserUpdateMe } from '@shared/models' | |
d3217560 | 9 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' |
af5e743b C |
10 | |
11 | @Component({ | |
67ed6552 C |
12 | selector: 'my-user-video-settings', |
13 | templateUrl: './user-video-settings.component.html', | |
14 | styleUrls: [ './user-video-settings.component.scss' ] | |
af5e743b | 15 | }) |
67ed6552 | 16 | export class UserVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy { |
df98563e | 17 | @Input() user: User = null |
d3217560 RK |
18 | @Input() reactiveUpdate = false |
19 | @Input() notifyOnUpdate = true | |
d18d6478 | 20 | @Input() userInformationLoaded: Subject<any> |
af5e743b | 21 | |
52c4976f | 22 | languageItems: SelectOptionsItem[] = [] |
d3217560 RK |
23 | defaultNSFWPolicy: NSFWPolicyType |
24 | formValuesWatcher: Subscription | |
3caf77d3 | 25 | |
52c4976f C |
26 | private allLanguagesGroup: string |
27 | ||
df98563e | 28 | constructor ( |
d18d6478 | 29 | protected formValidatorService: FormValidatorService, |
af5e743b | 30 | private authService: AuthService, |
f8b2c1b4 | 31 | private notifier: Notifier, |
b1d40cff | 32 | private userService: UserService, |
3caf77d3 | 33 | private serverService: ServerService, |
b1d40cff | 34 | private i18n: I18n |
af5e743b | 35 | ) { |
df98563e | 36 | super() |
af5e743b C |
37 | } |
38 | ||
d18d6478 | 39 | ngOnInit () { |
52c4976f C |
40 | this.allLanguagesGroup = this.i18n('All languages') |
41 | ||
d3217560 RK |
42 | let oldForm: any |
43 | ||
d18d6478 C |
44 | this.buildForm({ |
45 | nsfwPolicy: null, | |
ed638e53 | 46 | webTorrentEnabled: null, |
3caf77d3 | 47 | autoPlayVideo: null, |
6aa54148 | 48 | autoPlayNextVideo: null, |
3caf77d3 | 49 | videoLanguages: null |
df98563e | 50 | }) |
af5e743b | 51 | |
ccc00cb2 | 52 | forkJoin([ |
ba430d75 | 53 | this.serverService.getVideoLanguages(), |
d3217560 | 54 | this.serverService.getConfig(), |
ccc00cb2 | 55 | this.userInformationLoaded.pipe(first()) |
d3217560 | 56 | ]).subscribe(([ languages, config ]) => { |
52c4976f C |
57 | const group = this.allLanguagesGroup |
58 | ||
59 | this.languageItems = [ { label: this.i18n('Unknown language'), id: '_unknown', group } ] | |
ccc00cb2 | 60 | this.languageItems = this.languageItems |
52c4976f | 61 | .concat(languages.map(l => ({ label: l.label, id: l.id, group }))) |
3caf77d3 | 62 | |
52c4976f C |
63 | const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages |
64 | ? this.user.videoLanguages.map(l => ({ id: l })) | |
65 | : [ { group } ] | |
3caf77d3 | 66 | |
d3217560 RK |
67 | this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy |
68 | ||
ccc00cb2 | 69 | this.form.patchValue({ |
d3217560 | 70 | nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy, |
ccc00cb2 C |
71 | webTorrentEnabled: this.user.webTorrentEnabled, |
72 | autoPlayVideo: this.user.autoPlayVideo === true, | |
6aa54148 | 73 | autoPlayNextVideo: this.user.autoPlayNextVideo, |
ccc00cb2 C |
74 | videoLanguages |
75 | }) | |
d3217560 RK |
76 | |
77 | if (this.reactiveUpdate) { | |
78 | oldForm = { ...this.form.value } | |
52c4976f | 79 | |
d3217560 RK |
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 } | |
52c4976f C |
83 | |
84 | this.updateDetails([ updatedKey ]) | |
d3217560 RK |
85 | }) |
86 | } | |
ccc00cb2 | 87 | }) |
af5e743b C |
88 | } |
89 | ||
d3217560 RK |
90 | ngOnDestroy () { |
91 | this.formValuesWatcher?.unsubscribe() | |
92 | } | |
93 | ||
94 | updateDetails (onlyKeys?: string[]) { | |
ccc00cb2 | 95 | const nsfwPolicy = this.form.value[ 'nsfwPolicy' ] |
ed638e53 | 96 | const webTorrentEnabled = this.form.value['webTorrentEnabled'] |
7efe153b | 97 | const autoPlayVideo = this.form.value['autoPlayVideo'] |
6aa54148 | 98 | const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] |
3caf77d3 C |
99 | |
100 | let videoLanguages: string[] = this.form.value['videoLanguages'] | |
52c4976f | 101 | |
3caf77d3 | 102 | if (Array.isArray(videoLanguages)) { |
52c4976f C |
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.')) | |
3caf77d3 | 105 | return |
52c4976f C |
106 | } |
107 | ||
108 | if (videoLanguages.length === 0) { | |
109 | this.notifier.error(this.i18n('You need to enable at least 1 video language.')) | |
3caf77d3 C |
110 | return |
111 | } | |
52c4976f C |
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 | } | |
3caf77d3 C |
119 | } |
120 | ||
d3217560 | 121 | let details: UserUpdateMe = { |
0883b324 | 122 | nsfwPolicy, |
ed638e53 | 123 | webTorrentEnabled, |
3caf77d3 | 124 | autoPlayVideo, |
6aa54148 | 125 | autoPlayNextVideo, |
3caf77d3 | 126 | videoLanguages |
df98563e | 127 | } |
af5e743b | 128 | |
d3217560 | 129 | if (onlyKeys) details = pick(details, onlyKeys) |
af5e743b | 130 | |
d3217560 RK |
131 | if (this.authService.isLoggedIn()) { |
132 | this.userService.updateMyProfile(details).subscribe( | |
133 | () => { | |
134 | this.authService.refreshUserInformation() | |
af5e743b | 135 | |
d3217560 RK |
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 | } | |
af5e743b | 145 | } |
3caf77d3 C |
146 | |
147 | getDefaultVideoLanguageLabel () { | |
148 | return this.i18n('No language') | |
149 | } | |
150 | ||
151 | getSelectedVideoLanguageLabel () { | |
152 | return this.i18n('{{\'{0} languages selected') | |
153 | } | |
af5e743b | 154 | } |