]>
Commit | Line | Data |
---|---|---|
a9bfa85d C |
1 | |
2 | import { filter, throttleTime } from 'rxjs' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { AuthService, AuthStatus } from '@app/core/auth' | |
a9bfa85d | 5 | import { getBoolOrDefault } from '@root-helpers/local-storage-utils' |
42b40636 | 6 | import { logger } from '@root-helpers/logger' |
54909304 C |
7 | import { OAuthUserTokens, UserLocalStorageKeys } from '@root-helpers/users' |
8 | import { objectKeysTyped } from '@shared/core-utils' | |
a9bfa85d C |
9 | import { UserRole, UserUpdateMe } from '@shared/models' |
10 | import { NSFWPolicyType } from '@shared/models/videos' | |
11 | import { ServerService } from '../server' | |
12 | import { LocalStorageService } from '../wrappers/storage.service' | |
13 | ||
14 | @Injectable() | |
15 | export class UserLocalStorageService { | |
16 | ||
17 | constructor ( | |
18 | private authService: AuthService, | |
19 | private server: ServerService, | |
20 | private localStorageService: LocalStorageService | |
21 | ) { | |
22 | this.authService.userInformationLoaded.subscribe({ | |
23 | next: () => { | |
24 | const user = this.authService.getUser() | |
25 | ||
26 | this.setLoggedInUser(user) | |
27 | this.setUserInfo(user) | |
3545e72c | 28 | this.setTokens(user.oauthTokens) |
a9bfa85d C |
29 | } |
30 | }) | |
31 | ||
32 | this.authService.loginChangedSource | |
33 | .pipe(filter(status => status === AuthStatus.LoggedOut)) | |
34 | .subscribe({ | |
35 | next: () => { | |
36 | this.flushLoggedInUser() | |
37 | this.flushUserInfo() | |
38 | this.flushTokens() | |
39 | } | |
40 | }) | |
41 | ||
42 | this.authService.tokensRefreshed | |
43 | .subscribe({ | |
44 | next: () => { | |
45 | const user = this.authService.getUser() | |
46 | ||
3545e72c | 47 | this.setTokens(user.oauthTokens) |
a9bfa85d C |
48 | } |
49 | }) | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
54 | getLoggedInUser () { | |
55 | const usernameLocalStorage = this.localStorageService.getItem(UserLocalStorageKeys.USERNAME) | |
56 | ||
57 | if (!usernameLocalStorage) return undefined | |
58 | ||
59 | return { | |
60 | id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ID), 10), | |
61 | username: this.localStorageService.getItem(UserLocalStorageKeys.USERNAME), | |
62 | email: this.localStorageService.getItem(UserLocalStorageKeys.EMAIL), | |
9e5cf66b C |
63 | role: { |
64 | id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole, | |
65 | label: '' | |
66 | }, | |
a9bfa85d C |
67 | |
68 | ...this.getUserInfo() | |
69 | } | |
70 | } | |
71 | ||
72 | setLoggedInUser (user: { | |
73 | id: number | |
74 | username: string | |
75 | email: string | |
9e5cf66b C |
76 | role: { |
77 | id: UserRole | |
78 | } | |
a9bfa85d C |
79 | }) { |
80 | this.localStorageService.setItem(UserLocalStorageKeys.ID, user.id.toString()) | |
81 | this.localStorageService.setItem(UserLocalStorageKeys.USERNAME, user.username) | |
82 | this.localStorageService.setItem(UserLocalStorageKeys.EMAIL, user.email) | |
9e5cf66b | 83 | this.localStorageService.setItem(UserLocalStorageKeys.ROLE, user.role.id.toString()) |
a9bfa85d C |
84 | } |
85 | ||
86 | flushLoggedInUser () { | |
87 | this.localStorageService.removeItem(UserLocalStorageKeys.ID) | |
88 | this.localStorageService.removeItem(UserLocalStorageKeys.USERNAME) | |
89 | this.localStorageService.removeItem(UserLocalStorageKeys.EMAIL) | |
90 | this.localStorageService.removeItem(UserLocalStorageKeys.ROLE) | |
91 | } | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | getUserInfo () { | |
96 | let videoLanguages: string[] | |
97 | ||
98 | try { | |
99 | const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES) | |
100 | videoLanguages = languagesString && languagesString !== 'undefined' | |
101 | ? JSON.parse(languagesString) | |
102 | : null | |
103 | } catch (err) { | |
104 | videoLanguages = null | |
42b40636 | 105 | logger.error('Cannot parse desired video languages from localStorage.', err) |
a9bfa85d C |
106 | } |
107 | ||
108 | const htmlConfig = this.server.getHTMLConfig() | |
109 | ||
110 | const defaultNSFWPolicy = htmlConfig.instance.defaultNSFWPolicy | |
b65de1be | 111 | const defaultP2PEnabled = htmlConfig.defaults.p2p.webapp.enabled |
a9bfa85d C |
112 | |
113 | return { | |
114 | nsfwPolicy: this.localStorageService.getItem<NSFWPolicyType>(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy, | |
115 | p2pEnabled: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.P2P_ENABLED), defaultP2PEnabled), | |
116 | theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default', | |
117 | videoLanguages, | |
118 | ||
119 | autoPlayVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO), true), | |
120 | autoPlayNextVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO), false), | |
121 | autoPlayNextVideoPlaylist: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST), true) | |
122 | } | |
123 | } | |
124 | ||
125 | setUserInfo (profile: UserUpdateMe) { | |
54909304 | 126 | const localStorageKeys = { |
a9bfa85d C |
127 | nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY, |
128 | p2pEnabled: UserLocalStorageKeys.P2P_ENABLED, | |
b1d8261c C |
129 | autoPlayVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO, |
130 | autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO, | |
a9bfa85d C |
131 | autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, |
132 | theme: UserLocalStorageKeys.THEME, | |
133 | videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES | |
134 | } | |
135 | ||
54909304 | 136 | const obj: [ string, string | boolean | string[] ][] = objectKeysTyped(localStorageKeys) |
a9bfa85d C |
137 | .filter(key => key in profile) |
138 | .map(key => ([ localStorageKeys[key], profile[key] ])) | |
139 | ||
140 | for (const [ key, value ] of obj) { | |
141 | try { | |
142 | if (value === undefined) { | |
143 | this.localStorageService.removeItem(key) | |
144 | continue | |
145 | } | |
146 | ||
147 | const localStorageValue = typeof value === 'string' | |
148 | ? value | |
149 | : JSON.stringify(value) | |
150 | ||
151 | this.localStorageService.setItem(key, localStorageValue) | |
152 | } catch (err) { | |
42b40636 | 153 | logger.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err) |
a9bfa85d C |
154 | } |
155 | } | |
156 | } | |
157 | ||
158 | flushUserInfo () { | |
159 | this.localStorageService.removeItem(UserLocalStorageKeys.NSFW_POLICY) | |
160 | this.localStorageService.removeItem(UserLocalStorageKeys.P2P_ENABLED) | |
161 | this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) | |
162 | this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) | |
163 | this.localStorageService.removeItem(UserLocalStorageKeys.THEME) | |
164 | this.localStorageService.removeItem(UserLocalStorageKeys.VIDEO_LANGUAGES) | |
165 | } | |
166 | ||
167 | listenUserInfoChange () { | |
168 | return this.localStorageService.watch([ | |
169 | UserLocalStorageKeys.NSFW_POLICY, | |
170 | UserLocalStorageKeys.P2P_ENABLED, | |
171 | UserLocalStorageKeys.AUTO_PLAY_VIDEO, | |
6a7cea15 | 172 | UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO, |
a9bfa85d C |
173 | UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, |
174 | UserLocalStorageKeys.THEME, | |
175 | UserLocalStorageKeys.VIDEO_LANGUAGES | |
176 | ]).pipe( | |
177 | throttleTime(200), | |
178 | filter(() => this.authService.isLoggedIn() !== true) | |
179 | ) | |
180 | } | |
181 | ||
182 | // --------------------------------------------------------------------------- | |
183 | ||
184 | getTokens () { | |
3545e72c | 185 | return OAuthUserTokens.getUserTokens(this.localStorageService) |
a9bfa85d C |
186 | } |
187 | ||
3545e72c C |
188 | setTokens (tokens: OAuthUserTokens) { |
189 | OAuthUserTokens.saveToLocalStorage(this.localStorageService, tokens) | |
a9bfa85d C |
190 | } |
191 | ||
192 | flushTokens () { | |
3545e72c | 193 | OAuthUserTokens.flushLocalStorage(this.localStorageService) |
a9bfa85d C |
194 | } |
195 | } |