From 860c7aac80b58d34c63d3f25710e8190e2446ae1 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 28 Jan 2023 14:20:00 -0600 Subject: [PATCH] Fix:Refreshing server media progress after local sync --- .../audiobookshelf/app/server/ApiHandler.kt | 15 +++++++++----- layouts/default.vue | 20 +++++++++++++------ pages/bookshelf/index.vue | 11 ++++++++-- plugins/axios.js | 3 +-- store/user.js | 2 +- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt b/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt index 900584f8..1aa9772a 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/server/ApiHandler.kt @@ -32,8 +32,8 @@ class ApiHandler(var ctx:Context) { data class LocalMediaProgressSyncPayload(val localMediaProgress:List) @JsonIgnoreProperties(ignoreUnknown = true) - data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List) - data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int) + data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List, val serverProgressUpdates:List) + data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int, var serverProgressUpdates:List) fun getRequest(endpoint:String, httpClient:OkHttpClient?, config:ServerConnectionConfig?, cb: (JSObject) -> Unit) { val address = config?.address ?: DeviceManager.serverAddress @@ -254,7 +254,7 @@ class ApiHandler(var ctx:Context) { fun syncMediaProgress(cb: (LocalMediaProgressSyncResultsPayload) -> Unit) { if (!isOnline()) { Log.d(tag, "Error not online") - cb(LocalMediaProgressSyncResultsPayload(0,0,0)) + cb(LocalMediaProgressSyncResultsPayload(0,0,0, mutableListOf())) return } @@ -263,7 +263,7 @@ class ApiHandler(var ctx:Context) { it.serverConnectionConfigId == DeviceManager.serverConnectionConfig?.id } - val localSyncResultsPayload = LocalMediaProgressSyncResultsPayload(localMediaProgress.size,0, 0) + val localSyncResultsPayload = LocalMediaProgressSyncResultsPayload(localMediaProgress.size,0, 0, mutableListOf()) if (localMediaProgress.isNotEmpty()) { Log.d(tag, "Sending sync local progress request with ${localMediaProgress.size} progress items") @@ -279,16 +279,21 @@ class ApiHandler(var ctx:Context) { val progressSyncResponsePayload = jacksonMapper.readValue(it.toString()) localSyncResultsPayload.numLocalProgressUpdates = progressSyncResponsePayload.localProgressUpdates.size + localSyncResultsPayload.serverProgressUpdates = progressSyncResponsePayload.serverProgressUpdates localSyncResultsPayload.numServerProgressUpdates = progressSyncResponsePayload.numServerProgressUpdates Log.d(tag, "Media Progress Sync | Local Updates: $localSyncResultsPayload") if (progressSyncResponsePayload.localProgressUpdates.isNotEmpty()) { // Update all local media progress progressSyncResponsePayload.localProgressUpdates.forEach { localMediaProgress -> - MediaEventManager.syncEvent(localMediaProgress, "Received from server sync local API request") + MediaEventManager.syncEvent(localMediaProgress, "Local progress updated. Received from server sync local API request") DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress) } } + + progressSyncResponsePayload.serverProgressUpdates.forEach { localMediaProgress -> + MediaEventManager.syncEvent(localMediaProgress, "Server progress updated. Received from server sync local API request") + } } cb(localSyncResultsPayload) diff --git a/layouts/default.vue b/layouts/default.vue index 4ef44e8b..5ea95dda 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -183,12 +183,19 @@ export default { this.$store.commit('setLastLocalMediaSyncResults', null) return } - const { numLocalMediaProgressForServer, numServerProgressUpdates, numLocalProgressUpdates } = response + const { numLocalMediaProgressForServer, numServerProgressUpdates, numLocalProgressUpdates, serverProgressUpdates } = response if (numLocalMediaProgressForServer > 0) { response.syncedAt = Date.now() response.serverConfigName = this.$store.getters['user/getServerConfigName'] this.$store.commit('setLastLocalMediaSyncResults', response) + if (serverProgressUpdates && serverProgressUpdates.length) { + serverProgressUpdates.forEach((progress) => { + console.log(`[default] Server progress was updated ${progress.id}`) + this.$store.commit('user/updateUserMediaProgress', progress) + }) + } + if (numServerProgressUpdates > 0 || numLocalProgressUpdates > 0) { console.log(`[default] ${numServerProgressUpdates} Server progress updates | ${numLocalProgressUpdates} Local progress updates`) } else { @@ -199,7 +206,8 @@ export default { this.$store.commit('setLastLocalMediaSyncResults', null) } }, - async userUpdated(user) { + userUpdated(user) { + console.log('[default] userUpdated:', JSON.stringify(user)) if (this.user && this.user.id == user.id) { this.$store.commit('user/setUser', user) } @@ -210,7 +218,7 @@ export default { // Update local media progress if exists const localProg = await this.$db.getLocalMediaProgressForServerItem({ libraryItemId: prog.libraryItemId, episodeId: prog.episodeId }) - var newLocalMediaProgress = null + let newLocalMediaProgress = null if (localProg && localProg.lastUpdate < prog.lastUpdate) { if (localProg.currentTime == prog.currentTime && localProg.isFinished == prog.isFinished) { console.log('[default] syncing progress server lastUpdate > local lastUpdate but currentTime and isFinished is equal') @@ -229,12 +237,12 @@ export default { } else if (!localProg) { // Check if local library item exists // local media progress may not exist yet if it hasn't been played - var localLibraryItem = await this.$db.getLocalLibraryItemByLId(prog.libraryItemId) + const localLibraryItem = await this.$db.getLocalLibraryItemByLId(prog.libraryItemId) if (localLibraryItem) { if (prog.episodeId) { // If episode check if local episode exists - var lliEpisodes = localLibraryItem.media.episodes || [] - var localEpisode = lliEpisodes.find((ep) => ep.serverEpisodeId === prog.episodeId) + const lliEpisodes = localLibraryItem.media.episodes || [] + const localEpisode = lliEpisodes.find((ep) => ep.serverEpisodeId === prog.episodeId) if (localEpisode) { // Add new local media progress const payload = { diff --git a/pages/bookshelf/index.vue b/pages/bookshelf/index.vue index 95e7f452..1e66c8ab 100644 --- a/pages/bookshelf/index.vue +++ b/pages/bookshelf/index.vue @@ -45,7 +45,14 @@ export default { networkConnected(newVal) { // Update shelves when network connect status changes console.log(`Network changed to ${newVal} - fetch categories`) - this.fetchCategories() + + if (newVal) { + setTimeout(() => { + this.fetchCategories() + }, 4000) + } else { + this.fetchCategories() + } } }, computed: { @@ -123,7 +130,7 @@ export default { this.localLibraryItems = await this.$db.getLocalLibraryItems() const localCategories = await this.getLocalMediaItemCategories() - if (this.user && this.currentLibraryId) { + if (this.user && this.currentLibraryId && this.networkConnected) { const categories = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`).catch((error) => { console.error('Failed to fetch categories', error) return [] diff --git a/plugins/axios.js b/plugins/axios.js index 7d5a3d89..3eb40c64 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -27,7 +27,6 @@ export default function ({ $axios, store }) { }) $axios.onError(error => { - const code = parseInt(error.response && error.response.status) - console.error('Axios error code', code) + console.error('Axios error code', error) }) } \ No newline at end of file diff --git a/store/user.js b/store/user.js index 1e3fc578..153766fd 100644 --- a/store/user.js +++ b/store/user.js @@ -124,7 +124,7 @@ export const mutations = { }, updateUserMediaProgress(state, data) { if (!data || !state.user) return - var mediaProgressIndex = state.user.mediaProgress.findIndex(mp => mp.id === data.id) + const mediaProgressIndex = state.user.mediaProgress.findIndex(mp => mp.id === data.id) if (mediaProgressIndex >= 0) { state.user.mediaProgress.splice(mediaProgressIndex, 1, data) } else {