From f83dd29213dbd09cec499e751378e52341046fc2 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 28 Jan 2023 14:46:01 -0600 Subject: [PATCH] Update:syncLocalMediaProgress API response payload --- server/controllers/MeController.js | 14 ++++++++++---- server/objects/user/User.js | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/controllers/MeController.js b/server/controllers/MeController.js index ccfad479..7e4cfc09 100644 --- a/server/controllers/MeController.js +++ b/server/controllers/MeController.js @@ -192,7 +192,8 @@ class MeController { } const updatedLocalMediaProgress = [] var numServerProgressUpdates = 0 - var localMediaProgress = req.body.localMediaProgress || [] + const updatedServerMediaProgress = [] + const localMediaProgress = req.body.localMediaProgress || [] localMediaProgress.forEach(localProgress => { if (!localProgress.libraryItemId) { @@ -205,18 +206,22 @@ class MeController { return } - var mediaProgress = req.user.getMediaProgress(localProgress.libraryItemId, localProgress.episodeId) + let mediaProgress = req.user.getMediaProgress(localProgress.libraryItemId, localProgress.episodeId) if (!mediaProgress) { // New media progress from mobile Logger.debug(`[MeController] syncLocalMediaProgress local progress is new - creating ${localProgress.id}`) req.user.createUpdateMediaProgress(libraryItem, localProgress, localProgress.episodeId) + mediaProgress = req.user.getMediaProgress(localProgress.libraryItemId, localProgress.episodeId) + updatedServerMediaProgress.push(mediaProgress) numServerProgressUpdates++ } else if (mediaProgress.lastUpdate < localProgress.lastUpdate) { Logger.debug(`[MeController] syncLocalMediaProgress local progress is more recent - updating ${mediaProgress.id}`) req.user.createUpdateMediaProgress(libraryItem, localProgress, localProgress.episodeId) + mediaProgress = req.user.getMediaProgress(localProgress.libraryItemId, localProgress.episodeId) + updatedServerMediaProgress.push(mediaProgress) numServerProgressUpdates++ } else if (mediaProgress.lastUpdate > localProgress.lastUpdate) { - var updateTimeDifference = mediaProgress.lastUpdate - localProgress.lastUpdate + const updateTimeDifference = mediaProgress.lastUpdate - localProgress.lastUpdate Logger.debug(`[MeController] syncLocalMediaProgress server progress is more recent by ${updateTimeDifference}ms - ${mediaProgress.id}`) for (const key in localProgress) { @@ -240,7 +245,8 @@ class MeController { res.json({ numServerProgressUpdates, - localProgressUpdates: updatedLocalMediaProgress + localProgressUpdates: updatedLocalMediaProgress, // Array of LocalMediaProgress that were updated from server (server more recent) + serverProgressUpdates: updatedServerMediaProgress // Array of MediaProgress that made updates to server (local more recent) }) } diff --git a/server/objects/user/User.js b/server/objects/user/User.js index b019e19b..745cade2 100644 --- a/server/objects/user/User.js +++ b/server/objects/user/User.js @@ -314,18 +314,18 @@ class User { } createUpdateMediaProgress(libraryItem, updatePayload, episodeId = null) { - var itemProgress = this.mediaProgress.find(li => { + const itemProgress = this.mediaProgress.find(li => { if (episodeId && li.episodeId !== episodeId) return false return li.libraryItemId === libraryItem.id }) if (!itemProgress) { - var newItemProgress = new MediaProgress() + const newItemProgress = new MediaProgress() newItemProgress.setData(libraryItem.id, updatePayload, episodeId) this.mediaProgress.push(newItemProgress) return true } - var wasUpdated = itemProgress.update(updatePayload) + const wasUpdated = itemProgress.update(updatePayload) if (updatePayload.lastUpdate) itemProgress.lastUpdate = updatePayload.lastUpdate // For local to keep update times in sync return wasUpdated