]>
Commit | Line | Data |
---|---|---|
7b3a99d5 C |
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 | ||
43c66a91 | 13 | function 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 |
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 | ||
011e1e6b | 32 | return false |
7b3a99d5 C |
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) { | |
ff563914 | 48 | /** used to choose the most fitting resolution */ |
7b3a99d5 C |
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 | ||
3b019808 C |
64 | function saveLastSubtitle (language: string) { |
65 | return setLocalStorage('last-subtitle', language) | |
66 | } | |
67 | ||
68 | function getStoredLastSubtitle () { | |
69 | return getLocalStorage('last-subtitle') | |
70 | } | |
71 | ||
58b9ce30 | 72 | function saveVideoWatchHistory(videoUUID: string, duration: number) { |
73 | return setLocalStorage(`video-watch-history`, JSON.stringify({ | |
74 | ...getStoredVideoWatchHistory(), | |
75 | [videoUUID]: { | |
76 | duration, | |
77 | date: `${(new Date()).toISOString()}` | |
78 | } | |
79 | })) | |
80 | } | |
81 | ||
82 | function getStoredVideoWatchHistory(videoUUID?: string) { | |
83 | let data | |
84 | ||
85 | try { | |
86 | data = JSON.parse(getLocalStorage('video-watch-history')) | |
87 | } catch (error) { | |
88 | console.error('Cannot parse video watch history from local storage: ', error) | |
89 | } | |
90 | ||
91 | data = data || {} | |
92 | ||
93 | if (videoUUID) return data[videoUUID] | |
94 | ||
95 | return data | |
96 | } | |
97 | ||
98 | function cleanupVideoWatch() { | |
99 | const data = getStoredVideoWatchHistory() | |
100 | ||
101 | const newData = Object.keys(data).reduce((acc, videoUUID) => { | |
102 | const date = Date.parse(data[videoUUID].date) | |
103 | ||
104 | const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24)) | |
105 | ||
106 | if (diff > 30) return acc | |
107 | ||
108 | return { | |
109 | ...acc, | |
110 | [videoUUID]: data[videoUUID] | |
111 | } | |
112 | }, {}) | |
113 | ||
114 | setLocalStorage('video-watch-history', JSON.stringify(newData)) | |
115 | } | |
116 | ||
7b3a99d5 C |
117 | // --------------------------------------------------------------------------- |
118 | ||
119 | export { | |
120 | getStoredVolume, | |
43c66a91 | 121 | getStoredP2PEnabled, |
7b3a99d5 C |
122 | getStoredMute, |
123 | getStoredTheater, | |
124 | saveVolumeInStore, | |
125 | saveMuteInStore, | |
126 | saveTheaterInStore, | |
127 | saveAverageBandwidth, | |
3b019808 C |
128 | getAverageBandwidthInStore, |
129 | saveLastSubtitle, | |
58b9ce30 | 130 | getStoredLastSubtitle, |
131 | saveVideoWatchHistory, | |
132 | getStoredVideoWatchHistory, | |
133 | cleanupVideoWatch | |
7b3a99d5 C |
134 | } |
135 | ||
136 | // --------------------------------------------------------------------------- | |
137 | ||
138 | const KEY_PREFIX = 'peertube-videojs-' | |
139 | ||
140 | function getLocalStorage (key: string) { | |
141 | try { | |
142 | return localStorage.getItem(KEY_PREFIX + key) | |
143 | } catch { | |
144 | return undefined | |
145 | } | |
146 | } | |
147 | ||
148 | function setLocalStorage (key: string, value: string) { | |
149 | try { | |
150 | localStorage.setItem(KEY_PREFIX + key, value) | |
151 | } catch { /* empty */ | |
152 | } | |
153 | } |