From 8ee4751cacd869f61d0ce33262fb17a2486d1e4d Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 5 Jan 2022 19:07:32 -0600 Subject: [PATCH] Fix:Sync audiobook progress --- server/controllers/MeController.js | 4 ++-- server/objects/User.js | 20 +++++++++++--------- server/objects/UserAudiobookData.js | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/server/controllers/MeController.js b/server/controllers/MeController.js index a57e1d98..b9b2b31f 100644 --- a/server/controllers/MeController.js +++ b/server/controllers/MeController.js @@ -40,7 +40,7 @@ class MeController { if (!audiobook) { return res.status(404).send('Audiobook not found') } - var wasUpdated = req.user.updateAudiobookData(audiobook, req.body) + var wasUpdated = req.user.updateAudiobookData(audiobook.id, req.body) if (wasUpdated) { await this.db.updateEntity('user', req.user) this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) @@ -59,7 +59,7 @@ class MeController { userAbDataPayloads.forEach((userAbData) => { var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId) if (audiobook) { - var wasUpdated = req.user.updateAudiobookData(audiobook, userAbData) + var wasUpdated = req.user.updateAudiobookData(audiobook.id, userAbData) if (wasUpdated) shouldUpdate = true } }) diff --git a/server/objects/User.js b/server/objects/User.js index 1a6a8d10..48a92c43 100644 --- a/server/objects/User.js +++ b/server/objects/User.js @@ -140,7 +140,9 @@ class User { if (user.audiobooks) { this.audiobooks = {} for (const key in user.audiobooks) { - if (user.audiobooks[key]) { + if (key === '[object Object]') { // TEMP: Bug remove bad data + Logger.warn('[User] Construct found invalid UAD') + } else if (user.audiobooks[key]) { this.audiobooks[key] = new UserAudiobookData(user.audiobooks[key]) } } @@ -207,16 +209,16 @@ class User { return this.audiobooks[stream.audiobookId] } - updateAudiobookData(audiobook, updatePayload) { + updateAudiobookData(audiobookId, updatePayload) { if (!this.audiobooks) this.audiobooks = {} - if (!this.audiobooks[audiobook.id]) { - this.audiobooks[audiobook.id] = new UserAudiobookData() - this.audiobooks[audiobook.id].audiobookId = audiobook.id + if (!this.audiobooks[audiobookId]) { + this.audiobooks[audiobookId] = new UserAudiobookData() + this.audiobooks[audiobookId].audiobookId = audiobookId } - var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload) + var wasUpdated = this.audiobooks[audiobookId].update(updatePayload) if (wasUpdated) { - // Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`) - return this.audiobooks[audiobook.id] + // Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobookId])}`) + return this.audiobooks[audiobookId] } return false } @@ -251,7 +253,7 @@ class User { if (!this.audiobooks || !this.audiobooks[audiobook.id]) { return false } - return this.updateAudiobookData(audiobook, { + return this.updateAudiobookData(audiobook.id, { progress: 0, currentTime: 0, isRead: false, diff --git a/server/objects/UserAudiobookData.js b/server/objects/UserAudiobookData.js index ca11e742..65da4a84 100644 --- a/server/objects/UserAudiobookData.js +++ b/server/objects/UserAudiobookData.js @@ -86,7 +86,7 @@ class UserAudiobookData { update(payload) { var hasUpdates = false for (const key in payload) { - if (payload[key] !== this[key]) { + if (this[key] !== undefined && payload[key] !== this[key]) { if (key === 'isRead') { if (!payload[key]) { // Updating to Not Read - Reset progress and current time this.finishedAt = null