diff options
Diffstat (limited to 'client/src/app/core/users/user.service.ts')
-rw-r--r-- | client/src/app/core/users/user.service.ts | 55 |
1 files changed, 23 insertions, 32 deletions
diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts index c98b3844c..aac2a0206 100644 --- a/client/src/app/core/users/user.service.ts +++ b/client/src/app/core/users/user.service.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import { has } from 'lodash-es' | ||
2 | import { BytesPipe } from 'ngx-pipes' | 1 | import { BytesPipe } from 'ngx-pipes' |
3 | import { SortMeta } from 'primeng/api' | 2 | import { SortMeta } from 'primeng/api' |
4 | import { from, Observable, of } from 'rxjs' | 3 | import { from, Observable, of } from 'rxjs' |
@@ -7,6 +6,7 @@ import { HttpClient, HttpParams } from '@angular/common/http' | |||
7 | import { Injectable } from '@angular/core' | 6 | import { Injectable } from '@angular/core' |
8 | import { AuthService } from '@app/core/auth' | 7 | import { AuthService } from '@app/core/auth' |
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
9 | import { UserLocalStorageKeys } from '@root-helpers/users' | ||
10 | import { | 10 | import { |
11 | Avatar, | 11 | Avatar, |
12 | NSFWPolicyType, | 12 | NSFWPolicyType, |
@@ -81,37 +81,28 @@ export class UserService { | |||
81 | } | 81 | } |
82 | 82 | ||
83 | updateMyAnonymousProfile (profile: UserUpdateMe) { | 83 | updateMyAnonymousProfile (profile: UserUpdateMe) { |
84 | const supportedKeys = { | 84 | try { |
85 | // local storage keys | 85 | this.localStorageService.setItem(UserLocalStorageKeys.NSFW_POLICY, profile.nsfwPolicy) |
86 | nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val), | 86 | this.localStorageService.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, profile.webTorrentEnabled) |
87 | webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)), | ||
88 | autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)), | ||
89 | autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)), | ||
90 | theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val), | ||
91 | videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)), | ||
92 | 87 | ||
93 | // session storage keys | 88 | this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, profile.autoPlayNextVideo) |
94 | autoPlayNextVideo: (val: boolean) => | 89 | this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, profile.autoPlayNextVideoPlaylist) |
95 | this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val)) | ||
96 | } | ||
97 | 90 | ||
98 | for (const key of Object.keys(profile)) { | 91 | this.localStorageService.setItem(UserLocalStorageKeys.THEME, profile.theme) |
99 | try { | 92 | this.localStorageService.setItem(UserLocalStorageKeys.VIDEO_LANGUAGES, profile.videoLanguages) |
100 | if (has(supportedKeys, key)) supportedKeys[key](profile[key]) | 93 | } catch (err) { |
101 | } catch (err) { | 94 | console.error(`Cannot set item in localStorage. Likely due to a value impossible to stringify.`, err) |
102 | console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err) | ||
103 | } | ||
104 | } | 95 | } |
105 | } | 96 | } |
106 | 97 | ||
107 | listenAnonymousUpdate () { | 98 | listenAnonymousUpdate () { |
108 | return this.localStorageService.watch([ | 99 | return this.localStorageService.watch([ |
109 | User.KEYS.NSFW_POLICY, | 100 | UserLocalStorageKeys.NSFW_POLICY, |
110 | User.KEYS.WEBTORRENT_ENABLED, | 101 | UserLocalStorageKeys.WEBTORRENT_ENABLED, |
111 | User.KEYS.AUTO_PLAY_VIDEO, | 102 | UserLocalStorageKeys.AUTO_PLAY_VIDEO, |
112 | User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, | 103 | UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, |
113 | User.KEYS.THEME, | 104 | UserLocalStorageKeys.THEME, |
114 | User.KEYS.VIDEO_LANGUAGES | 105 | UserLocalStorageKeys.VIDEO_LANGUAGES |
115 | ]).pipe( | 106 | ]).pipe( |
116 | throttleTime(200), | 107 | throttleTime(200), |
117 | filter(() => this.authService.isLoggedIn() !== true), | 108 | filter(() => this.authService.isLoggedIn() !== true), |
@@ -269,7 +260,7 @@ export class UserService { | |||
269 | let videoLanguages: string[] | 260 | let videoLanguages: string[] |
270 | 261 | ||
271 | try { | 262 | try { |
272 | videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES)) | 263 | videoLanguages = JSON.parse(this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)) |
273 | } catch (err) { | 264 | } catch (err) { |
274 | videoLanguages = null | 265 | videoLanguages = null |
275 | console.error('Cannot parse desired video languages from localStorage.', err) | 266 | console.error('Cannot parse desired video languages from localStorage.', err) |
@@ -277,16 +268,16 @@ export class UserService { | |||
277 | 268 | ||
278 | return new User({ | 269 | return new User({ |
279 | // local storage keys | 270 | // local storage keys |
280 | nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType, | 271 | nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType, |
281 | webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false', | 272 | webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false', |
282 | theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default', | 273 | theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default', |
283 | videoLanguages, | 274 | videoLanguages, |
284 | 275 | ||
285 | autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', | 276 | autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', |
286 | autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true', | 277 | autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true', |
287 | 278 | ||
288 | // session storage keys | 279 | // session storage keys |
289 | autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' | 280 | autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' |
290 | }) | 281 | }) |
291 | } | 282 | } |
292 | 283 | ||