diff --git a/client/layouts/default.vue b/client/layouts/default.vue index b6e67dba..15f30ffa 100644 --- a/client/layouts/default.vue +++ b/client/layouts/default.vue @@ -178,6 +178,7 @@ export default { this.$store.commit('users/updateUser', user) }, currentUserAudiobookUpdate(payload) { + // console.log('Received user audiobook update', payload) this.$store.commit('user/updateUserAudiobook', payload) }, downloadToastClick(download) { diff --git a/client/package.json b/client/package.json index 36aadf6a..f75602ac 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "audiobookshelf-client", - "version": "1.5.2", + "version": "1.5.3", "description": "Audiobook manager and player", "main": "index.js", "scripts": { diff --git a/package.json b/package.json index 757fc6c7..d06c28cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "audiobookshelf", - "version": "1.5.2", + "version": "1.5.3", "description": "Self-hosted audiobook server for managing and playing audiobooks", "main": "index.js", "scripts": { diff --git a/server/Server.js b/server/Server.js index 4c7bf45f..59518e55 100644 --- a/server/Server.js +++ b/server/Server.js @@ -46,7 +46,7 @@ class Server { this.watcher = new Watcher(this.AudiobookPath) this.coverController = new CoverController(this.db, this.MetadataPath, this.AudiobookPath) this.scanner = new Scanner(this.AudiobookPath, this.MetadataPath, this.db, this.coverController, this.emitter.bind(this)) - this.streamManager = new StreamManager(this.db, this.MetadataPath, this.emitter.bind(this)) + this.streamManager = new StreamManager(this.db, this.MetadataPath, this.emitter.bind(this), this.clientEmitter.bind(this)) this.rssFeeds = new RssFeeds(this.Port, this.db) this.downloadManager = new DownloadManager(this.db, this.MetadataPath, this.AudiobookPath, this.emitter.bind(this)) this.apiController = new ApiController(this.MetadataPath, this.db, this.scanner, this.auth, this.streamManager, this.rssFeeds, this.downloadManager, this.coverController, this.backupManager, this.watcher, this.emitter.bind(this), this.clientEmitter.bind(this)) @@ -453,18 +453,23 @@ class Server { res.sendStatus(200) } - audiobookProgressUpdate(socket, progressPayload) { + async audiobookProgressUpdate(socket, progressPayload) { var client = socket.sheepClient if (!client || !client.user) { Logger.error('[Server] audiobookProgressUpdate invalid socket client') return } - var hasUpdates = client.user.updateAudiobookProgress(progressPayload.audiobookId, progressPayload) - if (hasUpdates) { - var userAudiobook = client.user.getAudiobookJSON(progressPayload.audiobookId) - socket.emit('current_user_audiobook_update', { + var audiobookProgress = client.user.updateAudiobookProgress(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: userAudiobook || null + data: audiobookProgress || null }) } } @@ -478,8 +483,9 @@ class Server { var userAudiobook = client.user.createBookmark(payload) if (userAudiobook) { await this.db.updateEntity('user', client.user) - socket.emit('bookmark_created', payload.time) - socket.emit('current_user_audiobook_update', { + + this.clientEmitter(client.user.id, 'bookmark_created', payload.time) + this.clientEmitter(client.user.id, 'current_user_audiobook_update', { id: userAudiobook.audiobookId, data: userAudiobook || null }) diff --git a/server/StreamManager.js b/server/StreamManager.js index 431d9cba..68b6f3a6 100644 --- a/server/StreamManager.js +++ b/server/StreamManager.js @@ -5,10 +5,11 @@ const fs = require('fs-extra') const Path = require('path') class StreamManager { - constructor(db, MetadataPath, emitter) { + constructor(db, MetadataPath, emitter, clientEmitter) { this.db = db this.emitter = emitter + this.clientEmitter = clientEmitter this.MetadataPath = MetadataPath this.streams = [] @@ -157,7 +158,7 @@ class StreamManager { this.db.updateEntity('user', client.user) if (userAudiobook) { - socket.emit('current_user_audiobook_update', { + this.clientEmitter(client.user.id, 'current_user_audiobook_update', { id: userAudiobook.audiobookId, data: userAudiobook.toJSON() }) diff --git a/server/objects/AudiobookProgress.js b/server/objects/AudiobookProgress.js index abd4f5aa..23f24814 100644 --- a/server/objects/AudiobookProgress.js +++ b/server/objects/AudiobookProgress.js @@ -1,3 +1,4 @@ +const Logger = require('../Logger') const AudioBookmark = require('./AudioBookmark') class AudiobookProgress { @@ -79,6 +80,7 @@ class AudiobookProgress { update(payload) { var hasUpdates = false + Logger.debug(`[AudiobookProgress] Update called ${JSON.stringify(payload)}`) for (const key in payload) { if (payload[key] !== this[key]) { if (key === 'isRead') { diff --git a/server/objects/User.js b/server/objects/User.js index 161446a6..0e8b459d 100644 --- a/server/objects/User.js +++ b/server/objects/User.js @@ -1,3 +1,4 @@ +const Logger = require('../Logger') const AudiobookProgress = require('./AudiobookProgress') class User { @@ -212,7 +213,12 @@ class User { this.audiobooks[audiobook.id] = new AudiobookProgress() this.audiobooks[audiobook.id].audiobookId = audiobook.id } - return this.audiobooks[audiobook.id].update(updatePayload) + var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload) + if (wasUpdated) { + Logger.debug(`[User] Audiobook progress was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`) + return this.audiobooks[audiobook.id] + } + return false } // Returns Boolean If update was made