]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/root-helpers/users/user-local-storage-manager.ts
Redesign account's channels page
[github/Chocobozzz/PeerTube.git] / client / src / root-helpers / users / user-local-storage-manager.ts
CommitLineData
a4ff3100
C
1import { NSFWPolicyType, UserRole } from '@shared/models'
2import { peertubeLocalStorage } from '../peertube-web-storage'
3import { UserLocalStorageKeys } from './user-local-storage-keys'
4
5function getUserInfoFromLocalStorage () {
6 const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)
7
8 if (!usernameLocalStorage) return undefined
9
10 return {
11 id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),
12 username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),
13 email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),
14 role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
15 nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
16 webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',
17 autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
18 videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'
19 }
20}
21
22function flushUserInfoFromLocalStorage () {
23 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)
24 peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)
25 peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)
26 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)
27 peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)
28 peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)
29 peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
30 peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)
31}
32
33function saveUserInfoIntoLocalStorage (info: {
34 id: number
35 username: string
36 email: string
37 role: UserRole
38 nsfwPolicy: NSFWPolicyType
39 webTorrentEnabled: boolean
40 autoPlayVideo: boolean
41}) {
42 peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())
43 peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)
44 peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)
45 peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())
46 peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())
47 peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))
48 peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))
49}
50
51export {
52 getUserInfoFromLocalStorage,
53 saveUserInfoIntoLocalStorage,
54 flushUserInfoFromLocalStorage
55}