Fix:Refreshing server media progress after local sync

This commit is contained in:
advplyr 2023-01-28 14:20:00 -06:00
parent 8bab4ae383
commit 860c7aac80
5 changed files with 35 additions and 16 deletions

View file

@ -32,8 +32,8 @@ class ApiHandler(var ctx:Context) {
data class LocalMediaProgressSyncPayload(val localMediaProgress:List<LocalMediaProgress>)
@JsonIgnoreProperties(ignoreUnknown = true)
data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List<LocalMediaProgress>)
data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int)
data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List<LocalMediaProgress>, val serverProgressUpdates:List<MediaProgress>)
data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int, var serverProgressUpdates:List<MediaProgress>)
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<MediaProgressSyncResponsePayload>(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)

View file

@ -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 = {

View file

@ -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 []

View file

@ -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)
})
}

View file

@ -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 {