Fix:Sync audiobook progress

This commit is contained in:
advplyr 2022-01-05 19:07:32 -06:00
parent 4778d09501
commit 8ee4751cac
3 changed files with 14 additions and 12 deletions

View File

@ -40,7 +40,7 @@ class MeController {
if (!audiobook) { if (!audiobook) {
return res.status(404).send('Audiobook not found') 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) { if (wasUpdated) {
await this.db.updateEntity('user', req.user) await this.db.updateEntity('user', req.user)
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
@ -59,7 +59,7 @@ class MeController {
userAbDataPayloads.forEach((userAbData) => { userAbDataPayloads.forEach((userAbData) => {
var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId) var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId)
if (audiobook) { if (audiobook) {
var wasUpdated = req.user.updateAudiobookData(audiobook, userAbData) var wasUpdated = req.user.updateAudiobookData(audiobook.id, userAbData)
if (wasUpdated) shouldUpdate = true if (wasUpdated) shouldUpdate = true
} }
}) })

View File

@ -140,7 +140,9 @@ class User {
if (user.audiobooks) { if (user.audiobooks) {
this.audiobooks = {} this.audiobooks = {}
for (const key in user.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]) this.audiobooks[key] = new UserAudiobookData(user.audiobooks[key])
} }
} }
@ -207,16 +209,16 @@ class User {
return this.audiobooks[stream.audiobookId] return this.audiobooks[stream.audiobookId]
} }
updateAudiobookData(audiobook, updatePayload) { updateAudiobookData(audiobookId, updatePayload) {
if (!this.audiobooks) this.audiobooks = {} if (!this.audiobooks) this.audiobooks = {}
if (!this.audiobooks[audiobook.id]) { if (!this.audiobooks[audiobookId]) {
this.audiobooks[audiobook.id] = new UserAudiobookData() this.audiobooks[audiobookId] = new UserAudiobookData()
this.audiobooks[audiobook.id].audiobookId = audiobook.id this.audiobooks[audiobookId].audiobookId = audiobookId
} }
var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload) var wasUpdated = this.audiobooks[audiobookId].update(updatePayload)
if (wasUpdated) { if (wasUpdated) {
// Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`) // Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobookId])}`)
return this.audiobooks[audiobook.id] return this.audiobooks[audiobookId]
} }
return false return false
} }
@ -251,7 +253,7 @@ class User {
if (!this.audiobooks || !this.audiobooks[audiobook.id]) { if (!this.audiobooks || !this.audiobooks[audiobook.id]) {
return false return false
} }
return this.updateAudiobookData(audiobook, { return this.updateAudiobookData(audiobook.id, {
progress: 0, progress: 0,
currentTime: 0, currentTime: 0,
isRead: false, isRead: false,

View File

@ -86,7 +86,7 @@ class UserAudiobookData {
update(payload) { update(payload) {
var hasUpdates = false var hasUpdates = false
for (const key in payload) { for (const key in payload) {
if (payload[key] !== this[key]) { if (this[key] !== undefined && payload[key] !== this[key]) {
if (key === 'isRead') { if (key === 'isRead') {
if (!payload[key]) { // Updating to Not Read - Reset progress and current time if (!payload[key]) { // Updating to Not Read - Reset progress and current time
this.finishedAt = null this.finishedAt = null