mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-04 18:24:46 +02:00
Fix player content url, update user progress object include media entity id, update reset progress route
This commit is contained in:
parent
3d2bbc7719
commit
6a06ba4327
10 changed files with 65 additions and 35 deletions
|
@ -80,15 +80,23 @@ class PlaybackSessionManager {
|
|||
}
|
||||
|
||||
async syncSession(user, session, syncData) {
|
||||
var libraryItem = this.db.libraryItems.find(li => li.id === session.libraryItemId)
|
||||
if (!libraryItem) {
|
||||
Logger.error(`[PlaybackSessionManager] syncSession Library Item not found "${sessino.libraryItemId}"`)
|
||||
return
|
||||
}
|
||||
|
||||
session.currentTime = syncData.currentTime
|
||||
session.addListeningTime(syncData.timeListened)
|
||||
Logger.debug(`[PlaybackSessionManager] syncSession "${session.id}" | Total Time Listened: ${session.timeListening}`)
|
||||
|
||||
const itemProgressUpdate = {
|
||||
mediaEntityId: syncData.mediaEntityId || null,
|
||||
duration: syncData.duration,
|
||||
currentTime: syncData.currentTime,
|
||||
progress: session.progress
|
||||
}
|
||||
var wasUpdated = user.createUpdateLibraryItemProgress(session.libraryItemId, itemProgressUpdate)
|
||||
var wasUpdated = user.createUpdateLibraryItemProgress(libraryItem, itemProgressUpdate)
|
||||
if (wasUpdated) {
|
||||
await this.db.updateEntity('user', user)
|
||||
var itemProgress = user.getLibraryItemProgress(session.libraryItemId)
|
||||
|
@ -112,6 +120,8 @@ class PlaybackSessionManager {
|
|||
}
|
||||
|
||||
saveSession(session) {
|
||||
if (!session.timeListening) return // Do not save a session with no listening time
|
||||
|
||||
if (session.lastSave) {
|
||||
return this.db.updateEntity('session', session)
|
||||
} else {
|
||||
|
|
|
@ -23,7 +23,7 @@ class MeController {
|
|||
return res.sendStatus(200)
|
||||
}
|
||||
await this.db.updateEntity('user', req.user)
|
||||
this.clientEmitter(req.user.id, 'user_item_progress_updated', { id: libraryItem.id, data: null })
|
||||
// this.clientEmitter(req.user.id, 'user_item_progress_updated', { id: libraryItem.id, data: null })
|
||||
|
||||
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
||||
res.sendStatus(200)
|
||||
|
@ -35,7 +35,7 @@ class MeController {
|
|||
if (!libraryItem) {
|
||||
return res.status(404).send('Item not found')
|
||||
}
|
||||
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem.id, req.body)
|
||||
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem, req.body)
|
||||
if (wasUpdated) {
|
||||
await this.db.updateEntity('user', req.user)
|
||||
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
||||
|
@ -54,7 +54,7 @@ class MeController {
|
|||
itemProgressPayloads.forEach((itemProgress) => {
|
||||
var libraryItem = this.db.libraryItems.find(li => li.id === itemProgress.id) // Make sure this library item exists
|
||||
if (libraryItem) {
|
||||
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem.id, itemProgress)
|
||||
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem, itemProgress)
|
||||
if (wasUpdated) shouldUpdate = true
|
||||
} else {
|
||||
Logger.error(`[MeController] batchUpdateLibraryItemProgress: Library Item does not exist ${itemProgress.id}`)
|
||||
|
|
|
@ -4,7 +4,9 @@ class LibraryItemProgress {
|
|||
constructor(progress) {
|
||||
this.id = null // Same as library item id
|
||||
this.libraryItemId = null
|
||||
this.mediaEntityId = null
|
||||
|
||||
this.duration = null
|
||||
this.progress = null // 0 to 1
|
||||
this.currentTime = null // seconds
|
||||
this.isFinished = false
|
||||
|
@ -22,6 +24,8 @@ class LibraryItemProgress {
|
|||
return {
|
||||
id: this.id,
|
||||
libraryItemId: this.libraryItemId,
|
||||
mediaEntityId: this.mediaEntityId,
|
||||
duration: this.duration,
|
||||
progress: this.progress,
|
||||
currentTime: this.currentTime,
|
||||
isFinished: this.isFinished,
|
||||
|
@ -34,6 +38,8 @@ class LibraryItemProgress {
|
|||
construct(progress) {
|
||||
this.id = progress.id
|
||||
this.libraryItemId = progress.libraryItemId
|
||||
this.mediaEntityId = progress.mediaEntityId || null
|
||||
this.duration = progress.duration || 0
|
||||
this.progress = progress.progress
|
||||
this.currentTime = progress.currentTime
|
||||
this.isFinished = !!progress.isFinished
|
||||
|
@ -46,9 +52,11 @@ class LibraryItemProgress {
|
|||
return !this.isFinished && this.progress > 0
|
||||
}
|
||||
|
||||
setData(libraryItemId, progress) {
|
||||
setData(libraryItemId, mediaEntityId, progress) {
|
||||
this.id = libraryItemId
|
||||
this.libraryItemId = libraryItemId
|
||||
this.mediaEntityId = mediaEntityId
|
||||
this.duration = progress.duration || 0
|
||||
this.progress = Math.min(1, (progress.progress || 0))
|
||||
this.currentTime = progress.currentTime || 0
|
||||
this.isFinished = !!progress.isFinished || this.progress == 1
|
||||
|
|
|
@ -211,11 +211,20 @@ class User {
|
|||
return this.libraryItemProgress.find(lip => lip.id === libraryItemId)
|
||||
}
|
||||
|
||||
createUpdateLibraryItemProgress(libraryItemId, updatePayload) {
|
||||
var itemProgress = this.libraryItemProgress.find(li => li.id === libraryItemId)
|
||||
createUpdateLibraryItemProgress(libraryItem, updatePayload) {
|
||||
var itemProgress = this.libraryItemProgress.find(li => li.id === libraryItem.id)
|
||||
if (!itemProgress) {
|
||||
var newItemProgress = new LibraryItemProgress()
|
||||
newItemProgress.setData(libraryItemId, updatePayload)
|
||||
|
||||
var mediaEntity = null
|
||||
if (updatePayload.mediaEntityId) mediaEntity = libraryItem.media.getMediaEntityById(updatePayload.mediaEntityId)
|
||||
if (!mediaEntity) mediaEntity = libraryItem.media.getPlaybackMediaEntity()
|
||||
if (!mediaEntity) {
|
||||
Logger.error(`[User] createUpdateLibraryItemProgress invalid library item has no playback media entity "${libraryItem.id}"`)
|
||||
return false
|
||||
}
|
||||
|
||||
newItemProgress.setData(libraryItem.id, mediaEntity.id, updatePayload)
|
||||
this.libraryItemProgress.push(newItemProgress)
|
||||
return true
|
||||
}
|
||||
|
@ -225,7 +234,7 @@ class User {
|
|||
|
||||
removeLibraryItemProgress(libraryItemId) {
|
||||
if (!this.libraryItemProgress.some(lip => lip.id == libraryItemId)) return false
|
||||
this.libraryItemProgress = this.libraryItemProgress.filter(lip => lip != libraryItemId)
|
||||
this.libraryItemProgress = this.libraryItemProgress.filter(lip => lip.id != libraryItemId)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -338,6 +338,8 @@ function cleanUserObject(db, userObj) {
|
|||
var liProgress = new LibraryItemProgress() // New Progress Object
|
||||
liProgress.id = userAudiobookData.audiobookId // This ID will be updated when library item is created
|
||||
liProgress.libraryItemId = userAudiobookData.audiobookId
|
||||
liProgress.mediaEntityId = userAudiobookData.audiobookId
|
||||
liProgress.duration = userAudiobookData.totalDuration
|
||||
liProgress.isFinished = !!userAudiobookData.isRead
|
||||
Object.keys(liProgress.toJSON()).forEach((key) => {
|
||||
if (userAudiobookData[key] !== undefined) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue