2 import { filter, throttleTime } from 'rxjs'
3 import { Injectable } from '@angular/core'
4 import { AuthService, AuthStatus } from '@app/core/auth'
5 import { getBoolOrDefault } from '@root-helpers/local-storage-utils'
6 import { logger } from '@root-helpers/logger'
7 import { OAuthUserTokens, UserLocalStorageKeys } from '@root-helpers/users'
8 import { objectKeysTyped } from '@shared/core-utils'
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'
15 export class UserLocalStorageService {
18 private authService: AuthService,
19 private server: ServerService,
20 private localStorageService: LocalStorageService
22 this.authService.userInformationLoaded.subscribe({
24 const user = this.authService.getUser()
26 this.setLoggedInUser(user)
27 this.setUserInfo(user)
28 this.setTokens(user.oauthTokens)
32 this.authService.loginChangedSource
33 .pipe(filter(status => status === AuthStatus.LoggedOut))
36 this.flushLoggedInUser()
42 this.authService.tokensRefreshed
45 const user = this.authService.getUser()
47 this.setTokens(user.oauthTokens)
52 // ---------------------------------------------------------------------------
55 const usernameLocalStorage = this.localStorageService.getItem(UserLocalStorageKeys.USERNAME)
57 if (!usernameLocalStorage) return undefined
60 id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ID), 10),
61 username: this.localStorageService.getItem(UserLocalStorageKeys.USERNAME),
62 email: this.localStorageService.getItem(UserLocalStorageKeys.EMAIL),
64 id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
72 setLoggedInUser (user: {
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)
83 this.localStorageService.setItem(UserLocalStorageKeys.ROLE, user.role.id.toString())
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)
93 // ---------------------------------------------------------------------------
96 let videoLanguages: string[]
99 const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
100 videoLanguages = languagesString && languagesString !== 'undefined'
101 ? JSON.parse(languagesString)
104 videoLanguages = null
105 logger.error('Cannot parse desired video languages from localStorage.', err)
108 const htmlConfig = this.server.getHTMLConfig()
110 const defaultNSFWPolicy = htmlConfig.instance.defaultNSFWPolicy
111 const defaultP2PEnabled = htmlConfig.defaults.p2p.webapp.enabled
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',
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)
125 setUserInfo (profile: UserUpdateMe) {
126 const localStorageKeys = {
127 nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,
128 p2pEnabled: UserLocalStorageKeys.P2P_ENABLED,
129 autoPlayVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,
130 autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO,
131 autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
132 theme: UserLocalStorageKeys.THEME,
133 videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES
136 const obj: [ string, string | boolean | string[] ][] = objectKeysTyped(localStorageKeys)
137 .filter(key => key in profile)
138 .map(key => ([ localStorageKeys[key], profile[key] ]))
140 for (const [ key, value ] of obj) {
142 if (value === undefined) {
143 this.localStorageService.removeItem(key)
147 const localStorageValue = typeof value === 'string'
149 : JSON.stringify(value)
151 this.localStorageService.setItem(key, localStorageValue)
153 logger.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)
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)
167 listenUserInfoChange () {
168 return this.localStorageService.watch([
169 UserLocalStorageKeys.NSFW_POLICY,
170 UserLocalStorageKeys.P2P_ENABLED,
171 UserLocalStorageKeys.AUTO_PLAY_VIDEO,
172 UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO,
173 UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
174 UserLocalStorageKeys.THEME,
175 UserLocalStorageKeys.VIDEO_LANGUAGES
178 filter(() => this.authService.isLoggedIn() !== true)
182 // ---------------------------------------------------------------------------
185 return OAuthUserTokens.getUserTokens(this.localStorageService)
188 setTokens (tokens: OAuthUserTokens) {
189 OAuthUserTokens.saveToLocalStorage(this.localStorageService, tokens)
193 OAuthUserTokens.flushLocalStorage(this.localStorageService)