]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/peertube-player-local-storage.ts
Automatically restart live on server/live restart
[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 getStoredMute () {
14 const value = getLocalStorage('mute')
15 if (value !== null && value !== undefined) return value === 'true'
16
17 return undefined
18 }
19
20 function getStoredTheater () {
21 const value = getLocalStorage('theater-enabled')
22 if (value !== null && value !== undefined) return value === 'true'
23
24 return false
25 }
26
27 function saveVolumeInStore (value: number) {
28 return setLocalStorage('volume', value.toString())
29 }
30
31 function saveMuteInStore (value: boolean) {
32 return setLocalStorage('mute', value.toString())
33 }
34
35 function saveTheaterInStore (enabled: boolean) {
36 return setLocalStorage('theater-enabled', enabled.toString())
37 }
38
39 function saveAverageBandwidth (value: number) {
40 /** used to choose the most fitting resolution */
41 return setLocalStorage('average-bandwidth', value.toString())
42 }
43
44 function 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
56 function saveLastSubtitle (language: string) {
57 return setLocalStorage('last-subtitle', language)
58 }
59
60 function getStoredLastSubtitle () {
61 return getLocalStorage('last-subtitle')
62 }
63
64 function saveVideoWatchHistory (videoUUID: string, duration: number) {
65 return setLocalStorage(`video-watch-history`, JSON.stringify({
66 ...getStoredVideoWatchHistory(),
67
68 [videoUUID]: {
69 duration,
70 date: `${(new Date()).toISOString()}`
71 }
72 }))
73 }
74
75 function getStoredVideoWatchHistory (videoUUID?: string) {
76 let data
77
78 try {
79 const value = getLocalStorage('video-watch-history')
80 if (!value) return {}
81
82 data = JSON.parse(value)
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
94 function cleanupVideoWatch () {
95 const data = getStoredVideoWatchHistory()
96 if (!data) return
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
114 // ---------------------------------------------------------------------------
115
116 export {
117 getStoredVolume,
118 getStoredMute,
119 getStoredTheater,
120 saveVolumeInStore,
121 saveMuteInStore,
122 saveTheaterInStore,
123 saveAverageBandwidth,
124 getAverageBandwidthInStore,
125 saveLastSubtitle,
126 getStoredLastSubtitle,
127 saveVideoWatchHistory,
128 getStoredVideoWatchHistory,
129 cleanupVideoWatch
130 }
131
132 // ---------------------------------------------------------------------------
133
134 const KEY_PREFIX = 'peertube-videojs-'
135
136 function getLocalStorage (key: string) {
137 try {
138 return localStorage.getItem(KEY_PREFIX + key)
139 } catch {
140 return undefined
141 }
142 }
143
144 function setLocalStorage (key: string, value: string) {
145 try {
146 localStorage.setItem(KEY_PREFIX + key, value)
147 } catch { /* empty */
148 }
149 }