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