audiobookshelf/server/objects/user/User.js

364 lines
12 KiB
JavaScript
Raw Normal View History

const Logger = require('../../Logger')
const AudioBookmark = require('./AudioBookmark')
const MediaProgress = require('./MediaProgress')
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.token = null
2021-08-27 14:01:47 +02:00
this.isActive = true
this.isLocked = false
this.lastSeen = null
2021-08-18 00:01:11 +02:00
this.createdAt = null
this.mediaProgress = []
this.bookmarks = []
this.settings = {}
this.permissions = {}
this.librariesAccessible = [] // Library IDs (Empty if ALL libraries)
2022-03-20 12:29:08 +01:00
this.itemTagsAccessible = [] // Empty if ALL item tags accessible
2021-08-18 00:01:11 +02:00
if (user) {
this.construct(user)
}
}
get isRoot() {
return this.type === 'root'
}
get canDelete() {
return !!this.permissions.delete && this.isActive
}
get canUpdate() {
return !!this.permissions.update && this.isActive
}
get canDownload() {
return !!this.permissions.download && this.isActive
}
get canUpload() {
return !!this.permissions.upload && this.isActive
}
get canAccessAllLibraries() {
return !!this.permissions.accessAllLibraries && this.isActive
}
2022-03-20 12:29:08 +01:00
get canAccessAllTags() {
return !!this.permissions.accessAllTags && this.isActive
}
get hasPw() {
return !!this.pash && !!this.pash.length
}
getDefaultUserSettings() {
return {
mobileOrderBy: 'recent',
mobileOrderDesc: true,
mobileFilterBy: 'all',
orderBy: 'book.title',
orderDesc: false,
filterBy: 'all',
playbackRate: 1,
bookshelfCoverSize: 120,
collapseSeries: false
}
}
getDefaultUserPermissions() {
return {
download: true,
update: true,
delete: this.type === 'root',
upload: this.type === 'root' || this.type === 'admin',
2022-03-20 12:29:08 +01:00
accessAllLibraries: true,
accessAllTags: true
}
}
2021-08-18 00:01:11 +02:00
toJSON() {
return {
id: this.id,
username: this.username,
pash: this.pash,
type: this.type,
token: this.token,
mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [],
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
2021-08-27 14:01:47 +02:00
isActive: this.isActive,
isLocked: this.isLocked,
lastSeen: this.lastSeen,
createdAt: this.createdAt,
settings: this.settings,
permissions: this.permissions,
2022-03-20 12:29:08 +01:00
librariesAccessible: [...this.librariesAccessible],
itemTagsAccessible: [...this.itemTagsAccessible]
2021-08-18 00:01:11 +02:00
}
}
toJSONForBrowser() {
return {
id: this.id,
username: this.username,
type: this.type,
token: this.token,
mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [],
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
2021-08-27 14:01:47 +02:00
isActive: this.isActive,
isLocked: this.isLocked,
lastSeen: this.lastSeen,
createdAt: this.createdAt,
settings: this.settings,
permissions: this.permissions,
2022-03-20 12:29:08 +01:00
librariesAccessible: [...this.librariesAccessible],
itemTagsAccessible: [...this.itemTagsAccessible]
2021-08-18 00:01:11 +02:00
}
}
// Data broadcasted
toJSONForPublic(sessions, libraryItems) {
var userSession = sessions ? sessions.find(s => s.userId === this.id) : null
var session = null
if (session) {
var libraryItem = libraryItems.find(li => li.id === session.libraryItemId)
if (libraryItem) {
session = userSession.toJSONForClient(libraryItem)
}
}
return {
id: this.id,
username: this.username,
type: this.type,
session,
mostRecent: this.getMostRecentItemProgress(libraryItems),
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
this.token = user.token
this.mediaProgress = []
if (user.mediaProgress) {
this.mediaProgress = user.mediaProgress.map(li => new MediaProgress(li)).filter(lip => lip.id)
}
this.bookmarks = []
if (user.bookmarks) {
this.bookmarks = user.bookmarks.filter(bm => typeof bm.libraryItemId == 'string').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
2021-08-27 14:01:47 +02:00
this.createdAt = user.createdAt || Date.now()
this.settings = user.settings || this.getDefaultUserSettings()
this.permissions = user.permissions || this.getDefaultUserPermissions()
// Upload permission added v1.1.13, make sure root user has upload permissions
if (this.type === 'root' && !this.permissions.upload) this.permissions.upload = true
// Library restriction permissions added v1.4.14, defaults to all libraries
if (this.permissions.accessAllLibraries === undefined) this.permissions.accessAllLibraries = true
2022-03-20 12:29:08 +01:00
// Library restriction permissions added v2.0, defaults to all libraries
if (this.permissions.accessAllTags === undefined) this.permissions.accessAllTags = true
2022-03-20 12:29:08 +01:00
this.librariesAccessible = [...(user.librariesAccessible || [])]
this.itemTagsAccessible = [...(user.itemTagsAccessible || [])]
2021-08-18 00:01:11 +02:00
}
update(payload) {
var hasUpdates = false
// Update the following keys:
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]
}
}
}
})
// 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]
}
}
}
// 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 = []
}
}
2022-03-20 12:29:08 +01:00
// Update accessible libraries
if (payload.itemTagsAccessible !== undefined) {
if (payload.itemTagsAccessible.length) {
if (payload.itemTagsAccessible.join(',') !== this.itemTagsAccessible.join(',')) {
hasUpdates = true
this.itemTagsAccessible = [...payload.itemTagsAccessible]
}
} else if (this.itemTagsAccessible.length > 0) {
hasUpdates = true
this.itemTagsAccessible = []
}
}
return hasUpdates
}
getMostRecentItemProgress(libraryItems) {
if (!this.mediaProgress.length) return null
var lip = this.mediaProgress.map(lip => lip.toJSON())
lip.sort((a, b) => b.lastUpdate - a.lastUpdate)
var mostRecentWithLip = lip.find(li => libraryItems.find(_li => _li.id === li.id))
if (!mostRecentWithLip) return null
var libraryItem = libraryItems.find(li => li.id === mostRecentWithLip.id)
return {
...mostRecentWithLip,
media: libraryItem.media.toJSONExpanded()
}
}
getMediaProgress(libraryItemId, episodeId = null) {
if (!this.mediaProgress) return null
return this.mediaProgress.find(lip => {
if (episodeId && lip.episodeId !== episodeId) return false
return lip.id === libraryItemId
})
}
createUpdateMediaProgress(libraryItem, updatePayload, episodeId = null) {
var itemProgress = this.mediaProgress.find(li => {
if (episodeId && li.episodeId !== episodeId) return false
return li.id === libraryItem.id
})
if (!itemProgress) {
var newItemProgress = new MediaProgress()
newItemProgress.setData(libraryItem.id, updatePayload, episodeId)
this.mediaProgress.push(newItemProgress)
return true
}
var wasUpdated = itemProgress.update(updatePayload)
return wasUpdated
}
removeMediaProgress(libraryItemId) {
if (!this.mediaProgress.some(lip => lip.id == libraryItemId)) return false
this.mediaProgress = this.mediaProgress.filter(lip => lip.id != libraryItemId)
return true
2021-08-18 00:01:11 +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
}
checkCanAccessLibrary(libraryId) {
if (this.permissions.accessAllLibraries) return true
if (!this.librariesAccessible) return false
return this.librariesAccessible.includes(libraryId)
}
2022-03-20 12:29:08 +01:00
checkCanAccessLibraryItemWithTags(tags) {
if (this.permissions.accessAllTags || !tags || !tags.length) return true
return this.itemTagsAccessible.some(tag => tags.includes(tag))
}
findBookmark(libraryItemId, time) {
return this.bookmarks.find(bm => bm.libraryItemId === libraryItemId && bm.time == time)
}
createBookmark(libraryItemId, time, title) {
var existingBookmark = this.findBookmark(libraryItemId, time)
if (existingBookmark) {
Logger.warn('[User] Create Bookmark already exists for this time')
existingBookmark.title = title
return existingBookmark
}
var newBookmark = new AudioBookmark()
newBookmark.setData(libraryItemId, time, title)
this.bookmarks.push(newBookmark)
return newBookmark
}
updateBookmark(libraryItemId, time, title) {
var bookmark = this.findBookmark(libraryItemId, time)
if (!bookmark) {
Logger.error(`[User] updateBookmark not found`)
return null
}
bookmark.title = title
return bookmark
}
removeBookmark(libraryItemId, time) {
this.bookmarks = this.bookmarks.filter(bm => (bm.libraryItemId !== libraryItemId || bm.time !== time))
}
// TODO: re-do mobile sync
syncLocalUserAudiobookData(localUserAudiobookData, audiobook) {
// if (!localUserAudiobookData || !localUserAudiobookData.audiobookId) {
// Logger.error(`[User] Invalid local user audiobook data`, localUserAudiobookData)
// return false
// }
// if (!this.audiobooks) this.audiobooks = {}
// 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