2022-03-16 00:57:15 +01:00
|
|
|
const Logger = require('../../Logger')
|
|
|
|
const AudioBookmark = require('../user/AudioBookmark')
|
2021-10-25 01:25:44 +02:00
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
class UserAudiobookData {
|
2021-09-06 21:13:01 +02:00
|
|
|
constructor(progress) {
|
|
|
|
this.audiobookId = null
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
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
|
2021-10-25 01:25:44 +02:00
|
|
|
this.bookmarks = []
|
2021-09-06 21:13:01 +02:00
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
this.construct(progress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 01:25:44 +02:00
|
|
|
bookmarksToJSON() {
|
|
|
|
if (!this.bookmarks) return []
|
2021-10-28 13:35:22 +02:00
|
|
|
return this.bookmarks.filter((b) => {
|
|
|
|
if (!b.toJSON) {
|
2021-10-29 00:19:09 +02:00
|
|
|
Logger.error(`[UserAudiobookData] Invalid bookmark ${JSON.stringify(b)}`)
|
2021-10-28 13:35:22 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).map(b => b.toJSON())
|
2021-10-25 01:25:44 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
audiobookId: this.audiobookId,
|
|
|
|
totalDuration: this.totalDuration,
|
|
|
|
progress: this.progress,
|
|
|
|
currentTime: this.currentTime,
|
|
|
|
isRead: this.isRead,
|
|
|
|
lastUpdate: this.lastUpdate,
|
|
|
|
startedAt: this.startedAt,
|
2021-10-25 01:25:44 +02:00
|
|
|
finishedAt: this.finishedAt,
|
|
|
|
bookmarks: this.bookmarksToJSON()
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-10-25 01:25:44 +02:00
|
|
|
if (progress.bookmarks) {
|
|
|
|
this.bookmarks = progress.bookmarks.map(b => new AudioBookmark(b))
|
|
|
|
} else {
|
|
|
|
this.bookmarks = []
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
updateProgressFromStream(stream) {
|
2022-03-13 02:59:35 +01:00
|
|
|
this.audiobookId = stream.libraryItemId
|
2021-09-06 21:13:01 +02:00
|
|
|
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) {
|
2021-10-24 21:02:49 +02:00
|
|
|
this.isRead = true
|
|
|
|
this.progress = 1
|
|
|
|
this.finishedAt = Date.now()
|
2021-09-06 21:13:01 +02:00
|
|
|
} else {
|
|
|
|
this.isRead = false
|
|
|
|
this.finishedAt = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in payload) {
|
2022-01-06 02:07:32 +01:00
|
|
|
if (this[key] !== undefined && payload[key] !== this[key]) {
|
2021-09-06 21:13:01 +02:00
|
|
|
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()
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
2021-10-25 01:25:44 +02:00
|
|
|
|
2021-10-27 03:09:04 +02:00
|
|
|
checkBookmarkExists(time) {
|
|
|
|
return this.bookmarks.find(bm => bm.time === time)
|
|
|
|
}
|
|
|
|
|
2021-10-25 01:25:44 +02:00
|
|
|
createBookmark(time, title) {
|
|
|
|
var newBookmark = new AudioBookmark()
|
|
|
|
newBookmark.setData(time, title)
|
|
|
|
this.bookmarks.push(newBookmark)
|
|
|
|
return newBookmark
|
|
|
|
}
|
2021-10-27 03:09:04 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
2021-10-29 00:19:09 +02:00
|
|
|
module.exports = UserAudiobookData
|