]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { pick } from 'lodash-es' |
dd24f1bb | 2 | import { 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' | |
dd24f1bb | 6 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
67ed6552 | 7 | import { UserUpdateMe } from '@shared/models' |
d3217560 | 8 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' |
af5e743b C |
9 | |
10 | @Component({ | |
67ed6552 C |
11 | selector: 'my-user-video-settings', |
12 | templateUrl: './user-video-settings.component.html', | |
13 | styleUrls: [ './user-video-settings.component.scss' ] | |
af5e743b | 14 | }) |
67ed6552 | 15 | export class UserVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy { |
df98563e | 16 | @Input() user: User = null |
d3217560 RK |
17 | @Input() reactiveUpdate = false |
18 | @Input() notifyOnUpdate = true | |
d18d6478 | 19 | @Input() userInformationLoaded: Subject<any> |
af5e743b | 20 | |
d3217560 RK |
21 | defaultNSFWPolicy: NSFWPolicyType |
22 | formValuesWatcher: Subscription | |
3caf77d3 | 23 | |
df98563e | 24 | constructor ( |
d18d6478 | 25 | protected formValidatorService: FormValidatorService, |
af5e743b | 26 | private authService: AuthService, |
f8b2c1b4 | 27 | private notifier: Notifier, |
b1d40cff | 28 | private userService: UserService, |
66357162 | 29 | private serverService: ServerService |
af5e743b | 30 | ) { |
df98563e | 31 | super() |
af5e743b C |
32 | } |
33 | ||
d18d6478 C |
34 | ngOnInit () { |
35 | this.buildForm({ | |
36 | nsfwPolicy: null, | |
a9bfa85d | 37 | p2pEnabled: null, |
3caf77d3 | 38 | autoPlayVideo: null, |
6aa54148 | 39 | autoPlayNextVideo: null, |
3caf77d3 | 40 | videoLanguages: null |
df98563e | 41 | }) |
af5e743b | 42 | |
dd24f1bb C |
43 | this.userInformationLoaded.pipe(first()) |
44 | .subscribe( | |
45 | () => { | |
46 | const serverConfig = this.serverService.getHTMLConfig() | |
47 | this.defaultNSFWPolicy = serverConfig.instance.defaultNSFWPolicy | |
48 | ||
49 | this.form.patchValue({ | |
50 | nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy, | |
a9bfa85d | 51 | p2pEnabled: this.user.p2pEnabled, |
dd24f1bb C |
52 | autoPlayVideo: this.user.autoPlayVideo === true, |
53 | autoPlayNextVideo: this.user.autoPlayNextVideo, | |
54 | videoLanguages: this.user.videoLanguages | |
55 | }) | |
56 | ||
57 | if (this.reactiveUpdate) this.handleReactiveUpdate() | |
58 | } | |
59 | ) | |
af5e743b C |
60 | } |
61 | ||
d3217560 RK |
62 | ngOnDestroy () { |
63 | this.formValuesWatcher?.unsubscribe() | |
64 | } | |
65 | ||
66 | updateDetails (onlyKeys?: string[]) { | |
9df52d66 | 67 | const nsfwPolicy = this.form.value['nsfwPolicy'] |
a9bfa85d | 68 | const p2pEnabled = this.form.value['p2pEnabled'] |
7efe153b | 69 | const autoPlayVideo = this.form.value['autoPlayVideo'] |
6aa54148 | 70 | const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] |
3caf77d3 | 71 | |
dd24f1bb | 72 | const videoLanguages = this.form.value['videoLanguages'] |
52c4976f | 73 | |
dd24f1bb C |
74 | if (Array.isArray(videoLanguages)) { |
75 | if (videoLanguages.length > 20) { | |
66357162 | 76 | this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`) |
3caf77d3 | 77 | return |
52c4976f | 78 | } |
3caf77d3 C |
79 | } |
80 | ||
d3217560 | 81 | let details: UserUpdateMe = { |
0883b324 | 82 | nsfwPolicy, |
a9bfa85d | 83 | p2pEnabled, |
3caf77d3 | 84 | autoPlayVideo, |
6aa54148 | 85 | autoPlayNextVideo, |
3caf77d3 | 86 | videoLanguages |
df98563e | 87 | } |
af5e743b | 88 | |
a9f6802e C |
89 | if (videoLanguages) { |
90 | details = Object.assign(details, videoLanguages) | |
91 | } | |
92 | ||
d3217560 | 93 | if (onlyKeys) details = pick(details, onlyKeys) |
af5e743b | 94 | |
d3217560 | 95 | if (this.authService.isLoggedIn()) { |
dbef4043 | 96 | return this.updateLoggedProfile(details) |
d3217560 | 97 | } |
dbef4043 C |
98 | |
99 | return this.updateAnonymousProfile(details) | |
af5e743b | 100 | } |
a9f6802e | 101 | |
dbef4043 C |
102 | private handleReactiveUpdate () { |
103 | let oldForm = { ...this.form.value } | |
104 | ||
105 | this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => { | |
106 | const updatedKey = Object.keys(formValue) | |
107 | .find(k => formValue[k] !== oldForm[k]) | |
108 | ||
109 | oldForm = { ...this.form.value } | |
110 | ||
111 | this.updateDetails([ updatedKey ]) | |
112 | }) | |
113 | } | |
114 | ||
115 | private updateLoggedProfile (details: UserUpdateMe) { | |
1378c0d3 C |
116 | this.userService.updateMyProfile(details) |
117 | .subscribe({ | |
118 | next: () => { | |
119 | this.authService.refreshUserInformation() | |
dbef4043 | 120 | |
1378c0d3 C |
121 | if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`) |
122 | }, | |
dbef4043 | 123 | |
1378c0d3 C |
124 | error: err => this.notifier.error(err.message) |
125 | }) | |
dbef4043 C |
126 | } |
127 | ||
128 | private updateAnonymousProfile (details: UserUpdateMe) { | |
129 | this.userService.updateMyAnonymousProfile(details) | |
130 | if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`) | |
131 | } | |
af5e743b | 132 | } |