]>
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 C |
6 | import { logger } from '@root-helpers/logger' |
7 | import { UserLocalStorageKeys, UserTokens } 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) | |
27 | this.setTokens(user.tokens) | |
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 | ||
46 | this.setTokens(user.tokens) | |
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), | |
62 | role: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole, | |
63 | ||
64 | ...this.getUserInfo() | |
65 | } | |
66 | } | |
67 | ||
68 | setLoggedInUser (user: { | |
69 | id: number | |
70 | username: string | |
71 | email: string | |
72 | role: UserRole | |
73 | }) { | |
74 | this.localStorageService.setItem(UserLocalStorageKeys.ID, user.id.toString()) | |
75 | this.localStorageService.setItem(UserLocalStorageKeys.USERNAME, user.username) | |
76 | this.localStorageService.setItem(UserLocalStorageKeys.EMAIL, user.email) | |
77 | this.localStorageService.setItem(UserLocalStorageKeys.ROLE, user.role.toString()) | |
78 | } | |
79 | ||
80 | flushLoggedInUser () { | |
81 | this.localStorageService.removeItem(UserLocalStorageKeys.ID) | |
82 | this.localStorageService.removeItem(UserLocalStorageKeys.USERNAME) | |
83 | this.localStorageService.removeItem(UserLocalStorageKeys.EMAIL) | |
84 | this.localStorageService.removeItem(UserLocalStorageKeys.ROLE) | |
85 | } | |
86 | ||
87 | // --------------------------------------------------------------------------- | |
88 | ||
89 | getUserInfo () { | |
90 | let videoLanguages: string[] | |
91 | ||
92 | try { | |
93 | const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES) | |
94 | videoLanguages = languagesString && languagesString !== 'undefined' | |
95 | ? JSON.parse(languagesString) | |
96 | : null | |
97 | } catch (err) { | |
98 | videoLanguages = null | |
42b40636 | 99 | logger.error('Cannot parse desired video languages from localStorage.', err) |
a9bfa85d C |
100 | } |
101 | ||
102 | const htmlConfig = this.server.getHTMLConfig() | |
103 | ||
104 | const defaultNSFWPolicy = htmlConfig.instance.defaultNSFWPolicy | |
b65de1be | 105 | const defaultP2PEnabled = htmlConfig.defaults.p2p.webapp.enabled |
a9bfa85d C |
106 | |
107 | return { | |
108 | nsfwPolicy: this.localStorageService.getItem<NSFWPolicyType>(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy, | |
109 | p2pEnabled: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.P2P_ENABLED), defaultP2PEnabled), | |
110 | theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default', | |
111 | videoLanguages, | |
112 | ||
113 | autoPlayVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO), true), | |
114 | autoPlayNextVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO), false), | |
115 | autoPlayNextVideoPlaylist: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST), true) | |
116 | } | |
117 | } | |
118 | ||
119 | setUserInfo (profile: UserUpdateMe) { | |
120 | const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = { | |
121 | nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY, | |
122 | p2pEnabled: UserLocalStorageKeys.P2P_ENABLED, | |
123 | autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO, | |
124 | autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, | |
125 | theme: UserLocalStorageKeys.THEME, | |
126 | videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES | |
127 | } | |
128 | ||
129 | const obj = Object.keys(localStorageKeys) | |
130 | .filter(key => key in profile) | |
131 | .map(key => ([ localStorageKeys[key], profile[key] ])) | |
132 | ||
133 | for (const [ key, value ] of obj) { | |
134 | try { | |
135 | if (value === undefined) { | |
136 | this.localStorageService.removeItem(key) | |
137 | continue | |
138 | } | |
139 | ||
140 | const localStorageValue = typeof value === 'string' | |
141 | ? value | |
142 | : JSON.stringify(value) | |
143 | ||
144 | this.localStorageService.setItem(key, localStorageValue) | |
145 | } catch (err) { | |
42b40636 | 146 | logger.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err) |
a9bfa85d C |
147 | } |
148 | } | |
149 | } | |
150 | ||
151 | flushUserInfo () { | |
152 | this.localStorageService.removeItem(UserLocalStorageKeys.NSFW_POLICY) | |
153 | this.localStorageService.removeItem(UserLocalStorageKeys.P2P_ENABLED) | |
154 | this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) | |
155 | this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) | |
156 | this.localStorageService.removeItem(UserLocalStorageKeys.THEME) | |
157 | this.localStorageService.removeItem(UserLocalStorageKeys.VIDEO_LANGUAGES) | |
158 | } | |
159 | ||
160 | listenUserInfoChange () { | |
161 | return this.localStorageService.watch([ | |
162 | UserLocalStorageKeys.NSFW_POLICY, | |
163 | UserLocalStorageKeys.P2P_ENABLED, | |
164 | UserLocalStorageKeys.AUTO_PLAY_VIDEO, | |
165 | UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, | |
166 | UserLocalStorageKeys.THEME, | |
167 | UserLocalStorageKeys.VIDEO_LANGUAGES | |
168 | ]).pipe( | |
169 | throttleTime(200), | |
170 | filter(() => this.authService.isLoggedIn() !== true) | |
171 | ) | |
172 | } | |
173 | ||
174 | // --------------------------------------------------------------------------- | |
175 | ||
176 | getTokens () { | |
177 | return UserTokens.getUserTokens(this.localStorageService) | |
178 | } | |
179 | ||
180 | setTokens (tokens: UserTokens) { | |
181 | UserTokens.saveToLocalStorage(this.localStorageService, tokens) | |
182 | } | |
183 | ||
184 | flushTokens () { | |
185 | UserTokens.flushLocalStorage(this.localStorageService) | |
186 | } | |
187 | } |