]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/peertube-player-local-storage.ts
Fix console error when watching a video
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player-local-storage.ts
1 function 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
13 function getStoredP2PEnabled (): boolean {
14 const value = getLocalStorage('webtorrent_enabled')
15 if (value !== null && value !== undefined) return value === 'true'
16
17 // By default webtorrent is enabled
18 return true
19 }
20
21 function getStoredMute () {
22 const value = getLocalStorage('mute')
23 if (value !== null && value !== undefined) return value === 'true'
24
25 return undefined
26 }
27
28 function getStoredTheater () {
29 const value = getLocalStorage('theater-enabled')
30 if (value !== null && value !== undefined) return value === 'true'
31
32 return false
33 }
34
35 function saveVolumeInStore (value: number) {
36 return setLocalStorage('volume', value.toString())
37 }
38
39 function saveMuteInStore (value: boolean) {
40 return setLocalStorage('mute', value.toString())
41 }
42
43 function saveTheaterInStore (enabled: boolean) {
44 return setLocalStorage('theater-enabled', enabled.toString())
45 }
46
47 function saveAverageBandwidth (value: number) {
48 /** used to choose the most fitting resolution */
49 return setLocalStorage('average-bandwidth', value.toString())
50 }
51
52 function 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
64 function saveLastSubtitle (language: string) {
65 return setLocalStorage('last-subtitle', language)
66 }
67
68 function getStoredLastSubtitle () {
69 return getLocalStorage('last-subtitle')
70 }
71
72 function saveVideoWatchHistory (videoUUID: string, duration: number) {
73 return setLocalStorage(`video-watch-history`, JSON.stringify({
74 ...getStoredVideoWatchHistory(),
75
76 [videoUUID]: {
77 duration,
78 date: `${(new Date()).toISOString()}`
79 }
80 }))
81 }
82
83 function getStoredVideoWatchHistory (videoUUID?: string) {
84 let data
85
86 try {
87 const value = getLocalStorage('video-watch-history')
88 if (!value) return {}
89
90 data = JSON.parse(value)
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
102 function cleanupVideoWatch () {
103 const data = getStoredVideoWatchHistory()
104 if (!data) return
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
122 // ---------------------------------------------------------------------------
123
124 export {
125 getStoredVolume,
126 getStoredP2PEnabled,
127 getStoredMute,
128 getStoredTheater,
129 saveVolumeInStore,
130 saveMuteInStore,
131 saveTheaterInStore,
132 saveAverageBandwidth,
133 getAverageBandwidthInStore,
134 saveLastSubtitle,
135 getStoredLastSubtitle,
136 saveVideoWatchHistory,
137 getStoredVideoWatchHistory,
138 cleanupVideoWatch
139 }
140
141 // ---------------------------------------------------------------------------
142
143 const KEY_PREFIX = 'peertube-videojs-'
144
145 function getLocalStorage (key: string) {
146 try {
147 return localStorage.getItem(KEY_PREFIX + key)
148 } catch {
149 return undefined
150 }
151 }
152
153 function setLocalStorage (key: string, value: string) {
154 try {
155 localStorage.setItem(KEY_PREFIX + key, value)
156 } catch { /* empty */
157 }
158 }