diff options
Diffstat (limited to 'client/src/assets/player/peertube-player-local-storage.ts')
-rw-r--r-- | client/src/assets/player/peertube-player-local-storage.ts | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts index 75ccfe618..cf2cfb472 100644 --- a/client/src/assets/player/peertube-player-local-storage.ts +++ b/client/src/assets/player/peertube-player-local-storage.ts | |||
@@ -68,6 +68,51 @@ function getStoredLastSubtitle () { | |||
68 | return getLocalStorage('last-subtitle') | 68 | return getLocalStorage('last-subtitle') |
69 | } | 69 | } |
70 | 70 | ||
71 | function 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 | |||
81 | function 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 | |||
97 | function 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 | |||
71 | // --------------------------------------------------------------------------- | 116 | // --------------------------------------------------------------------------- |
72 | 117 | ||
73 | export { | 118 | export { |
@@ -81,7 +126,10 @@ export { | |||
81 | saveAverageBandwidth, | 126 | saveAverageBandwidth, |
82 | getAverageBandwidthInStore, | 127 | getAverageBandwidthInStore, |
83 | saveLastSubtitle, | 128 | saveLastSubtitle, |
84 | getStoredLastSubtitle | 129 | getStoredLastSubtitle, |
130 | saveVideoWatchHistory, | ||
131 | getStoredVideoWatchHistory, | ||
132 | cleanupVideoWatch | ||
85 | } | 133 | } |
86 | 134 | ||
87 | // --------------------------------------------------------------------------- | 135 | // --------------------------------------------------------------------------- |