New data model migration for users, bookmarks and playback sessions

This commit is contained in:
advplyr 2022-03-15 18:57:15 -05:00
parent 4c2ad3ede5
commit 68b13ae45f
17 changed files with 462 additions and 192 deletions

View file

@ -0,0 +1,136 @@
const Logger = require('../../Logger')
const AudioBookmark = require('../user/AudioBookmark')
class UserAudiobookData {
constructor(progress) {
this.audiobookId = 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
this.bookmarks = []
if (progress) {
this.construct(progress)
}
}
bookmarksToJSON() {
if (!this.bookmarks) return []
return this.bookmarks.filter((b) => {
if (!b.toJSON) {
Logger.error(`[UserAudiobookData] Invalid bookmark ${JSON.stringify(b)}`)
return false
}
return true
}).map(b => b.toJSON())
}
toJSON() {
return {
audiobookId: this.audiobookId,
totalDuration: this.totalDuration,
progress: this.progress,
currentTime: this.currentTime,
isRead: this.isRead,
lastUpdate: this.lastUpdate,
startedAt: this.startedAt,
finishedAt: this.finishedAt,
bookmarks: this.bookmarksToJSON()
}
}
construct(progress) {
this.audiobookId = progress.audiobookId
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
if (progress.bookmarks) {
this.bookmarks = progress.bookmarks.map(b => new AudioBookmark(b))
} else {
this.bookmarks = []
}
}
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
}
checkBookmarkExists(time) {
return this.bookmarks.find(bm => bm.time === time)
}
createBookmark(time, title) {
var newBookmark = new AudioBookmark()
newBookmark.setData(time, title)
this.bookmarks.push(newBookmark)
return newBookmark
}
updateBookmark(time, title) {
var bookmark = this.bookmarks.find(bm => bm.time === time)
if (!bookmark) return false
bookmark.title = title
return bookmark
}
deleteBookmark(time) {
this.bookmarks = this.bookmarks.filter(bm => bm.time !== time)
}
}
module.exports = UserAudiobookData

View file

@ -0,0 +1,98 @@
const Logger = require('../../Logger')
const date = require('date-and-time')
const { getId } = require('../../utils/index')
class UserListeningSession {
constructor(session) {
this.id = null
this.sessionType = 'listeningSession'
this.userId = null
this.audiobookId = null
this.audiobookTitle = null
this.audiobookAuthor = null
this.audiobookDuration = 0
this.audiobookGenres = []
this.date = null
this.dayOfWeek = null
this.timeListening = null
this.lastUpdate = null
this.startedAt = null
if (session) {
this.construct(session)
}
}
toJSON() {
return {
id: this.id,
sessionType: this.sessionType,
userId: this.userId,
audiobookId: this.audiobookId,
audiobookTitle: this.audiobookTitle,
audiobookAuthor: this.audiobookAuthor,
audiobookDuration: this.audiobookDuration,
audiobookGenres: [...this.audiobookGenres],
date: this.date,
dayOfWeek: this.dayOfWeek,
timeListening: this.timeListening,
lastUpdate: this.lastUpdate,
startedAt: this.startedAt
}
}
construct(session) {
this.id = session.id
this.sessionType = session.sessionType
this.userId = session.userId
this.audiobookId = session.audiobookId
this.audiobookTitle = session.audiobookTitle
this.audiobookAuthor = session.audiobookAuthor
this.audiobookDuration = session.audiobookDuration || 0
this.audiobookGenres = session.audiobookGenres
this.date = session.date
this.dayOfWeek = session.dayOfWeek
this.timeListening = session.timeListening || null
this.lastUpdate = session.lastUpdate || null
this.startedAt = session.startedAt
}
setData(libraryItem, user) {
this.id = getId('ls')
this.userId = user.id
this.audiobookId = libraryItem.id
// TODO: For podcasts this needs to be generic
this.audiobookTitle = libraryItem.media.metadata.title || ''
this.audiobookAuthor = libraryItem.media.metadata.authorName || ''
this.audiobookDuration = libraryItem.media.duration || 0
this.audiobookGenres = [...libraryItem.media.metadata.genres]
this.timeListening = 0
this.lastUpdate = Date.now()
this.startedAt = 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.lastUpdate = 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 = UserListeningSession