aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/users/user-local-storage-manager.ts
blob: c75cea12706ebfcb8d3173dcfb3cd0f7757d5be3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NSFWPolicyType, UserRole } from '@shared/models'
import { peertubeLocalStorage } from '../peertube-web-storage'
import { UserLocalStorageKeys } from './user-local-storage-keys'

function getUserInfoFromLocalStorage () {
  const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)

  if (!usernameLocalStorage) return undefined

  return {
    id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),
    username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),
    email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),
    role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
    nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
    webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',
    autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
    videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'
  }
}

function flushUserInfoFromLocalStorage () {
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
  peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)
}

function saveUserInfoIntoLocalStorage (info: {
  id: number
  username: string
  email: string
  role: UserRole
  nsfwPolicy: NSFWPolicyType
  webTorrentEnabled: boolean
  autoPlayVideo: boolean
}) {
  peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())
  peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)
  peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)
  peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())
  peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())
  peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))
  peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))
}

export {
  getUserInfoFromLocalStorage,
  saveUserInfoIntoLocalStorage,
  flushUserInfoFromLocalStorage
}