2022-03-16 00:57:15 +01:00
|
|
|
const Logger = require('../../Logger')
|
|
|
|
const AudioBookmark = require('./AudioBookmark')
|
|
|
|
const LibraryItemProgress = require('./LibraryItemProgress')
|
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.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
|
2022-03-16 00:57:15 +01:00
|
|
|
|
|
|
|
this.libraryItemProgress = []
|
|
|
|
this.bookmarks = []
|
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)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-03-20 12:29:08 +01:00
|
|
|
get canAccessAllTags() {
|
|
|
|
return !!this.permissions.accessAllTags && 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',
|
2022-03-20 12:29:08 +01:00
|
|
|
accessAllLibraries: true,
|
|
|
|
accessAllTags: true
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2022-03-16 00:57:15 +01:00
|
|
|
libraryItemProgress: this.libraryItemProgress ? this.libraryItemProgress.map(li => li.toJSON()) : [],
|
|
|
|
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
|
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,
|
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,
|
2022-03-16 00:57:15 +01:00
|
|
|
libraryItemProgress: this.libraryItemProgress ? this.libraryItemProgress.map(li => li.toJSON()) : [],
|
2022-03-18 02:28:04 +01:00
|
|
|
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
|
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,
|
2022-03-20 12:29:08 +01:00
|
|
|
librariesAccessible: [...this.librariesAccessible],
|
|
|
|
itemTagsAccessible: [...this.itemTagsAccessible]
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
// Data broadcasted
|
2022-03-18 01:10:47 +01:00
|
|
|
toJSONForPublic(sessions, libraryItems) {
|
|
|
|
var session = sessions ? sessions.find(s => s.userId === this.id) : null
|
2021-10-13 03:07:42 +02:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
username: this.username,
|
|
|
|
type: this.type,
|
2022-03-18 01:10:47 +01:00
|
|
|
session: session ? session.toJSONForClient() : null,
|
|
|
|
mostRecent: this.getMostRecentItemProgress(libraryItems),
|
2021-10-13 03:07:42 +02:00
|
|
|
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
|
2022-03-16 00:57:15 +01:00
|
|
|
|
|
|
|
this.libraryItemProgress = []
|
|
|
|
if (user.libraryItemProgress) {
|
2022-03-18 01:10:47 +01:00
|
|
|
this.libraryItemProgress = user.libraryItemProgress.map(li => new LibraryItemProgress(li)).filter(lip => lip.id)
|
2022-03-16 00:57:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.bookmarks = []
|
|
|
|
if (user.bookmarks) {
|
2022-03-18 02:28:04 +01:00
|
|
|
this.bookmarks = user.bookmarks.filter(bm => typeof bm.libraryItemId == 'string').map(bm => new AudioBookmark(bm))
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
2022-03-16 00:57:15 +01: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
|
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
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2022-03-20 12:29:08 +01:00
|
|
|
this.librariesAccessible = [...(user.librariesAccessible || [])]
|
|
|
|
this.itemTagsAccessible = [...(user.itemTagsAccessible || [])]
|
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 = []
|
|
|
|
}
|
|
|
|
}
|
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 = []
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 01:20:29 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
getMostRecentItemProgress(libraryItems) {
|
|
|
|
if (!this.libraryItemProgress.length) return null
|
|
|
|
var lip = this.libraryItemProgress.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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLibraryItemProgress(libraryItemId) {
|
|
|
|
if (!this.libraryItemProgress) return null
|
|
|
|
return this.libraryItemProgress.find(lip => lip.id === libraryItemId)
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 21:31:46 +01:00
|
|
|
createUpdateLibraryItemProgress(libraryItem, updatePayload) {
|
|
|
|
var itemProgress = this.libraryItemProgress.find(li => li.id === libraryItem.id)
|
2022-03-17 19:33:22 +01:00
|
|
|
if (!itemProgress) {
|
|
|
|
var newItemProgress = new LibraryItemProgress()
|
2022-03-18 21:31:46 +01:00
|
|
|
|
|
|
|
var mediaEntity = null
|
|
|
|
if (updatePayload.mediaEntityId) mediaEntity = libraryItem.media.getMediaEntityById(updatePayload.mediaEntityId)
|
|
|
|
if (!mediaEntity) mediaEntity = libraryItem.media.getPlaybackMediaEntity()
|
|
|
|
if (!mediaEntity) {
|
|
|
|
Logger.error(`[User] createUpdateLibraryItemProgress invalid library item has no playback media entity "${libraryItem.id}"`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
newItemProgress.setData(libraryItem.id, mediaEntity.id, updatePayload)
|
2022-03-17 19:33:22 +01:00
|
|
|
this.libraryItemProgress.push(newItemProgress)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var wasUpdated = itemProgress.update(updatePayload)
|
|
|
|
return wasUpdated
|
|
|
|
}
|
|
|
|
|
|
|
|
removeLibraryItemProgress(libraryItemId) {
|
|
|
|
if (!this.libraryItemProgress.some(lip => lip.id == libraryItemId)) return false
|
2022-03-18 21:31:46 +01:00
|
|
|
this.libraryItemProgress = this.libraryItemProgress.filter(lip => lip.id != libraryItemId)
|
2022-03-17 19:33:22 +01:00
|
|
|
return true
|
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
|
|
|
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
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
findBookmark(libraryItemId, time) {
|
|
|
|
return this.bookmarks.find(bm => bm.libraryItemId === libraryItemId && bm.time == time)
|
2021-10-25 01:25:44 +02:00
|
|
|
}
|
2021-10-27 03:09:04 +02:00
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
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
|
2021-10-27 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
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
|
|
|
|
}
|
2021-10-27 03:09:04 +02:00
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
removeBookmark(libraryItemId, time) {
|
|
|
|
this.bookmarks = this.bookmarks.filter(bm => (bm.libraryItemId !== libraryItemId || bm.time !== time))
|
2021-10-27 03:09:04 +02:00
|
|
|
}
|
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
|