]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player-local-storage.ts
Fix some old typing issues
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player-local-storage.ts
CommitLineData
7b3a99d5
C
1function getStoredVolume () {
2 const value = getLocalStorage('volume')
3 if (value !== null && value !== undefined) {
4 const valueNumber = parseFloat(value)
5 if (isNaN(valueNumber)) return undefined
6
7 return valueNumber
8 }
9
10 return undefined
11}
12
43c66a91 13function getStoredP2PEnabled (): boolean {
ed638e53
RK
14 const value = getLocalStorage('webtorrent_enabled')
15 if (value !== null && value !== undefined) return value === 'true'
64cc5e85 16
e280dd06
C
17 // By default webtorrent is enabled
18 return true
64cc5e85
RK
19}
20
7b3a99d5
C
21function getStoredMute () {
22 const value = getLocalStorage('mute')
23 if (value !== null && value !== undefined) return value === 'true'
24
25 return undefined
26}
27
28function getStoredTheater () {
29 const value = getLocalStorage('theater-enabled')
30 if (value !== null && value !== undefined) return value === 'true'
31
011e1e6b 32 return false
7b3a99d5
C
33}
34
35function saveVolumeInStore (value: number) {
36 return setLocalStorage('volume', value.toString())
37}
38
39function saveMuteInStore (value: boolean) {
40 return setLocalStorage('mute', value.toString())
41}
42
43function saveTheaterInStore (enabled: boolean) {
44 return setLocalStorage('theater-enabled', enabled.toString())
45}
46
47function saveAverageBandwidth (value: number) {
ff563914 48 /** used to choose the most fitting resolution */
7b3a99d5
C
49 return setLocalStorage('average-bandwidth', value.toString())
50}
51
52function getAverageBandwidthInStore () {
53 const value = getLocalStorage('average-bandwidth')
54 if (value !== null && value !== undefined) {
55 const valueNumber = parseInt(value, 10)
56 if (isNaN(valueNumber)) return undefined
57
58 return valueNumber
59 }
60
61 return undefined
62}
63
3b019808
C
64function saveLastSubtitle (language: string) {
65 return setLocalStorage('last-subtitle', language)
66}
67
68function getStoredLastSubtitle () {
69 return getLocalStorage('last-subtitle')
70}
71
c6bfbaeb 72function saveVideoWatchHistory (videoUUID: string, duration: number) {
58b9ce30 73 return setLocalStorage(`video-watch-history`, JSON.stringify({
74 ...getStoredVideoWatchHistory(),
c6bfbaeb 75
58b9ce30 76 [videoUUID]: {
77 duration,
78 date: `${(new Date()).toISOString()}`
79 }
80 }))
81}
82
c6bfbaeb 83function getStoredVideoWatchHistory (videoUUID?: string) {
58b9ce30 84 let data
85
86 try {
c6bfbaeb
C
87 const value = getLocalStorage('video-watch-history')
88 if (!value) return {}
89
90 data = JSON.parse(value)
58b9ce30 91 } catch (error) {
92 console.error('Cannot parse video watch history from local storage: ', error)
93 }
94
95 data = data || {}
96
97 if (videoUUID) return data[videoUUID]
98
99 return data
100}
101
c6bfbaeb 102function cleanupVideoWatch () {
58b9ce30 103 const data = getStoredVideoWatchHistory()
c6bfbaeb 104 if (!data) return
58b9ce30 105
106 const newData = Object.keys(data).reduce((acc, videoUUID) => {
107 const date = Date.parse(data[videoUUID].date)
108
109 const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24))
110
111 if (diff > 30) return acc
112
113 return {
114 ...acc,
115 [videoUUID]: data[videoUUID]
116 }
117 }, {})
118
119 setLocalStorage('video-watch-history', JSON.stringify(newData))
120}
121
7b3a99d5
C
122// ---------------------------------------------------------------------------
123
124export {
125 getStoredVolume,
43c66a91 126 getStoredP2PEnabled,
7b3a99d5
C
127 getStoredMute,
128 getStoredTheater,
129 saveVolumeInStore,
130 saveMuteInStore,
131 saveTheaterInStore,
132 saveAverageBandwidth,
3b019808
C
133 getAverageBandwidthInStore,
134 saveLastSubtitle,
58b9ce30 135 getStoredLastSubtitle,
136 saveVideoWatchHistory,
137 getStoredVideoWatchHistory,
138 cleanupVideoWatch
7b3a99d5
C
139}
140
141// ---------------------------------------------------------------------------
142
143const KEY_PREFIX = 'peertube-videojs-'
144
145function getLocalStorage (key: string) {
146 try {
147 return localStorage.getItem(KEY_PREFIX + key)
148 } catch {
149 return undefined
150 }
151}
152
153function setLocalStorage (key: string, value: string) {
154 try {
155 localStorage.setItem(KEY_PREFIX + key, value)
156 } catch { /* empty */
157 }
158}