2021-09-06 21:13:01 +02:00
|
|
|
const AudiobookProgress = require('./AudiobookProgress')
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
class User {
|
|
|
|
constructor(user) {
|
|
|
|
this.id = null
|
|
|
|
this.username = null
|
|
|
|
this.pash = null
|
|
|
|
this.type = null
|
|
|
|
this.stream = null
|
|
|
|
this.token = null
|
2021-08-27 14:01:47 +02:00
|
|
|
this.isActive = true
|
2021-08-18 00:01:11 +02:00
|
|
|
this.createdAt = null
|
|
|
|
this.audiobooks = null
|
2021-08-24 01:31:04 +02:00
|
|
|
this.settings = {}
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
this.construct(user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
getDefaultUserSettings() {
|
|
|
|
return {
|
|
|
|
orderBy: 'book.title',
|
|
|
|
orderDesc: false,
|
|
|
|
filterBy: 'all',
|
2021-08-26 16:47:51 +02:00
|
|
|
playbackRate: 1,
|
|
|
|
bookshelfCoverSize: 120
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
username: this.username,
|
|
|
|
pash: this.pash,
|
|
|
|
type: this.type,
|
|
|
|
stream: this.stream,
|
|
|
|
token: this.token,
|
2021-09-06 21:13:01 +02:00
|
|
|
audiobooks: this.audiobooksToJSON(),
|
2021-08-27 14:01:47 +02:00
|
|
|
isActive: this.isActive,
|
2021-08-24 01:31:04 +02:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
settings: this.settings
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSONForBrowser() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
username: this.username,
|
|
|
|
type: this.type,
|
|
|
|
stream: this.stream,
|
|
|
|
token: this.token,
|
2021-09-06 21:13:01 +02:00
|
|
|
audiobooks: this.audiobooksToJSON(),
|
2021-08-27 14:01:47 +02:00
|
|
|
isActive: this.isActive,
|
2021-08-24 01:31:04 +02:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
settings: this.settings
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(user) {
|
|
|
|
this.id = user.id
|
|
|
|
this.username = user.username
|
|
|
|
this.pash = user.pash
|
|
|
|
this.type = user.type
|
2021-08-27 14:01:47 +02:00
|
|
|
this.stream = user.stream || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.token = user.token
|
2021-09-06 21:13:01 +02:00
|
|
|
if (user.audiobooks) {
|
|
|
|
this.audiobooks = {}
|
|
|
|
for (const key in user.audiobooks) {
|
|
|
|
if (user.audiobooks[key]) {
|
|
|
|
this.audiobooks[key] = new AudiobookProgress(user.audiobooks[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 14:01:47 +02:00
|
|
|
this.isActive = (user.isActive === undefined || user.id === 'root') ? true : !!user.isActive
|
|
|
|
this.createdAt = user.createdAt || Date.now()
|
2021-08-24 01:31:04 +02:00
|
|
|
this.settings = user.settings || this.getDefaultUserSettings()
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 01:20:29 +02:00
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
const keysToCheck = ['pash', 'type', 'username', 'isActive']
|
|
|
|
keysToCheck.forEach((key) => {
|
|
|
|
if (payload[key] !== undefined) {
|
|
|
|
if (key === 'isActive' || payload[key]) { // pash, type, username must evaluate to true (cannot be null or empty)
|
|
|
|
if (payload[key] !== this[key]) {
|
|
|
|
hasUpdates = true
|
|
|
|
this[key] = payload[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
updateAudiobookProgressFromStream(stream) {
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!this.audiobooks) this.audiobooks = {}
|
|
|
|
if (!this.audiobooks[stream.audiobookId]) {
|
2021-09-06 21:13:01 +02:00
|
|
|
this.audiobooks[stream.audiobookId] = new AudiobookProgress()
|
|
|
|
}
|
|
|
|
this.audiobooks[stream.audiobookId].updateFromStream(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAudiobookProgress(audiobookId, updatePayload) {
|
|
|
|
if (!this.audiobooks) this.audiobooks = {}
|
|
|
|
if (!this.audiobooks[audiobookId]) {
|
|
|
|
this.audiobooks[audiobookId] = new AudiobookProgress()
|
|
|
|
this.audiobooks[audiobookId].audiobookId = audiobookId
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
return this.audiobooks[audiobookId].update(updatePayload)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
// Returns Boolean If update was made
|
|
|
|
updateSettings(settings) {
|
|
|
|
if (!this.settings) {
|
|
|
|
this.settings = { ...settings }
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var madeUpdates = false
|
|
|
|
|
|
|
|
for (const key in this.settings) {
|
|
|
|
if (settings[key] !== undefined && this.settings[key] !== settings[key]) {
|
|
|
|
this.settings[key] = settings[key]
|
|
|
|
madeUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if new settings update has keys not currently in user settings
|
|
|
|
for (const key in settings) {
|
|
|
|
if (settings[key] !== undefined && this.settings[key] === undefined) {
|
|
|
|
this.settings[key] = settings[key]
|
|
|
|
madeUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return madeUpdates
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
resetAudiobookProgress(audiobookId) {
|
|
|
|
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete this.audiobooks[audiobookId]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = User
|