]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/peertube-player-local-storage.ts
Resume videos for non-logged in users (#3885)
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player-local-storage.ts
index 6882f68a604a5203dfc1fd48b5e3263bf542b32a..cf2cfb472e6b4bbdf80e41e1eecd810847f5ea40 100644 (file)
@@ -10,6 +10,14 @@ function getStoredVolume () {
   return undefined
 }
 
+function getStoredP2PEnabled (): boolean {
+  const value = getLocalStorage('webtorrent_enabled')
+  if (value !== null && value !== undefined) return value === 'true'
+
+  // By default webtorrent is enabled
+  return true
+}
+
 function getStoredMute () {
   const value = getLocalStorage('mute')
   if (value !== null && value !== undefined) return value === 'true'
@@ -21,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) {
@@ -52,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,
+  getStoredP2PEnabled,
   getStoredMute,
   getStoredTheater,
   saveVolumeInStore,
   saveMuteInStore,
   saveTheaterInStore,
   saveAverageBandwidth,
-  getAverageBandwidthInStore
+  getAverageBandwidthInStore,
+  saveLastSubtitle,
+  getStoredLastSubtitle,
+  saveVideoWatchHistory,
+  getStoredVideoWatchHistory,
+  cleanupVideoWatch
 }
 
 // ---------------------------------------------------------------------------