From 58b9ce3080c12678e8c1c28c08da09d6ea60011d Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Wed, 31 Mar 2021 11:26:32 +0200 Subject: Resume videos for non-logged in users (#3885) * client: resume videos for non-logged in users closes #3866 * fix build for embeded * Update client/src/app/app.component.ts * fix review comments --- .../assets/player/peertube-player-local-storage.ts | 50 +++++++++++++++++++++- .../src/assets/player/peertube-player-manager.ts | 5 ++- client/src/assets/player/peertube-plugin.ts | 13 ++++-- .../src/assets/player/peertube-videojs-typings.ts | 2 + 4 files changed, 64 insertions(+), 6 deletions(-) (limited to 'client/src/assets/player') 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 () { 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 { @@ -81,7 +126,10 @@ export { saveAverageBandwidth, getAverageBandwidthInStore, saveLastSubtitle, - getStoredLastSubtitle + getStoredLastSubtitle, + saveVideoWatchHistory, + getStoredVideoWatchHistory, + cleanupVideoWatch } // --------------------------------------------------------------------------- diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts index 1d335805b..119dec379 100644 --- a/client/src/assets/player/peertube-player-manager.ts +++ b/client/src/assets/player/peertube-player-manager.ts @@ -106,6 +106,8 @@ export interface CommonOptions extends CustomizationOptions { videoCaptions: VideoJSCaption[] + videoUUID: string + userWatching?: UserWatching serverUrl: string @@ -231,7 +233,8 @@ export class PeertubePlayerManager { subtitle: commonOptions.subtitle, videoCaptions: commonOptions.videoCaptions, stopTime: commonOptions.stopTime, - isLive: commonOptions.isLive + isLive: commonOptions.isLive, + videoUUID: commonOptions.videoUUID } } diff --git a/client/src/assets/player/peertube-plugin.ts b/client/src/assets/player/peertube-plugin.ts index 75a6e662e..07c7e33f6 100644 --- a/client/src/assets/player/peertube-plugin.ts +++ b/client/src/assets/player/peertube-plugin.ts @@ -13,6 +13,7 @@ import { getStoredVolume, saveLastSubtitle, saveMuteInStore, + saveVideoWatchHistory, saveVolumeInStore } from './peertube-player-local-storage' @@ -120,7 +121,7 @@ class PeerTubePlugin extends Plugin { this.initializePlayer() this.runViewAdd() - if (options.userWatching) this.runUserWatchVideo(options.userWatching) + this.runUserWatchVideo(options.userWatching, options.videoUUID) }) } @@ -178,7 +179,7 @@ class PeerTubePlugin extends Plugin { }, 1000) } - private runUserWatchVideo (options: UserWatching) { + private runUserWatchVideo (options: UserWatching, videoUUID: string) { let lastCurrentTime = 0 this.userWatchingVideoInterval = setInterval(() => { @@ -187,8 +188,12 @@ class PeerTubePlugin extends Plugin { if (currentTime - lastCurrentTime >= 1) { lastCurrentTime = currentTime - this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader) - .catch(err => console.error('Cannot notify user is watching.', err)) + if (options) { + this.notifyUserIsWatching(currentTime, options.url, options.authorizationHeader) + .catch(err => console.error('Cannot notify user is watching.', err)) + } else { + saveVideoWatchHistory(videoUUID, currentTime) + } } }, this.CONSTANTS.USER_WATCHING_VIDEO_INTERVAL) } diff --git a/client/src/assets/player/peertube-videojs-typings.ts b/client/src/assets/player/peertube-videojs-typings.ts index e5259092c..4a6c80247 100644 --- a/client/src/assets/player/peertube-videojs-typings.ts +++ b/client/src/assets/player/peertube-videojs-typings.ts @@ -108,6 +108,8 @@ type PeerTubePluginOptions = { stopTime: number | string isLive: boolean + + videoUUID: string } type PlaylistPluginOptions = { -- cgit v1.2.3