]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player-local-storage.ts
Resume videos for non-logged in users (#3885)
[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) {
48 return setLocalStorage('average-bandwidth', value.toString())
49}
50
51function getAverageBandwidthInStore () {
52 const value = getLocalStorage('average-bandwidth')
53 if (value !== null && value !== undefined) {
54 const valueNumber = parseInt(value, 10)
55 if (isNaN(valueNumber)) return undefined
56
57 return valueNumber
58 }
59
60 return undefined
61}
62
3b019808
C
63function saveLastSubtitle (language: string) {
64 return setLocalStorage('last-subtitle', language)
65}
66
67function getStoredLastSubtitle () {
68 return getLocalStorage('last-subtitle')
69}
70
58b9ce30 71function saveVideoWatchHistory(videoUUID: string, duration: number) {
72 return setLocalStorage(`video-watch-history`, JSON.stringify({
73 ...getStoredVideoWatchHistory(),
74 [videoUUID]: {
75 duration,
76 date: `${(new Date()).toISOString()}`
77 }
78 }))
79}
80
81function getStoredVideoWatchHistory(videoUUID?: string) {
82 let data
83
84 try {
85 data = JSON.parse(getLocalStorage('video-watch-history'))
86 } catch (error) {
87 console.error('Cannot parse video watch history from local storage: ', error)
88 }
89
90 data = data || {}
91
92 if (videoUUID) return data[videoUUID]
93
94 return data
95}
96
97function cleanupVideoWatch() {
98 const data = getStoredVideoWatchHistory()
99
100 const newData = Object.keys(data).reduce((acc, videoUUID) => {
101 const date = Date.parse(data[videoUUID].date)
102
103 const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24))
104
105 if (diff > 30) return acc
106
107 return {
108 ...acc,
109 [videoUUID]: data[videoUUID]
110 }
111 }, {})
112
113 setLocalStorage('video-watch-history', JSON.stringify(newData))
114}
115
7b3a99d5
C
116// ---------------------------------------------------------------------------
117
118export {
119 getStoredVolume,
43c66a91 120 getStoredP2PEnabled,
7b3a99d5
C
121 getStoredMute,
122 getStoredTheater,
123 saveVolumeInStore,
124 saveMuteInStore,
125 saveTheaterInStore,
126 saveAverageBandwidth,
3b019808
C
127 getAverageBandwidthInStore,
128 saveLastSubtitle,
58b9ce30 129 getStoredLastSubtitle,
130 saveVideoWatchHistory,
131 getStoredVideoWatchHistory,
132 cleanupVideoWatch
7b3a99d5
C
133}
134
135// ---------------------------------------------------------------------------
136
137const KEY_PREFIX = 'peertube-videojs-'
138
139function getLocalStorage (key: string) {
140 try {
141 return localStorage.getItem(KEY_PREFIX + key)
142 } catch {
143 return undefined
144 }
145}
146
147function setLocalStorage (key: string, value: string) {
148 try {
149 localStorage.setItem(KEY_PREFIX + key, value)
150 } catch { /* empty */
151 }
152}