mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-10 05:49:37 +00:00
New data model update MeController user progress routes
This commit is contained in:
parent
c4eeb1cfb7
commit
1cf9e85272
13 changed files with 234 additions and 281 deletions
|
|
@ -5,7 +5,6 @@ class LibraryItemProgress {
|
|||
this.id = null // Same as library item id
|
||||
this.libraryItemId = null
|
||||
|
||||
this.totalDuration = null // seconds
|
||||
this.progress = null // 0 to 1
|
||||
this.currentTime = null // seconds
|
||||
this.isFinished = false
|
||||
|
|
@ -23,7 +22,6 @@ class LibraryItemProgress {
|
|||
return {
|
||||
id: this.id,
|
||||
libraryItemId: this.libraryItemId,
|
||||
totalDuration: this.totalDuration,
|
||||
progress: this.progress,
|
||||
currentTime: this.currentTime,
|
||||
isFinished: this.isFinished,
|
||||
|
|
@ -36,7 +34,6 @@ class LibraryItemProgress {
|
|||
construct(progress) {
|
||||
this.id = progress.id
|
||||
this.libraryItemId = progress.libraryItemId
|
||||
this.totalDuration = progress.totalDuration
|
||||
this.progress = progress.progress
|
||||
this.currentTime = progress.currentTime
|
||||
this.isFinished = !!progress.isFinished
|
||||
|
|
@ -46,25 +43,40 @@ class LibraryItemProgress {
|
|||
}
|
||||
|
||||
updateProgressFromStream(stream) {
|
||||
this.audiobookId = stream.libraryItemId
|
||||
this.totalDuration = stream.totalDuration
|
||||
this.progress = stream.clientProgress
|
||||
this.currentTime = stream.clientCurrentTime
|
||||
// this.audiobookId = stream.libraryItemId
|
||||
// this.totalDuration = stream.totalDuration
|
||||
// this.progress = stream.clientProgress
|
||||
// this.currentTime = stream.clientCurrentTime
|
||||
// this.lastUpdate = Date.now()
|
||||
|
||||
// if (!this.startedAt) {
|
||||
// this.startedAt = Date.now()
|
||||
// }
|
||||
|
||||
// // If has < 10 seconds remaining mark as read
|
||||
// var timeRemaining = this.totalDuration - this.currentTime
|
||||
// if (timeRemaining < 10) {
|
||||
// this.isFinished = true
|
||||
// this.progress = 1
|
||||
// this.finishedAt = Date.now()
|
||||
// } else {
|
||||
// this.isFinished = false
|
||||
// this.finishedAt = null
|
||||
// }
|
||||
}
|
||||
|
||||
setData(libraryItemId, progress) {
|
||||
this.id = libraryItemId
|
||||
this.libraryItemId = libraryItemId
|
||||
this.progress = Math.min(1, (progress.progress || 0))
|
||||
this.currentTime = progress.currentTime || 0
|
||||
this.isFinished = !!progress.isFinished || this.progress == 1
|
||||
this.lastUpdate = Date.now()
|
||||
|
||||
if (!this.startedAt) {
|
||||
this.startedAt = Date.now()
|
||||
}
|
||||
|
||||
// If has < 10 seconds remaining mark as read
|
||||
var timeRemaining = this.totalDuration - this.currentTime
|
||||
if (timeRemaining < 10) {
|
||||
this.isFinished = true
|
||||
this.progress = 1
|
||||
this.startedAt = Date.now()
|
||||
this.finishedAt = null
|
||||
if (this.isFinished) {
|
||||
this.finishedAt = Date.now()
|
||||
} else {
|
||||
this.isFinished = false
|
||||
this.finishedAt = null
|
||||
this.progress = 1
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -204,18 +204,22 @@ class User {
|
|||
// return this.audiobooks[stream.audiobookId]
|
||||
}
|
||||
|
||||
updateAudiobookData(audiobookId, updatePayload) {
|
||||
// if (!this.audiobooks) this.audiobooks = {}
|
||||
// if (!this.audiobooks[audiobookId]) {
|
||||
// this.audiobooks[audiobookId] = new UserAudiobookData()
|
||||
// this.audiobooks[audiobookId].audiobookId = audiobookId
|
||||
// }
|
||||
// var wasUpdated = this.audiobooks[audiobookId].update(updatePayload)
|
||||
// if (wasUpdated) {
|
||||
// // Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobookId])}`)
|
||||
// return this.audiobooks[audiobookId]
|
||||
// }
|
||||
// return false
|
||||
createUpdateLibraryItemProgress(libraryItemId, updatePayload) {
|
||||
var itemProgress = this.libraryItemProgress.find(li => li.id === libraryItemId)
|
||||
if (!itemProgress) {
|
||||
var newItemProgress = new LibraryItemProgress()
|
||||
newItemProgress.setData(libraryItemId, updatePayload)
|
||||
this.libraryItemProgress.push(newItemProgress)
|
||||
return true
|
||||
}
|
||||
var wasUpdated = itemProgress.update(updatePayload)
|
||||
return wasUpdated
|
||||
}
|
||||
|
||||
removeLibraryItemProgress(libraryItemId) {
|
||||
if (!this.libraryItemProgress.some(lip => lip.id == libraryItemId)) return false
|
||||
this.libraryItemProgress = this.libraryItemProgress.filter(lip => lip != libraryItemId)
|
||||
return true
|
||||
}
|
||||
|
||||
// Returns Boolean If update was made
|
||||
|
|
@ -244,28 +248,6 @@ class User {
|
|||
return madeUpdates
|
||||
}
|
||||
|
||||
resetAudiobookProgress(libraryItem) {
|
||||
// if (!this.audiobooks || !this.audiobooks[libraryItem.id]) {
|
||||
// return false
|
||||
// }
|
||||
// return this.updateAudiobookData(libraryItem.id, {
|
||||
// progress: 0,
|
||||
// currentTime: 0,
|
||||
// isRead: false,
|
||||
// lastUpdate: Date.now(),
|
||||
// startedAt: null,
|
||||
// finishedAt: null
|
||||
// })
|
||||
}
|
||||
|
||||
deleteAudiobookData(audiobookId) {
|
||||
// if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
// return false
|
||||
// }
|
||||
// delete this.audiobooks[audiobookId]
|
||||
// return true
|
||||
}
|
||||
|
||||
checkCanAccessLibrary(libraryId) {
|
||||
if (this.permissions.accessAllLibraries) return true
|
||||
if (!this.librariesAccessible) return false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue