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