mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-14 15:59:38 +00:00
New data model migration for users, bookmarks and playback sessions
This commit is contained in:
parent
4c2ad3ede5
commit
68b13ae45f
17 changed files with 462 additions and 192 deletions
|
|
@ -7,7 +7,7 @@ const { getId, secondsToTimestamp } = require('../utils/index')
|
|||
const { writeConcatFile } = require('../utils/ffmpegHelpers')
|
||||
const hlsPlaylistGenerator = require('../utils/hlsPlaylistGenerator')
|
||||
|
||||
const UserListeningSession = require('./UserListeningSession')
|
||||
const UserListeningSession = require('./legacy/UserListeningSession')
|
||||
|
||||
class Stream extends EventEmitter {
|
||||
constructor(streamPath, client, libraryItem, transcodeOptions = {}) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
const Logger = require('../Logger')
|
||||
const AudioBookmark = require('./AudioBookmark')
|
||||
const Logger = require('../../Logger')
|
||||
const AudioBookmark = require('../user/AudioBookmark')
|
||||
|
||||
class UserAudiobookData {
|
||||
constructor(progress) {
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
const Logger = require('../Logger')
|
||||
const Logger = require('../../Logger')
|
||||
const date = require('date-and-time')
|
||||
const { getId } = require('../utils/index')
|
||||
const { getId } = require('../../utils/index')
|
||||
|
||||
class UserListeningSession {
|
||||
constructor(session) {
|
||||
|
|
@ -46,7 +46,7 @@ class BookMetadata {
|
|||
subtitle: this.subtitle,
|
||||
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
|
||||
narrators: [...this.narrators],
|
||||
series: this.series.map(s => ({ ...s })),
|
||||
series: this.series.map(s => ({ ...s })), // Series JSONMinimal with name, id and sequence
|
||||
genres: [...this.genres],
|
||||
publishedYear: this.publishedYear,
|
||||
publishedDate: this.publishedDate,
|
||||
|
|
@ -80,6 +80,10 @@ class BookMetadata {
|
|||
}
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new BookMetadata(this.toJSON())
|
||||
}
|
||||
|
||||
get titleIgnorePrefix() {
|
||||
if (!this.title) return ''
|
||||
if (this.title.toLowerCase().startsWith('the ')) {
|
||||
|
|
|
|||
|
|
@ -48,6 +48,10 @@ class PodcastMetadata {
|
|||
return this.toJSON()
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new PodcastMetadata(this.toJSON())
|
||||
}
|
||||
|
||||
searchQuery(query) { // Returns key if match is found
|
||||
var keysToCheck = ['title', 'artist', 'itunesId', 'itunesArtistId']
|
||||
for (var key of keysToCheck) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
class AudioBookmark {
|
||||
constructor(bookmark) {
|
||||
this.libraryItemId = null
|
||||
this.title = null
|
||||
this.time = null
|
||||
this.createdAt = null
|
||||
|
|
@ -11,6 +12,7 @@ class AudioBookmark {
|
|||
|
||||
toJSON() {
|
||||
return {
|
||||
libraryItemId: this.libraryItemId,
|
||||
title: this.title || '',
|
||||
time: this.time,
|
||||
createdAt: this.createdAt
|
||||
|
|
@ -18,12 +20,14 @@ class AudioBookmark {
|
|||
}
|
||||
|
||||
construct(bookmark) {
|
||||
this.libraryItemId = bookmark.libraryItemId
|
||||
this.title = bookmark.title || ''
|
||||
this.time = bookmark.time || 0
|
||||
this.createdAt = bookmark.createdAt
|
||||
}
|
||||
|
||||
setData(time, title) {
|
||||
setData(libraryItemId, time, title) {
|
||||
this.libraryItemId = libraryItemId
|
||||
this.title = title
|
||||
this.time = time
|
||||
this.createdAt = Date.now()
|
||||
99
server/objects/user/LibraryItemProgress.js
Normal file
99
server/objects/user/LibraryItemProgress.js
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
const Logger = require('../../Logger')
|
||||
|
||||
class LibraryItemProgress {
|
||||
constructor(progress) {
|
||||
this.id = null // Same as library item id
|
||||
this.libararyItemId = null
|
||||
|
||||
this.totalDuration = null // seconds
|
||||
this.progress = null // 0 to 1
|
||||
this.currentTime = null // seconds
|
||||
this.isRead = false
|
||||
|
||||
this.lastUpdate = null
|
||||
this.startedAt = null
|
||||
this.finishedAt = null
|
||||
|
||||
if (progress) {
|
||||
this.construct(progress)
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
id: this.id,
|
||||
libararyItemId: this.libararyItemId,
|
||||
totalDuration: this.totalDuration,
|
||||
progress: this.progress,
|
||||
currentTime: this.currentTime,
|
||||
isRead: this.isRead,
|
||||
lastUpdate: this.lastUpdate,
|
||||
startedAt: this.startedAt,
|
||||
finishedAt: this.finishedAt
|
||||
}
|
||||
}
|
||||
|
||||
construct(progress) {
|
||||
this.id = progress.id
|
||||
this.libararyItemId = progress.libararyItemId
|
||||
this.totalDuration = progress.totalDuration
|
||||
this.progress = progress.progress
|
||||
this.currentTime = progress.currentTime
|
||||
this.isRead = !!progress.isRead
|
||||
this.lastUpdate = progress.lastUpdate
|
||||
this.startedAt = progress.startedAt
|
||||
this.finishedAt = progress.finishedAt || null
|
||||
}
|
||||
|
||||
updateProgressFromStream(stream) {
|
||||
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.isRead = true
|
||||
this.progress = 1
|
||||
this.finishedAt = Date.now()
|
||||
} else {
|
||||
this.isRead = false
|
||||
this.finishedAt = null
|
||||
}
|
||||
}
|
||||
|
||||
update(payload) {
|
||||
var hasUpdates = false
|
||||
for (const key in payload) {
|
||||
if (this[key] !== undefined && payload[key] !== this[key]) {
|
||||
if (key === 'isRead') {
|
||||
if (!payload[key]) { // Updating to Not Read - Reset progress and current time
|
||||
this.finishedAt = null
|
||||
this.progress = 0
|
||||
this.currentTime = 0
|
||||
} else { // Updating to Read
|
||||
if (!this.finishedAt) this.finishedAt = Date.now()
|
||||
this.progress = 1
|
||||
}
|
||||
}
|
||||
|
||||
this[key] = payload[key]
|
||||
hasUpdates = true
|
||||
}
|
||||
}
|
||||
if (!this.startedAt) {
|
||||
this.startedAt = Date.now()
|
||||
}
|
||||
if (hasUpdates) {
|
||||
this.lastUpdate = Date.now()
|
||||
}
|
||||
return hasUpdates
|
||||
}
|
||||
}
|
||||
module.exports = LibraryItemProgress
|
||||
103
server/objects/user/PlaybackSession.js
Normal file
103
server/objects/user/PlaybackSession.js
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
const date = require('date-and-time')
|
||||
const { getId } = require('../../utils/index')
|
||||
const { PlayMethod } = require('../../utils/constants')
|
||||
const BookMetadata = require('../metadata/BookMetadata')
|
||||
const PodcastMetadata = require('../metadata/PodcastMetadata')
|
||||
|
||||
class PlaybackSession {
|
||||
constructor(session) {
|
||||
this.id = null
|
||||
this.userId = null
|
||||
this.libraryItemId = null
|
||||
this.mediaType = null
|
||||
this.mediaMetadata = null
|
||||
|
||||
this.playMethod = null
|
||||
|
||||
this.date = null
|
||||
this.dayOfWeek = null
|
||||
|
||||
this.timeListening = null
|
||||
this.startedAt = null
|
||||
this.updatedAt = null
|
||||
|
||||
if (session) {
|
||||
this.construct(session)
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
id: this.id,
|
||||
sessionType: this.sessionType,
|
||||
userId: this.userId,
|
||||
libraryItemId: this.libraryItemId,
|
||||
mediaType: this.mediaType,
|
||||
mediaMetadata: this.mediaMetadata ? this.mediaMetadata.toJSON() : null,
|
||||
playMethod: this.playMethod,
|
||||
date: this.date,
|
||||
dayOfWeek: this.dayOfWeek,
|
||||
timeListening: this.timeListening,
|
||||
lastUpdate: this.lastUpdate,
|
||||
updatedAt: this.updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
construct(session) {
|
||||
this.id = session.id
|
||||
this.sessionType = session.sessionType
|
||||
this.userId = session.userId
|
||||
this.libraryItemId = session.libraryItemId
|
||||
this.mediaType = session.mediaType
|
||||
this.playMethod = session.playMethod
|
||||
|
||||
this.mediaMetadata = null
|
||||
if (session.mediaMetadata) {
|
||||
if (this.mediaType === 'book') {
|
||||
this.mediaMetadata = new BookMetadata(session.mediaMetadata)
|
||||
} else if (this.mediaType === 'podcast') {
|
||||
this.mediaMetadata = new PodcastMetadata(session.mediaMetadata)
|
||||
}
|
||||
}
|
||||
|
||||
this.date = session.date
|
||||
this.dayOfWeek = session.dayOfWeek
|
||||
|
||||
this.timeListening = session.timeListening || null
|
||||
this.startedAt = session.startedAt
|
||||
this.updatedAt = session.updatedAt || null
|
||||
}
|
||||
|
||||
setData(libraryItem, user) {
|
||||
this.id = getId('ls')
|
||||
this.userId = user.id
|
||||
this.libraryItemId = libraryItem.id
|
||||
this.mediaType = libraryItem.mediaType
|
||||
this.mediaMetadata = libraryItem.media.metadata.clone()
|
||||
this.playMethod = PlayMethod.TRANSCODE
|
||||
|
||||
this.timeListening = 0
|
||||
this.startedAt = Date.now()
|
||||
this.updatedAt = Date.now()
|
||||
}
|
||||
|
||||
addListeningTime(timeListened) {
|
||||
if (timeListened && !isNaN(timeListened)) {
|
||||
if (!this.date) {
|
||||
// Set date info on first listening update
|
||||
this.date = date.format(new Date(), 'YYYY-MM-DD')
|
||||
this.dayOfWeek = date.format(new Date(), 'dddd')
|
||||
}
|
||||
|
||||
this.timeListening += timeListened
|
||||
this.updatedAt = Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
// New date since start of listening session
|
||||
checkDateRollover() {
|
||||
if (!this.date) return false
|
||||
return date.format(new Date(), 'YYYY-MM-DD') !== this.date
|
||||
}
|
||||
}
|
||||
module.exports = PlaybackSession
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
const Logger = require('../Logger')
|
||||
const UserAudiobookData = require('./UserAudiobookData')
|
||||
const Logger = require('../../Logger')
|
||||
const { isObject } = require('../../utils')
|
||||
const AudioBookmark = require('./AudioBookmark')
|
||||
const LibraryItemProgress = require('./LibraryItemProgress')
|
||||
|
||||
class User {
|
||||
constructor(user) {
|
||||
|
|
@ -13,7 +15,9 @@ class User {
|
|||
this.isLocked = false
|
||||
this.lastSeen = null
|
||||
this.createdAt = null
|
||||
this.audiobooks = null
|
||||
|
||||
this.libraryItemProgress = []
|
||||
this.bookmarks = []
|
||||
|
||||
this.settings = {}
|
||||
this.permissions = {}
|
||||
|
|
@ -70,17 +74,6 @@ class User {
|
|||
}
|
||||
}
|
||||
|
||||
audiobooksToJSON() {
|
||||
if (!this.audiobooks) return null
|
||||
var _map = {}
|
||||
for (const key in this.audiobooks) {
|
||||
if (this.audiobooks[key]) {
|
||||
_map[key] = this.audiobooks[key].toJSON()
|
||||
}
|
||||
}
|
||||
return _map
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
id: this.id,
|
||||
|
|
@ -89,7 +82,8 @@ class User {
|
|||
type: this.type,
|
||||
stream: this.stream,
|
||||
token: this.token,
|
||||
audiobooks: this.audiobooksToJSON(),
|
||||
libraryItemProgress: this.libraryItemProgress ? this.libraryItemProgress.map(li => li.toJSON()) : [],
|
||||
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
|
||||
isActive: this.isActive,
|
||||
isLocked: this.isLocked,
|
||||
lastSeen: this.lastSeen,
|
||||
|
|
@ -107,7 +101,7 @@ class User {
|
|||
type: this.type,
|
||||
stream: this.stream,
|
||||
token: this.token,
|
||||
audiobooks: this.audiobooksToJSON(),
|
||||
libraryItemProgress: this.libraryItemProgress ? this.libraryItemProgress.map(li => li.toJSON()) : [],
|
||||
isActive: this.isActive,
|
||||
isLocked: this.isLocked,
|
||||
lastSeen: this.lastSeen,
|
||||
|
|
@ -138,16 +132,17 @@ class User {
|
|||
this.type = user.type
|
||||
this.stream = user.stream || null
|
||||
this.token = user.token
|
||||
if (user.audiobooks) {
|
||||
this.audiobooks = {}
|
||||
for (const key in user.audiobooks) {
|
||||
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.libraryItemProgress = []
|
||||
if (user.libraryItemProgress) {
|
||||
this.libraryItemProgress = user.libraryItemProgress.map(li => new LibraryItemProgress(li))
|
||||
}
|
||||
|
||||
this.bookmarks = []
|
||||
if (user.bookmarks) {
|
||||
this.bookmarks = user.bookmarks.map(bm => new AudioBookmark(bm))
|
||||
}
|
||||
|
||||
this.isActive = (user.isActive === undefined || user.type === 'root') ? true : !!user.isActive
|
||||
this.isLocked = user.type === 'root' ? false : !!user.isLocked
|
||||
this.lastSeen = user.lastSeen || null
|
||||
|
|
@ -202,26 +197,26 @@ class User {
|
|||
}
|
||||
|
||||
updateAudiobookProgressFromStream(stream) {
|
||||
if (!this.audiobooks) this.audiobooks = {}
|
||||
if (!this.audiobooks[stream.audiobookId]) {
|
||||
this.audiobooks[stream.audiobookId] = new UserAudiobookData()
|
||||
}
|
||||
this.audiobooks[stream.audiobookId].updateProgressFromStream(stream)
|
||||
return this.audiobooks[stream.audiobookId]
|
||||
// if (!this.audiobooks) this.audiobooks = {}
|
||||
// if (!this.audiobooks[stream.audiobookId]) {
|
||||
// this.audiobooks[stream.audiobookId] = new UserAudiobookData()
|
||||
// }
|
||||
// this.audiobooks[stream.audiobookId].updateProgressFromStream(stream)
|
||||
// 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
|
||||
// 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
|
||||
}
|
||||
|
||||
// Returns Boolean If update was made
|
||||
|
|
@ -251,25 +246,25 @@ class User {
|
|||
}
|
||||
|
||||
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
|
||||
})
|
||||
// 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
|
||||
// if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
// return false
|
||||
// }
|
||||
// delete this.audiobooks[audiobookId]
|
||||
// return true
|
||||
}
|
||||
|
||||
checkCanAccessLibrary(libraryId) {
|
||||
|
|
@ -278,59 +273,60 @@ class User {
|
|||
return this.librariesAccessible.includes(libraryId)
|
||||
}
|
||||
|
||||
getAudiobookJSON(audiobookId) {
|
||||
if (!this.audiobooks) return null
|
||||
return this.audiobooks[audiobookId] ? this.audiobooks[audiobookId].toJSON() : null
|
||||
getLibraryItemProgress(libraryItemId) {
|
||||
if (!this.libraryItemProgress) return null
|
||||
var progress = this.libraryItemProgress.find(lip => lip.id === libraryItemId)
|
||||
return progress ? progress.toJSON() : null
|
||||
}
|
||||
|
||||
createBookmark({ audiobookId, time, title }) {
|
||||
if (!this.audiobooks) this.audiobooks = {}
|
||||
if (!this.audiobooks[audiobookId]) {
|
||||
this.audiobooks[audiobookId] = new UserAudiobookData()
|
||||
this.audiobooks[audiobookId].audiobookId = audiobookId
|
||||
}
|
||||
if (this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
return {
|
||||
error: 'Bookmark already exists'
|
||||
}
|
||||
}
|
||||
createBookmark({ libraryItemId, time, title }) {
|
||||
// if (!this.audiobooks) this.audiobooks = {}
|
||||
// if (!this.audiobooks[audiobookId]) {
|
||||
// this.audiobooks[audiobookId] = new UserAudiobookData()
|
||||
// this.audiobooks[audiobookId].audiobookId = audiobookId
|
||||
// }
|
||||
// if (this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
// return {
|
||||
// error: 'Bookmark already exists'
|
||||
// }
|
||||
// }
|
||||
|
||||
var success = this.audiobooks[audiobookId].createBookmark(time, title)
|
||||
if (success) return this.audiobooks[audiobookId]
|
||||
return null
|
||||
// var success = this.audiobooks[audiobookId].createBookmark(time, title)
|
||||
// if (success) return this.audiobooks[audiobookId]
|
||||
// return null
|
||||
}
|
||||
|
||||
updateBookmark({ audiobookId, time, title }) {
|
||||
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
return {
|
||||
error: 'Invalid Audiobook'
|
||||
}
|
||||
}
|
||||
if (!this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
return {
|
||||
error: 'Bookmark does not exist'
|
||||
}
|
||||
}
|
||||
// if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
// return {
|
||||
// error: 'Invalid Audiobook'
|
||||
// }
|
||||
// }
|
||||
// if (!this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
// return {
|
||||
// error: 'Bookmark does not exist'
|
||||
// }
|
||||
// }
|
||||
|
||||
var success = this.audiobooks[audiobookId].updateBookmark(time, title)
|
||||
if (success) return this.audiobooks[audiobookId]
|
||||
return null
|
||||
// var success = this.audiobooks[audiobookId].updateBookmark(time, title)
|
||||
// if (success) return this.audiobooks[audiobookId]
|
||||
// return null
|
||||
}
|
||||
|
||||
deleteBookmark({ audiobookId, time }) {
|
||||
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
return {
|
||||
error: 'Invalid Audiobook'
|
||||
}
|
||||
}
|
||||
if (!this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
return {
|
||||
error: 'Bookmark does not exist'
|
||||
}
|
||||
}
|
||||
// if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
||||
// return {
|
||||
// error: 'Invalid Audiobook'
|
||||
// }
|
||||
// }
|
||||
// if (!this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
||||
// return {
|
||||
// error: 'Bookmark does not exist'
|
||||
// }
|
||||
// }
|
||||
|
||||
this.audiobooks[audiobookId].deleteBookmark(time)
|
||||
return this.audiobooks[audiobookId]
|
||||
// this.audiobooks[audiobookId].deleteBookmark(time)
|
||||
// return this.audiobooks[audiobookId]
|
||||
}
|
||||
|
||||
syncLocalUserAudiobookData(localUserAudiobookData, audiobook) {
|
||||
Loading…
Add table
Add a link
Reference in a new issue