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