]>
Commit | Line | Data |
---|---|---|
1 | import { logger } from '@root-helpers/logger' | |
2 | ||
3 | function getStoredVolume () { | |
4 | const value = getLocalStorage('volume') | |
5 | if (value !== null && value !== undefined) { | |
6 | const valueNumber = parseFloat(value) | |
7 | if (isNaN(valueNumber)) return undefined | |
8 | ||
9 | return valueNumber | |
10 | } | |
11 | ||
12 | return undefined | |
13 | } | |
14 | ||
15 | function getStoredMute () { | |
16 | const value = getLocalStorage('mute') | |
17 | if (value !== null && value !== undefined) return value === 'true' | |
18 | ||
19 | return undefined | |
20 | } | |
21 | ||
22 | function getStoredTheater () { | |
23 | const value = getLocalStorage('theater-enabled') | |
24 | if (value !== null && value !== undefined) return value === 'true' | |
25 | ||
26 | return false | |
27 | } | |
28 | ||
29 | function saveVolumeInStore (value: number) { | |
30 | return setLocalStorage('volume', value.toString()) | |
31 | } | |
32 | ||
33 | function saveMuteInStore (value: boolean) { | |
34 | return setLocalStorage('mute', value.toString()) | |
35 | } | |
36 | ||
37 | function saveTheaterInStore (enabled: boolean) { | |
38 | return setLocalStorage('theater-enabled', enabled.toString()) | |
39 | } | |
40 | ||
41 | function saveAverageBandwidth (value: number) { | |
42 | /** used to choose the most fitting resolution */ | |
43 | return setLocalStorage('average-bandwidth', value.toString()) | |
44 | } | |
45 | ||
46 | function getAverageBandwidthInStore () { | |
47 | const value = getLocalStorage('average-bandwidth') | |
48 | if (value !== null && value !== undefined) { | |
49 | const valueNumber = parseInt(value, 10) | |
50 | if (isNaN(valueNumber)) return undefined | |
51 | ||
52 | return valueNumber | |
53 | } | |
54 | ||
55 | return undefined | |
56 | } | |
57 | ||
58 | function saveLastSubtitle (language: string) { | |
59 | return setLocalStorage('last-subtitle', language) | |
60 | } | |
61 | ||
62 | function getStoredLastSubtitle () { | |
63 | return getLocalStorage('last-subtitle') | |
64 | } | |
65 | ||
66 | function saveVideoWatchHistory (videoUUID: string, duration: number) { | |
67 | return setLocalStorage(`video-watch-history`, JSON.stringify({ | |
68 | ...getStoredVideoWatchHistory(), | |
69 | ||
70 | [videoUUID]: { | |
71 | duration, | |
72 | date: `${(new Date()).toISOString()}` | |
73 | } | |
74 | })) | |
75 | } | |
76 | ||
77 | function getStoredVideoWatchHistory (videoUUID?: string) { | |
78 | let data | |
79 | ||
80 | try { | |
81 | const value = getLocalStorage('video-watch-history') | |
82 | if (!value) return {} | |
83 | ||
84 | data = JSON.parse(value) | |
85 | } catch (error) { | |
86 | logger.error('Cannot parse video watch history from local storage/', error) | |
87 | } | |
88 | ||
89 | data = data || {} | |
90 | ||
91 | if (videoUUID) return data[videoUUID] | |
92 | ||
93 | return data | |
94 | } | |
95 | ||
96 | function cleanupVideoWatch () { | |
97 | const data = getStoredVideoWatchHistory() | |
98 | if (!data) return | |
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 | ||
116 | // --------------------------------------------------------------------------- | |
117 | ||
118 | export { | |
119 | getStoredVolume, | |
120 | getStoredMute, | |
121 | getStoredTheater, | |
122 | saveVolumeInStore, | |
123 | saveMuteInStore, | |
124 | saveTheaterInStore, | |
125 | saveAverageBandwidth, | |
126 | getAverageBandwidthInStore, | |
127 | saveLastSubtitle, | |
128 | getStoredLastSubtitle, | |
129 | saveVideoWatchHistory, | |
130 | getStoredVideoWatchHistory, | |
131 | cleanupVideoWatch | |
132 | } | |
133 | ||
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | const KEY_PREFIX = 'peertube-videojs-' | |
137 | ||
138 | function getLocalStorage (key: string) { | |
139 | try { | |
140 | return localStorage.getItem(KEY_PREFIX + key) | |
141 | } catch { | |
142 | return undefined | |
143 | } | |
144 | } | |
145 | ||
146 | function setLocalStorage (key: string, value: string) { | |
147 | try { | |
148 | localStorage.setItem(KEY_PREFIX + key, value) | |
149 | } catch { /* empty */ | |
150 | } | |
151 | } |