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