From 43217657d76a228e526362adb3d8bdb19717063b Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 4 Jul 2024 11:19:29 -0500 Subject: [PATCH] Update media item shares to close when changing shares on same device --- server/controllers/ShareController.js | 13 ++++--------- server/managers/ShareManager.js | 11 ++++++++++- server/models/LibraryItem.js | 1 - 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/server/controllers/ShareController.js b/server/controllers/ShareController.js index 3022cb7f58..0770bfa477 100644 --- a/server/controllers/ShareController.js +++ b/server/controllers/ShareController.js @@ -42,19 +42,14 @@ class ShareController { const playbackSession = ShareManager.findPlaybackSessionBySessionId(req.cookies.share_session_id) if (playbackSession) { - const playbackSessionMediaItemShare = ShareManager.findByMediaItemId(playbackSession.mediaItemId) - if (!playbackSessionMediaItemShare) { - Logger.error(`[ShareController] Share playback session ${req.cookies.share_session_id} media item share not found with id ${playbackSession.mediaItemId}`) - return res.sendStatus(500) - } - if (playbackSessionMediaItemShare.slug === slug) { + if (mediaItemShare.id === playbackSession.mediaItemShareId) { Logger.debug(`[ShareController] Found share playback session ${req.cookies.share_session_id}`) mediaItemShare.playbackSession = playbackSession.toJSONForClient() return res.json(mediaItemShare) } else { - // TODO: Close old session and use same session id - Logger.info(`[ShareController] Share playback session found with id ${req.cookies.share_session_id} but media item share slug ${playbackSessionMediaItemShare.slug} does not match requested slug ${slug}`) - res.clearCookie('share_session_id') + // Changed media item share - close other session + Logger.debug(`[ShareController] Other playback session is already open for share session. Closing session "${playbackSession.displayTitle}"`) + ShareManager.closeSharePlaybackSession(playbackSession) } } else { Logger.info(`[ShareController] Share playback session not found with id ${req.cookies.share_session_id}`) diff --git a/server/managers/ShareManager.js b/server/managers/ShareManager.js index da3b6fc59a..f6827973a5 100644 --- a/server/managers/ShareManager.js +++ b/server/managers/ShareManager.js @@ -25,10 +25,19 @@ class ShareManager { * @param {import('../objects/PlaybackSession')} playbackSession */ addOpenSharePlaybackSession(playbackSession) { - Logger.info(`[ShareManager] Adding new open share playback session ${playbackSession.shareSessionId}`) + Logger.info(`[ShareManager] Adding new open share playback session "${playbackSession.displayTitle}"`) this.openSharePlaybackSessions.push(playbackSession) } + /** + * + * @param {import('../objects/PlaybackSession')} playbackSession + */ + closeSharePlaybackSession(playbackSession) { + Logger.info(`[ShareManager] Closing share playback session "${playbackSession.displayTitle}"`) + this.openSharePlaybackSessions = this.openSharePlaybackSessions.filter((s) => s.id !== playbackSession.id) + } + /** * Find an open media item share by media item ID * @param {string} mediaItemId diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index cffcb80aad..098eacdf91 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -568,7 +568,6 @@ class LibraryItem extends Model { oldLibraryItem.numEpisodesIncomplete = li.numEpisodesIncomplete } if (li.mediaType === 'book' && options.include?.includes?.('share')) { - console.log('Lookup share for media item id', li.mediaId) oldLibraryItem.mediaItemShare = ShareManager.findByMediaItemId(li.mediaId) }