2021-10-26 04:14:54 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-10-29 00:19:09 +02:00
|
|
|
const UserAudiobookData = require('./UserAudiobookData')
|
2021-09-06 21:13:01 +02:00
|
|
|
|
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-09-29 17:16:38 +02:00
|
|
|
this.isLocked = false
|
2021-10-13 03:07:42 +02:00
|
|
|
this.lastSeen = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.createdAt = null
|
|
|
|
this.audiobooks = null
|
2021-09-07 00:42:15 +02:00
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
this.settings = {}
|
2021-09-07 00:42:15 +02:00
|
|
|
this.permissions = {}
|
2021-10-23 03:08:02 +02:00
|
|
|
this.librariesAccessible = [] // Library IDs (Empty if ALL libraries)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
this.construct(user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:42:15 +02:00
|
|
|
get isRoot() {
|
|
|
|
return this.type === 'root'
|
|
|
|
}
|
|
|
|
get canDelete() {
|
2021-09-11 02:55:02 +02:00
|
|
|
return !!this.permissions.delete && this.isActive
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
|
|
|
get canUpdate() {
|
2021-09-11 02:55:02 +02:00
|
|
|
return !!this.permissions.update && this.isActive
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
|
|
|
get canDownload() {
|
2021-09-11 02:55:02 +02:00
|
|
|
return !!this.permissions.download && this.isActive
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
2021-09-18 19:45:34 +02:00
|
|
|
get canUpload() {
|
|
|
|
return !!this.permissions.upload && this.isActive
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
get canAccessAllLibraries() {
|
|
|
|
return !!this.permissions.accessAllLibraries && this.isActive
|
|
|
|
}
|
2021-10-16 14:49:12 +02:00
|
|
|
get hasPw() {
|
|
|
|
return !!this.pash && !!this.pash.length
|
|
|
|
}
|
2021-09-07 00:42:15 +02:00
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
getDefaultUserSettings() {
|
|
|
|
return {
|
2021-10-16 22:49:34 +02:00
|
|
|
mobileOrderBy: 'recent',
|
|
|
|
mobileOrderDesc: true,
|
|
|
|
mobileFilterBy: 'all',
|
2021-08-24 01:31:04 +02:00
|
|
|
orderBy: 'book.title',
|
|
|
|
orderDesc: false,
|
|
|
|
filterBy: 'all',
|
2021-08-26 16:47:51 +02:00
|
|
|
playbackRate: 1,
|
2022-01-25 01:03:54 +01:00
|
|
|
bookshelfCoverSize: 120,
|
|
|
|
collapseSeries: false
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:42:15 +02:00
|
|
|
getDefaultUserPermissions() {
|
|
|
|
return {
|
|
|
|
download: true,
|
|
|
|
update: true,
|
2021-09-18 19:45:34 +02:00
|
|
|
delete: this.type === 'root',
|
2021-10-23 03:08:02 +02:00
|
|
|
upload: this.type === 'root' || this.type === 'admin',
|
|
|
|
accessAllLibraries: true
|
2021-09-07 00:42:15 +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-09-29 17:16:38 +02:00
|
|
|
isLocked: this.isLocked,
|
2021-10-13 03:07:42 +02:00
|
|
|
lastSeen: this.lastSeen,
|
2021-08-24 01:31:04 +02:00
|
|
|
createdAt: this.createdAt,
|
2021-09-07 00:42:15 +02:00
|
|
|
settings: this.settings,
|
2021-10-23 03:08:02 +02:00
|
|
|
permissions: this.permissions,
|
|
|
|
librariesAccessible: [...this.librariesAccessible]
|
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-09-29 17:16:38 +02:00
|
|
|
isLocked: this.isLocked,
|
2021-10-13 03:07:42 +02:00
|
|
|
lastSeen: this.lastSeen,
|
2021-08-24 01:31:04 +02:00
|
|
|
createdAt: this.createdAt,
|
2021-09-07 00:42:15 +02:00
|
|
|
settings: this.settings,
|
2021-10-23 03:08:02 +02:00
|
|
|
permissions: this.permissions,
|
|
|
|
librariesAccessible: [...this.librariesAccessible]
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
// Data broadcasted
|
2021-10-13 03:07:42 +02:00
|
|
|
toJSONForPublic(streams) {
|
|
|
|
var stream = this.stream && streams ? streams.find(s => s.id === this.stream) : null
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
username: this.username,
|
|
|
|
type: this.type,
|
|
|
|
stream: stream ? stream.toJSON() : null,
|
|
|
|
lastSeen: this.lastSeen,
|
|
|
|
createdAt: this.createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-01-06 02:07:32 +01:00
|
|
|
if (key === '[object Object]') { // TEMP: Bug remove bad data
|
|
|
|
Logger.warn('[User] Construct found invalid UAD')
|
|
|
|
} else if (user.audiobooks[key]) {
|
2021-10-29 00:19:09 +02:00
|
|
|
this.audiobooks[key] = new UserAudiobookData(user.audiobooks[key])
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 17:16:38 +02:00
|
|
|
this.isActive = (user.isActive === undefined || user.type === 'root') ? true : !!user.isActive
|
|
|
|
this.isLocked = user.type === 'root' ? false : !!user.isLocked
|
2021-10-13 03:07:42 +02:00
|
|
|
this.lastSeen = user.lastSeen || null
|
2021-08-27 14:01:47 +02:00
|
|
|
this.createdAt = user.createdAt || Date.now()
|
2021-08-24 01:31:04 +02:00
|
|
|
this.settings = user.settings || this.getDefaultUserSettings()
|
2021-09-07 00:42:15 +02:00
|
|
|
this.permissions = user.permissions || this.getDefaultUserPermissions()
|
2021-09-18 19:45:34 +02:00
|
|
|
// Upload permission added v1.1.13, make sure root user has upload permissions
|
|
|
|
if (this.type === 'root' && !this.permissions.upload) this.permissions.upload = true
|
2021-10-23 03:08:02 +02:00
|
|
|
|
|
|
|
// Library restriction permissions added v1.4.14, defaults to all libraries
|
|
|
|
if (this.permissions.accessAllLibraries === undefined) this.permissions.accessAllLibraries = true
|
|
|
|
|
|
|
|
this.librariesAccessible = (user.librariesAccessible || []).map(l => l)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 01:20:29 +02:00
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
2021-09-07 00:42:15 +02:00
|
|
|
// Update the following keys:
|
2021-09-06 01:20:29 +02:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-07 00:42:15 +02:00
|
|
|
// And update permissions
|
|
|
|
if (payload.permissions) {
|
|
|
|
for (const key in payload.permissions) {
|
|
|
|
if (payload.permissions[key] !== this.permissions[key]) {
|
|
|
|
hasUpdates = true
|
|
|
|
this.permissions[key] = payload.permissions[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
// Update accessible libraries
|
|
|
|
if (payload.librariesAccessible !== undefined) {
|
|
|
|
if (payload.librariesAccessible.length) {
|
|
|
|
if (payload.librariesAccessible.join(',') !== this.librariesAccessible.join(',')) {
|
|
|
|
hasUpdates = true
|
|
|
|
this.librariesAccessible = [...payload.librariesAccessible]
|
|
|
|
}
|
|
|
|
} else if (this.librariesAccessible.length > 0) {
|
|
|
|
hasUpdates = true
|
|
|
|
this.librariesAccessible = []
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 01:20:29 +02:00
|
|
|
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-10-29 00:19:09 +02:00
|
|
|
this.audiobooks[stream.audiobookId] = new UserAudiobookData()
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
2021-10-29 00:19:09 +02:00
|
|
|
this.audiobooks[stream.audiobookId].updateProgressFromStream(stream)
|
2021-10-24 22:53:51 +02:00
|
|
|
return this.audiobooks[stream.audiobookId]
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 02:07:32 +01:00
|
|
|
updateAudiobookData(audiobookId, updatePayload) {
|
2021-09-06 21:13:01 +02:00
|
|
|
if (!this.audiobooks) this.audiobooks = {}
|
2022-01-06 02:07:32 +01:00
|
|
|
if (!this.audiobooks[audiobookId]) {
|
|
|
|
this.audiobooks[audiobookId] = new UserAudiobookData()
|
|
|
|
this.audiobooks[audiobookId].audiobookId = audiobookId
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2022-01-06 02:07:32 +01:00
|
|
|
var wasUpdated = this.audiobooks[audiobookId].update(updatePayload)
|
2021-10-26 04:14:54 +02:00
|
|
|
if (wasUpdated) {
|
2022-01-06 02:07:32 +01:00
|
|
|
// Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobookId])}`)
|
|
|
|
return this.audiobooks[audiobookId]
|
2021-10-26 04:14:54 +02:00
|
|
|
}
|
|
|
|
return false
|
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-10-23 03:08:02 +02:00
|
|
|
resetAudiobookProgress(audiobook) {
|
|
|
|
if (!this.audiobooks || !this.audiobooks[audiobook.id]) {
|
2021-09-13 01:22:52 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-01-06 02:07:32 +01:00
|
|
|
return this.updateAudiobookData(audiobook.id, {
|
2021-09-13 01:22:52 +02:00
|
|
|
progress: 0,
|
|
|
|
currentTime: 0,
|
|
|
|
isRead: false,
|
|
|
|
lastUpdate: Date.now(),
|
|
|
|
startedAt: null,
|
|
|
|
finishedAt: null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
deleteAudiobookData(audiobookId) {
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete this.audiobooks[audiobookId]
|
|
|
|
return true
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
|
|
|
|
checkCanAccessLibrary(libraryId) {
|
|
|
|
if (this.permissions.accessAllLibraries) return true
|
|
|
|
if (!this.librariesAccessible) return false
|
|
|
|
return this.librariesAccessible.includes(libraryId)
|
|
|
|
}
|
2021-10-24 22:53:51 +02:00
|
|
|
|
|
|
|
getAudiobookJSON(audiobookId) {
|
2021-12-02 02:07:03 +01:00
|
|
|
if (!this.audiobooks) return null
|
2021-10-24 22:53:51 +02:00
|
|
|
return this.audiobooks[audiobookId] ? this.audiobooks[audiobookId].toJSON() : null
|
|
|
|
}
|
2021-10-25 01:25:44 +02:00
|
|
|
|
|
|
|
createBookmark({ audiobookId, time, title }) {
|
2021-11-04 01:39:49 +01:00
|
|
|
if (!this.audiobooks) this.audiobooks = {}
|
|
|
|
if (!this.audiobooks[audiobookId]) {
|
|
|
|
this.audiobooks[audiobookId] = new UserAudiobookData()
|
|
|
|
this.audiobooks[audiobookId].audiobookId = audiobookId
|
2021-10-27 03:09:04 +02:00
|
|
|
}
|
|
|
|
if (this.audiobooks[audiobookId].checkBookmarkExists(time)) {
|
|
|
|
return {
|
|
|
|
error: 'Bookmark already exists'
|
|
|
|
}
|
2021-10-25 01:25:44 +02:00
|
|
|
}
|
2021-10-27 03:09:04 +02:00
|
|
|
|
2021-10-25 01:25:44 +02:00
|
|
|
var success = this.audiobooks[audiobookId].createBookmark(time, title)
|
|
|
|
if (success) return this.audiobooks[audiobookId]
|
|
|
|
return null
|
|
|
|
}
|
2021-10-27 03:09:04 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.audiobooks[audiobookId].deleteBookmark(time)
|
|
|
|
return this.audiobooks[audiobookId]
|
|
|
|
}
|
2021-11-20 03:08:03 +01:00
|
|
|
|
|
|
|
syncLocalUserAudiobookData(localUserAudiobookData, audiobook) {
|
|
|
|
if (!localUserAudiobookData || !localUserAudiobookData.audiobookId) {
|
|
|
|
Logger.error(`[User] Invalid local user audiobook data`, localUserAudiobookData)
|
|
|
|
return false
|
|
|
|
}
|
2022-02-04 01:12:12 +01:00
|
|
|
if (!this.audiobooks) this.audiobooks = {}
|
2021-11-20 03:08:03 +01:00
|
|
|
|
|
|
|
if (!this.audiobooks[localUserAudiobookData.audiobookId]) {
|
|
|
|
this.audiobooks[localUserAudiobookData.audiobookId] = new UserAudiobookData(localUserAudiobookData)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var userAbD = this.audiobooks[localUserAudiobookData.audiobookId]
|
|
|
|
if (userAbD.lastUpdate >= localUserAudiobookData.lastUpdate) {
|
|
|
|
// Server audiobook data is more recent
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local Data More recent
|
|
|
|
var wasUpdated = this.audiobooks[localUserAudiobookData.audiobookId].update(localUserAudiobookData)
|
|
|
|
if (wasUpdated) {
|
|
|
|
Logger.debug(`[User] syncLocalUserAudiobookData local data was more recent for "${audiobook.title}"`)
|
|
|
|
}
|
|
|
|
return wasUpdated
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = User
|