]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { Component, Input, OnInit, OnDestroy } from '@angular/core' |
3caf77d3 | 2 | import { Notifier, ServerService } from '@app/core' |
d3217560 RK |
3 | import { UserUpdateMe } from '../../../../../../shared/models/users' |
4 | import { User, UserService } from '@app/shared/users' | |
c30745f3 | 5 | import { AuthService } from '../../../core' |
d3217560 | 6 | import { FormReactive } from '@app/shared/forms/form-reactive' |
b1d40cff | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 8 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
d3217560 | 9 | import { forkJoin, Subject, Subscription } from 'rxjs' |
3caf77d3 | 10 | import { SelectItem } from 'primeng/api' |
ccc00cb2 | 11 | import { first } from 'rxjs/operators' |
d3217560 RK |
12 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' |
13 | import { pick } from 'lodash-es' | |
af5e743b C |
14 | |
15 | @Component({ | |
ed56ad11 C |
16 | selector: 'my-account-video-settings', |
17 | templateUrl: './my-account-video-settings.component.html', | |
18 | styleUrls: [ './my-account-video-settings.component.scss' ] | |
af5e743b | 19 | }) |
d3217560 | 20 | export class MyAccountVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy { |
df98563e | 21 | @Input() user: User = null |
d3217560 RK |
22 | @Input() reactiveUpdate = false |
23 | @Input() notifyOnUpdate = true | |
d18d6478 | 24 | @Input() userInformationLoaded: Subject<any> |
af5e743b | 25 | |
3caf77d3 | 26 | languageItems: SelectItem[] = [] |
d3217560 RK |
27 | defaultNSFWPolicy: NSFWPolicyType |
28 | formValuesWatcher: Subscription | |
3caf77d3 | 29 | |
df98563e | 30 | constructor ( |
d18d6478 | 31 | protected formValidatorService: FormValidatorService, |
af5e743b | 32 | private authService: AuthService, |
f8b2c1b4 | 33 | private notifier: Notifier, |
b1d40cff | 34 | private userService: UserService, |
3caf77d3 | 35 | private serverService: ServerService, |
b1d40cff | 36 | private i18n: I18n |
af5e743b | 37 | ) { |
df98563e | 38 | super() |
af5e743b C |
39 | } |
40 | ||
d18d6478 | 41 | ngOnInit () { |
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 ]) => { |
ccc00cb2 C |
57 | this.languageItems = [ { label: this.i18n('Unknown language'), value: '_unknown' } ] |
58 | this.languageItems = this.languageItems | |
59 | .concat(languages.map(l => ({ label: l.label, value: l.id }))) | |
3caf77d3 | 60 | |
ccc00cb2 C |
61 | const videoLanguages = this.user.videoLanguages |
62 | ? this.user.videoLanguages | |
63 | : this.languageItems.map(l => l.value) | |
3caf77d3 | 64 | |
d3217560 RK |
65 | this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy |
66 | ||
ccc00cb2 | 67 | this.form.patchValue({ |
d3217560 | 68 | nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy, |
ccc00cb2 C |
69 | webTorrentEnabled: this.user.webTorrentEnabled, |
70 | autoPlayVideo: this.user.autoPlayVideo === true, | |
6aa54148 | 71 | autoPlayNextVideo: this.user.autoPlayNextVideo, |
ccc00cb2 C |
72 | videoLanguages |
73 | }) | |
d3217560 RK |
74 | |
75 | if (this.reactiveUpdate) { | |
76 | oldForm = { ...this.form.value } | |
77 | this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => { | |
78 | const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k]) | |
79 | oldForm = { ...this.form.value } | |
80 | this.updateDetails([updatedKey]) | |
81 | }) | |
82 | } | |
ccc00cb2 | 83 | }) |
af5e743b C |
84 | } |
85 | ||
d3217560 RK |
86 | ngOnDestroy () { |
87 | this.formValuesWatcher?.unsubscribe() | |
88 | } | |
89 | ||
90 | updateDetails (onlyKeys?: string[]) { | |
ccc00cb2 | 91 | const nsfwPolicy = this.form.value[ 'nsfwPolicy' ] |
ed638e53 | 92 | const webTorrentEnabled = this.form.value['webTorrentEnabled'] |
7efe153b | 93 | const autoPlayVideo = this.form.value['autoPlayVideo'] |
6aa54148 | 94 | const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] |
3caf77d3 C |
95 | |
96 | let videoLanguages: string[] = this.form.value['videoLanguages'] | |
97 | if (Array.isArray(videoLanguages)) { | |
98 | if (videoLanguages.length === this.languageItems.length) { | |
99 | videoLanguages = null // null means "All" | |
100 | } else if (videoLanguages.length > 20) { | |
101 | this.notifier.error('Too many languages are enabled. Please enable them all or stay below 20 enabled languages.') | |
102 | return | |
103 | } else if (videoLanguages.length === 0) { | |
104 | this.notifier.error('You need to enabled at least 1 video language.') | |
105 | return | |
106 | } | |
107 | } | |
108 | ||
d3217560 | 109 | let details: UserUpdateMe = { |
0883b324 | 110 | nsfwPolicy, |
ed638e53 | 111 | webTorrentEnabled, |
3caf77d3 | 112 | autoPlayVideo, |
6aa54148 | 113 | autoPlayNextVideo, |
3caf77d3 | 114 | videoLanguages |
df98563e | 115 | } |
af5e743b | 116 | |
d3217560 | 117 | if (onlyKeys) details = pick(details, onlyKeys) |
af5e743b | 118 | |
d3217560 RK |
119 | if (this.authService.isLoggedIn()) { |
120 | this.userService.updateMyProfile(details).subscribe( | |
121 | () => { | |
122 | this.authService.refreshUserInformation() | |
af5e743b | 123 | |
d3217560 RK |
124 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Video settings updated.')) |
125 | }, | |
126 | ||
127 | err => this.notifier.error(err.message) | |
128 | ) | |
129 | } else { | |
130 | this.userService.updateMyAnonymousProfile(details) | |
131 | if (this.notifyOnUpdate) this.notifier.success(this.i18n('Display/Video settings updated.')) | |
132 | } | |
af5e743b | 133 | } |
3caf77d3 C |
134 | |
135 | getDefaultVideoLanguageLabel () { | |
136 | return this.i18n('No language') | |
137 | } | |
138 | ||
139 | getSelectedVideoLanguageLabel () { | |
140 | return this.i18n('{{\'{0} languages selected') | |
141 | } | |
af5e743b | 142 | } |