From ff92fbb849641f9478fdf4c43f4665abb29cb8f5 Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 28 Oct 2021 17:19:09 -0500 Subject: [PATCH] Change: AudiobookProgress becomes UserAudiobookData, Change: Delete audiobook progress route --- client/pages/audiobook/_id/index.vue | 2 +- server/ApiController.js | 25 +++++++++++-------- server/Server.js | 7 +----- server/objects/User.js | 18 ++++++------- ...iobookProgress.js => UserAudiobookData.js} | 10 ++++---- 5 files changed, 30 insertions(+), 32 deletions(-) rename server/objects/{AudiobookProgress.js => UserAudiobookData.js} (93%) diff --git a/client/pages/audiobook/_id/index.vue b/client/pages/audiobook/_id/index.vue index ad09e3c4..b8d935d5 100644 --- a/client/pages/audiobook/_id/index.vue +++ b/client/pages/audiobook/_id/index.vue @@ -413,7 +413,7 @@ export default { if (confirm(`Are you sure you want to reset your progress?`)) { this.resettingProgress = true this.$axios - .$delete(`/api/user/audiobook/${this.audiobookId}`) + .$patch(`/api/user/audiobook/${this.audiobookId}/reset-progress`) .then(() => { console.log('Progress reset complete') this.$toast.success(`Your progress was reset`) diff --git a/server/ApiController.js b/server/ApiController.js index 7e95a8e1..0ee53ad0 100644 --- a/server/ApiController.js +++ b/server/ApiController.js @@ -60,9 +60,12 @@ class ApiController { this.router.patch('/match/:id', this.match.bind(this)) + // Old Route : Wait until refactor of mobile app to replace with path /reset-progress this.router.delete('/user/audiobook/:id', this.resetUserAudiobookProgress.bind(this)) - this.router.patch('/user/audiobook/:id', this.updateUserAudiobookProgress.bind(this)) - this.router.patch('/user/audiobooks', this.batchUpdateUserAudiobooksProgress.bind(this)) + + this.router.patch('/user/audiobook/:id/reset-progress', this.resetUserAudiobookProgress.bind(this)) + this.router.patch('/user/audiobook/:id', this.updateUserAudiobookData.bind(this)) + this.router.patch('/user/audiobooks', this.batchUpdateUserAudiobookData.bind(this)) this.router.patch('/user/password', this.userChangePassword.bind(this)) this.router.patch('/user/settings', this.userUpdateSettings.bind(this)) @@ -342,7 +345,7 @@ class ApiController { // Remove audiobook from users for (let i = 0; i < this.db.users.length; i++) { var user = this.db.users[i] - var madeUpdates = user.deleteAudiobookProgress(audiobook.id) + var madeUpdates = user.deleteAudiobookData(audiobook.id) if (madeUpdates) { await this.db.updateEntity('user', user) } @@ -564,12 +567,12 @@ class ApiController { res.sendStatus(200) } - async updateUserAudiobookProgress(req, res) { + async updateUserAudiobookData(req, res) { var audiobook = this.db.audiobooks.find(ab => ab.id === req.params.id) if (!audiobook) { return res.status(404).send('Audiobook not found') } - var wasUpdated = req.user.updateAudiobookProgress(audiobook, req.body) + var wasUpdated = req.user.updateAudiobookData(audiobook, req.body) if (wasUpdated) { await this.db.updateEntity('user', req.user) this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) @@ -577,17 +580,17 @@ class ApiController { res.sendStatus(200) } - async batchUpdateUserAudiobooksProgress(req, res) { - var abProgresses = req.body - if (!abProgresses || !abProgresses.length) { + async batchUpdateUserAudiobookData(req, res) { + var userAbDataPayloads = req.body + if (!userAbDataPayloads || !userAbDataPayloads.length) { return res.sendStatus(500) } var shouldUpdate = false - abProgresses.forEach((progress) => { - var audiobook = this.db.audiobooks.find(ab => ab.id === progress.audiobookId) + userAbDataPayloads.forEach((userAbData) => { + var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId) if (audiobook) { - var wasUpdated = req.user.updateAudiobookProgress(audiobook, progress) + var wasUpdated = req.user.updateAudiobookData(audiobook, userAbData) if (wasUpdated) shouldUpdate = true } }) diff --git a/server/Server.js b/server/Server.js index b983f0b3..6ba19f20 100644 --- a/server/Server.js +++ b/server/Server.js @@ -462,14 +462,9 @@ class Server { Logger.error('[Server] audiobookProgressUpdate invalid socket client') return } - var audiobookProgress = client.user.updateAudiobookProgress(progressPayload.audiobookId, progressPayload) + var audiobookProgress = client.user.updateAudiobookData(progressPayload.audiobookId, progressPayload) if (audiobookProgress) { await this.db.updateEntity('user', client.user) - - // This audiobook progress is out of date, why? - // var userAudiobook = client.user.getAudiobookJSON(progressPayload.audiobookId) - // Logger.debug(`[Server] Emitting audiobook progress update to clients ${this.getClientsForUser(client.user.id).length}: ${JSON.stringify(userAudiobook)}`) - this.clientEmitter(client.user.id, 'current_user_audiobook_update', { id: progressPayload.audiobookId, data: audiobookProgress || null diff --git a/server/objects/User.js b/server/objects/User.js index deb56d4a..1a666781 100644 --- a/server/objects/User.js +++ b/server/objects/User.js @@ -1,5 +1,5 @@ const Logger = require('../Logger') -const AudiobookProgress = require('./AudiobookProgress') +const UserAudiobookData = require('./UserAudiobookData') class User { constructor(user) { @@ -141,7 +141,7 @@ class User { this.audiobooks = {} for (const key in user.audiobooks) { if (user.audiobooks[key]) { - this.audiobooks[key] = new AudiobookProgress(user.audiobooks[key]) + this.audiobooks[key] = new UserAudiobookData(user.audiobooks[key]) } } } @@ -201,21 +201,21 @@ class User { updateAudiobookProgressFromStream(stream) { if (!this.audiobooks) this.audiobooks = {} if (!this.audiobooks[stream.audiobookId]) { - this.audiobooks[stream.audiobookId] = new AudiobookProgress() + this.audiobooks[stream.audiobookId] = new UserAudiobookData() } - this.audiobooks[stream.audiobookId].updateFromStream(stream) + this.audiobooks[stream.audiobookId].updateProgressFromStream(stream) return this.audiobooks[stream.audiobookId] } - updateAudiobookProgress(audiobook, updatePayload) { + updateAudiobookData(audiobook, updatePayload) { if (!this.audiobooks) this.audiobooks = {} if (!this.audiobooks[audiobook.id]) { - this.audiobooks[audiobook.id] = new AudiobookProgress() + this.audiobooks[audiobook.id] = new UserAudiobookData() this.audiobooks[audiobook.id].audiobookId = audiobook.id } var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload) if (wasUpdated) { - Logger.debug(`[User] Audiobook progress was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`) + Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`) return this.audiobooks[audiobook.id] } return false @@ -251,7 +251,7 @@ class User { if (!this.audiobooks || !this.audiobooks[audiobook.id]) { return false } - return this.updateAudiobookProgress(audiobook, { + return this.updateAudiobookData(audiobook, { progress: 0, currentTime: 0, isRead: false, @@ -261,7 +261,7 @@ class User { }) } - deleteAudiobookProgress(audiobookId) { + deleteAudiobookData(audiobookId) { if (!this.audiobooks || !this.audiobooks[audiobookId]) { return false } diff --git a/server/objects/AudiobookProgress.js b/server/objects/UserAudiobookData.js similarity index 93% rename from server/objects/AudiobookProgress.js rename to server/objects/UserAudiobookData.js index 9a119f3b..6cbf3fb4 100644 --- a/server/objects/AudiobookProgress.js +++ b/server/objects/UserAudiobookData.js @@ -1,7 +1,7 @@ const Logger = require('../Logger') const AudioBookmark = require('./AudioBookmark') -class AudiobookProgress { +class UserAudiobookData { constructor(progress) { this.audiobookId = null @@ -24,7 +24,7 @@ class AudiobookProgress { if (!this.bookmarks) return [] return this.bookmarks.filter((b) => { if (!b.toJSON) { - Logger.error(`[AudiobookProgress] Invalid bookmark ${JSON.stringify(b)}`) + Logger.error(`[UserAudiobookData] Invalid bookmark ${JSON.stringify(b)}`) return false } return true @@ -61,7 +61,7 @@ class AudiobookProgress { } } - updateFromStream(stream) { + updateProgressFromStream(stream) { this.audiobookId = stream.audiobookId this.totalDuration = stream.totalDuration this.progress = stream.clientProgress @@ -86,7 +86,7 @@ class AudiobookProgress { update(payload) { var hasUpdates = false - Logger.debug(`[AudiobookProgress] Update called ${JSON.stringify(payload)}`) + Logger.debug(`[UserAudiobookData] Update called ${JSON.stringify(payload)}`) for (const key in payload) { if (payload[key] !== this[key]) { if (key === 'isRead') { @@ -135,4 +135,4 @@ class AudiobookProgress { this.bookmarks = this.bookmarks.filter(bm => bm.time !== time) } } -module.exports = AudiobookProgress \ No newline at end of file +module.exports = UserAudiobookData \ No newline at end of file