X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fassets%2Fplayer%2Fpeertube-player-local-storage.ts;h=cf2cfb472e6b4bbdf80e41e1eecd810847f5ea40;hb=58b9ce3080c12678e8c1c28c08da09d6ea60011d;hp=3ac5fe58a4c9f3636ce87e4ac3760805d686bf34;hpb=6d8c8ea73a774c3568e6d28a4cbebcf7979d5c2a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts index 3ac5fe58a..cf2cfb472 100644 --- a/client/src/assets/player/peertube-player-local-storage.ts +++ b/client/src/assets/player/peertube-player-local-storage.ts @@ -10,11 +10,12 @@ function getStoredVolume () { return undefined } -function getStoredWebTorrentEnabled (): boolean { +function getStoredP2PEnabled (): boolean { const value = getLocalStorage('webtorrent_enabled') if (value !== null && value !== undefined) return value === 'true' - return false + // By default webtorrent is enabled + return true } function getStoredMute () { @@ -28,7 +29,7 @@ function getStoredTheater () { const value = getLocalStorage('theater-enabled') if (value !== null && value !== undefined) return value === 'true' - return undefined + return false } function saveVolumeInStore (value: number) { @@ -59,18 +60,76 @@ function getAverageBandwidthInStore () { return undefined } +function saveLastSubtitle (language: string) { + return setLocalStorage('last-subtitle', language) +} + +function getStoredLastSubtitle () { + return getLocalStorage('last-subtitle') +} + +function saveVideoWatchHistory(videoUUID: string, duration: number) { + return setLocalStorage(`video-watch-history`, JSON.stringify({ + ...getStoredVideoWatchHistory(), + [videoUUID]: { + duration, + date: `${(new Date()).toISOString()}` + } + })) +} + +function getStoredVideoWatchHistory(videoUUID?: string) { + let data + + try { + data = JSON.parse(getLocalStorage('video-watch-history')) + } catch (error) { + console.error('Cannot parse video watch history from local storage: ', error) + } + + data = data || {} + + if (videoUUID) return data[videoUUID] + + return data +} + +function cleanupVideoWatch() { + const data = getStoredVideoWatchHistory() + + const newData = Object.keys(data).reduce((acc, videoUUID) => { + const date = Date.parse(data[videoUUID].date) + + const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24)) + + if (diff > 30) return acc + + return { + ...acc, + [videoUUID]: data[videoUUID] + } + }, {}) + + setLocalStorage('video-watch-history', JSON.stringify(newData)) +} + // --------------------------------------------------------------------------- export { getStoredVolume, - getStoredWebTorrentEnabled, + getStoredP2PEnabled, getStoredMute, getStoredTheater, saveVolumeInStore, saveMuteInStore, saveTheaterInStore, saveAverageBandwidth, - getAverageBandwidthInStore + getAverageBandwidthInStore, + saveLastSubtitle, + getStoredLastSubtitle, + saveVideoWatchHistory, + getStoredVideoWatchHistory, + cleanupVideoWatch } // ---------------------------------------------------------------------------