2022-03-16 00:57:15 +01:00
|
|
|
const Logger = require('../../Logger')
|
|
|
|
const AudioBookmark = require('./AudioBookmark')
|
2022-03-26 17:59:34 +01:00
|
|
|
const MediaProgress = require('./MediaProgress')
|
2021-09-06 21:13:01 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
class User {
|
|
|
|
constructor(user) {
|
|
|
|
this.id = null
|
2023-07-05 01:14:44 +02:00
|
|
|
this.oldUserId = null // TODO: Temp for keeping old access tokens
|
2021-08-18 00:01:11 +02:00
|
|
|
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
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
this.mediaProgress = []
|
2022-09-29 00:12:27 +02:00
|
|
|
this.seriesHideFromContinueListening = [] // Series IDs that should not show on home page continue listening
|
2022-03-16 00:57:15 +01:00
|
|
|
this.bookmarks = []
|
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)
|
2023-04-27 23:02:15 +02:00
|
|
|
this.itemTagsSelected = [] // 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'
|
|
|
|
}
|
2022-04-29 23:42:40 +02:00
|
|
|
get isAdmin() {
|
|
|
|
return this.type === 'admin'
|
|
|
|
}
|
2022-04-30 01:38:13 +02:00
|
|
|
get isGuest() {
|
|
|
|
return this.type === 'guest'
|
|
|
|
}
|
2022-04-30 01:29:40 +02:00
|
|
|
get isAdminOrUp() {
|
|
|
|
return this.isAdmin || this.isRoot
|
|
|
|
}
|
2021-09-07 00:42:15 +02:00
|
|
|
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
|
|
|
|
}
|
2022-05-28 23:53:03 +02:00
|
|
|
get canAccessExplicitContent() {
|
|
|
|
return !!this.permissions.accessExplicitContent && this.isActive
|
2022-03-20 12:29:08 +01:00
|
|
|
}
|
2021-10-16 14:49:12 +02:00
|
|
|
get hasPw() {
|
|
|
|
return !!this.pash && !!this.pash.length
|
|
|
|
}
|
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,
|
2022-05-28 23:53:03 +02:00
|
|
|
accessAllTags: true,
|
|
|
|
accessExplicitContent: true
|
2021-09-07 00:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
oldUserId: this.oldUserId,
|
2021-08-18 00:01:11 +02:00
|
|
|
username: this.username,
|
|
|
|
pash: this.pash,
|
|
|
|
type: this.type,
|
|
|
|
token: this.token,
|
2022-03-26 17:59:34 +01:00
|
|
|
mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [],
|
2022-09-29 00:12:27 +02:00
|
|
|
seriesHideFromContinueListening: [...this.seriesHideFromContinueListening],
|
2022-03-16 00:57:15 +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-10-23 03:08:02 +02:00
|
|
|
permissions: this.permissions,
|
2022-03-20 12:29:08 +01:00
|
|
|
librariesAccessible: [...this.librariesAccessible],
|
2023-04-27 23:02:15 +02:00
|
|
|
itemTagsSelected: [...this.itemTagsSelected]
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:56:50 +02:00
|
|
|
toJSONForBrowser(hideRootToken = false, minimal = false) {
|
|
|
|
const json = {
|
2021-08-18 00:01:11 +02:00
|
|
|
id: this.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
oldUserId: this.oldUserId,
|
2021-08-18 00:01:11 +02:00
|
|
|
username: this.username,
|
|
|
|
type: this.type,
|
2023-03-29 21:56:50 +02:00
|
|
|
token: (this.type === 'root' && hideRootToken) ? '' : this.token,
|
2022-03-26 17:59:34 +01:00
|
|
|
mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [],
|
2022-09-29 00:12:27 +02:00
|
|
|
seriesHideFromContinueListening: [...this.seriesHideFromContinueListening],
|
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-10-23 03:08:02 +02:00
|
|
|
permissions: this.permissions,
|
2022-03-20 12:29:08 +01:00
|
|
|
librariesAccessible: [...this.librariesAccessible],
|
2023-04-27 23:02:15 +02:00
|
|
|
itemTagsSelected: [...this.itemTagsSelected]
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2023-03-29 21:56:50 +02:00
|
|
|
if (minimal) {
|
|
|
|
delete json.mediaProgress
|
|
|
|
delete json.bookmarks
|
|
|
|
}
|
|
|
|
return json
|
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) {
|
2022-03-26 17:59:34 +01:00
|
|
|
var userSession = sessions ? sessions.find(s => s.userId === this.id) : null
|
|
|
|
var session = null
|
2022-11-13 15:25:51 +01:00
|
|
|
if (userSession) {
|
|
|
|
var libraryItem = libraryItems.find(li => li.id === userSession.libraryItemId)
|
2022-03-26 17:59:34 +01:00
|
|
|
if (libraryItem) {
|
|
|
|
session = userSession.toJSONForClient(libraryItem)
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 03:07:42 +02:00
|
|
|
return {
|
|
|
|
id: this.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
oldUserId: this.oldUserId,
|
2021-10-13 03:07:42 +02:00
|
|
|
username: this.username,
|
|
|
|
type: this.type,
|
2022-03-26 17:59:34 +01:00
|
|
|
session,
|
2022-03-18 01:10:47 +01:00
|
|
|
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
|
2023-07-05 01:14:44 +02:00
|
|
|
this.oldUserId = user.oldUserId
|
2021-08-18 00:01:11 +02:00
|
|
|
this.username = user.username
|
|
|
|
this.pash = user.pash
|
|
|
|
this.type = user.type
|
|
|
|
this.token = user.token
|
2022-03-16 00:57:15 +01:00
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
this.mediaProgress = []
|
|
|
|
if (user.mediaProgress) {
|
|
|
|
this.mediaProgress = user.mediaProgress.map(li => new MediaProgress(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
|
|
|
|
2022-09-29 00:12:27 +02:00
|
|
|
this.seriesHideFromContinueListening = []
|
|
|
|
if (user.seriesHideFromContinueListening) this.seriesHideFromContinueListening = [...user.seriesHideFromContinueListening]
|
|
|
|
|
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-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
|
2022-05-28 23:53:03 +02:00
|
|
|
// Explicit content restriction permission added v2.0.18
|
|
|
|
if (this.permissions.accessExplicitContent === undefined) this.permissions.accessExplicitContent = true
|
2023-04-28 00:18:54 +02:00
|
|
|
// itemTagsAccessible was renamed to itemTagsSelected in version v2.2.20
|
|
|
|
if (user.itemTagsAccessible?.length) {
|
2023-04-27 23:02:15 +02:00
|
|
|
this.permissions.selectedTagsNotAccessible = false
|
2023-04-28 00:18:54 +02:00
|
|
|
user.itemTagsSelected = user.itemTagsAccessible
|
2023-04-27 23:02:15 +02:00
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2022-03-20 12:29:08 +01:00
|
|
|
this.librariesAccessible = [...(user.librariesAccessible || [])]
|
2023-04-27 23:02:15 +02:00
|
|
|
this.itemTagsSelected = [...(user.itemTagsSelected || [])]
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-04-29 23:50:06 +02:00
|
|
|
|
2022-09-29 00:12:27 +02:00
|
|
|
if (payload.seriesHideFromContinueListening && Array.isArray(payload.seriesHideFromContinueListening)) {
|
|
|
|
if (this.seriesHideFromContinueListening.join(',') !== payload.seriesHideFromContinueListening.join(',')) {
|
|
|
|
hasUpdates = true
|
|
|
|
this.seriesHideFromContinueListening = [...payload.seriesHideFromContinueListening]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 23:50:06 +02:00
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
// Update accessible libraries
|
2022-04-29 23:50:06 +02:00
|
|
|
if (this.permissions.accessAllLibraries) {
|
|
|
|
// Access all libraries
|
|
|
|
if (this.librariesAccessible.length) {
|
|
|
|
this.librariesAccessible = []
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
} else if (payload.librariesAccessible !== undefined) {
|
2021-10-23 03:08:02 +02:00
|
|
|
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
|
|
|
|
2022-04-29 23:50:06 +02:00
|
|
|
// Update accessible tags
|
|
|
|
if (this.permissions.accessAllTags) {
|
|
|
|
// Access all tags
|
2023-04-27 23:02:15 +02:00
|
|
|
if (this.itemTagsSelected.length) {
|
|
|
|
this.itemTagsSelected = []
|
|
|
|
this.permissions.selectedTagsNotAccessible = false
|
2022-04-29 23:50:06 +02:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
2023-04-27 23:02:15 +02:00
|
|
|
} else if (payload.itemTagsSelected !== undefined) {
|
|
|
|
if (payload.itemTagsSelected.length) {
|
|
|
|
if (payload.itemTagsSelected.join(',') !== this.itemTagsSelected.join(',')) {
|
2022-03-20 12:29:08 +01:00
|
|
|
hasUpdates = true
|
2023-04-27 23:02:15 +02:00
|
|
|
this.itemTagsSelected = [...payload.itemTagsSelected]
|
2022-03-20 12:29:08 +01:00
|
|
|
}
|
2023-04-27 23:02:15 +02:00
|
|
|
} else if (this.itemTagsSelected.length > 0) {
|
2022-03-20 12:29:08 +01:00
|
|
|
hasUpdates = true
|
2023-04-27 23:02:15 +02:00
|
|
|
this.itemTagsSelected = []
|
|
|
|
this.permissions.selectedTagsNotAccessible = false
|
2022-03-20 12:29:08 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-06 01:20:29 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:45:28 +02:00
|
|
|
getDefaultLibraryId(libraries) {
|
|
|
|
// Libraries should already be in ascending display order, find first accessible
|
|
|
|
var firstAccessibleLibrary = libraries.find(lib => this.checkCanAccessLibrary(lib.id))
|
|
|
|
if (!firstAccessibleLibrary) return null
|
|
|
|
return firstAccessibleLibrary.id
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:03:16 +01:00
|
|
|
// Returns most recent media progress w/ `media` object and optionally an `episode` object
|
2022-03-18 01:10:47 +01:00
|
|
|
getMostRecentItemProgress(libraryItems) {
|
2022-03-26 17:59:34 +01:00
|
|
|
if (!this.mediaProgress.length) return null
|
2022-11-13 16:03:16 +01:00
|
|
|
var mediaProgressObjects = this.mediaProgress.map(lip => lip.toJSON())
|
|
|
|
mediaProgressObjects.sort((a, b) => b.lastUpdate - a.lastUpdate)
|
|
|
|
|
|
|
|
var libraryItemMedia = null
|
|
|
|
var progressEpisode = null
|
|
|
|
// Find the most recent progress that still has a libraryItem and episode
|
|
|
|
var mostRecentProgress = mediaProgressObjects.find((progress) => {
|
|
|
|
const libraryItem = libraryItems.find(li => li.id === progress.libraryItemId)
|
|
|
|
if (!libraryItem) {
|
|
|
|
Logger.warn('[User] Library item not found for users progress ' + progress.libraryItemId)
|
|
|
|
return false
|
|
|
|
} else if (progress.episodeId) {
|
|
|
|
const episode = libraryItem.mediaType === 'podcast' ? libraryItem.media.getEpisode(progress.episodeId) : null
|
|
|
|
if (!episode) {
|
|
|
|
Logger.warn(`[User] Episode ${progress.episodeId} not found for user media progress, podcast: ${libraryItem.media.metadata.title}`)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
libraryItemMedia = libraryItem.media.toJSONExpanded()
|
|
|
|
progressEpisode = episode.toJSON()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
libraryItemMedia = libraryItem.media.toJSONExpanded()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!mostRecentProgress) return null
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
return {
|
2022-11-13 16:03:16 +01:00
|
|
|
...mostRecentProgress,
|
|
|
|
media: libraryItemMedia,
|
|
|
|
episode: progressEpisode
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:41:26 +01:00
|
|
|
getMediaProgress(libraryItemId, episodeId = null) {
|
2022-03-26 17:59:34 +01:00
|
|
|
if (!this.mediaProgress) return null
|
2022-03-26 23:41:26 +01:00
|
|
|
return this.mediaProgress.find(lip => {
|
|
|
|
if (episodeId && lip.episodeId !== episodeId) return false
|
2022-04-09 02:19:47 +02:00
|
|
|
return lip.libraryItemId === libraryItemId
|
2022-03-26 23:41:26 +01:00
|
|
|
})
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:31:11 +02:00
|
|
|
getAllMediaProgressForLibraryItem(libraryItemId) {
|
|
|
|
if (!this.mediaProgress) return []
|
|
|
|
return this.mediaProgress.filter(li => li.libraryItemId === libraryItemId)
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:41:26 +01:00
|
|
|
createUpdateMediaProgress(libraryItem, updatePayload, episodeId = null) {
|
2023-01-28 21:46:01 +01:00
|
|
|
const itemProgress = this.mediaProgress.find(li => {
|
2022-03-26 23:41:26 +01:00
|
|
|
if (episodeId && li.episodeId !== episodeId) return false
|
2022-04-09 02:19:47 +02:00
|
|
|
return li.libraryItemId === libraryItem.id
|
2022-03-26 23:41:26 +01:00
|
|
|
})
|
2022-03-17 19:33:22 +01:00
|
|
|
if (!itemProgress) {
|
2023-01-28 21:46:01 +01:00
|
|
|
const newItemProgress = new MediaProgress()
|
2022-03-18 21:31:46 +01:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
newItemProgress.setData(libraryItem, updatePayload, episodeId, this.id)
|
2022-03-26 17:59:34 +01:00
|
|
|
this.mediaProgress.push(newItemProgress)
|
2022-03-17 19:33:22 +01:00
|
|
|
return true
|
|
|
|
}
|
2023-01-28 21:46:01 +01:00
|
|
|
const wasUpdated = itemProgress.update(updatePayload)
|
2022-04-10 00:56:51 +02:00
|
|
|
|
|
|
|
if (updatePayload.lastUpdate) itemProgress.lastUpdate = updatePayload.lastUpdate // For local to keep update times in sync
|
2022-03-17 19:33:22 +01:00
|
|
|
return wasUpdated
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:12:27 +02:00
|
|
|
removeMediaProgress(id) {
|
|
|
|
if (!this.mediaProgress.some(mp => mp.id === id)) return false
|
|
|
|
this.mediaProgress = this.mediaProgress.filter(mp => mp.id !== id)
|
|
|
|
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
|
|
|
|
2022-03-20 12:29:08 +01:00
|
|
|
checkCanAccessLibraryItemWithTags(tags) {
|
2022-04-22 02:29:15 +02:00
|
|
|
if (this.permissions.accessAllTags) return true
|
2023-04-27 23:02:15 +02:00
|
|
|
if (this.permissions.selectedTagsNotAccessible) {
|
2023-04-28 00:18:54 +02:00
|
|
|
if (!tags?.length) return true
|
2023-04-27 23:02:15 +02:00
|
|
|
return tags.every(tag => !this.itemTagsSelected.includes(tag))
|
|
|
|
}
|
2023-04-28 00:18:54 +02:00
|
|
|
if (!tags?.length) return false
|
2023-04-27 23:02:15 +02:00
|
|
|
return this.itemTagsSelected.some(tag => tags.includes(tag))
|
2022-03-20 12:29:08 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 01:48:57 +02:00
|
|
|
checkCanAccessLibraryItem(libraryItem) {
|
|
|
|
if (!this.checkCanAccessLibrary(libraryItem.libraryId)) return false
|
2022-05-31 02:26:53 +02:00
|
|
|
|
2022-05-28 23:53:03 +02:00
|
|
|
if (libraryItem.media.metadata.explicit && !this.canAccessExplicitContent) return false
|
2022-05-09 01:48:57 +02:00
|
|
|
return this.checkCanAccessLibraryItemWithTags(libraryItem.media.tags)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-09-29 00:12:27 +02:00
|
|
|
|
|
|
|
checkShouldHideSeriesFromContinueListening(seriesId) {
|
|
|
|
return this.seriesHideFromContinueListening.includes(seriesId)
|
|
|
|
}
|
|
|
|
|
|
|
|
addSeriesToHideFromContinueListening(seriesId) {
|
|
|
|
if (this.seriesHideFromContinueListening.includes(seriesId)) return false
|
|
|
|
this.seriesHideFromContinueListening.push(seriesId)
|
|
|
|
return true
|
|
|
|
}
|
2022-09-29 00:45:39 +02:00
|
|
|
|
2022-11-16 00:20:57 +01:00
|
|
|
removeSeriesFromHideFromContinueListening(seriesId) {
|
|
|
|
if (!this.seriesHideFromContinueListening.includes(seriesId)) return false
|
|
|
|
this.seriesHideFromContinueListening = this.seriesHideFromContinueListening.filter(sid => sid !== seriesId)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:45:39 +02:00
|
|
|
removeProgressFromContinueListening(progressId) {
|
|
|
|
const progress = this.mediaProgress.find(mp => mp.id === progressId)
|
|
|
|
if (!progress) return false
|
|
|
|
return progress.removeFromContinueListening()
|
|
|
|
}
|
2023-07-15 21:45:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of podcast episodes not finished for library item
|
|
|
|
* Note: libraryItem passed in from libraryHelpers is not a LibraryItem class instance
|
|
|
|
* @param {LibraryItem|object} libraryItem
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
getNumEpisodesIncompleteForPodcast(libraryItem) {
|
|
|
|
if (!libraryItem?.media.episodes) return 0
|
|
|
|
let numEpisodesIncomplete = 0
|
|
|
|
for (const episode of libraryItem.media.episodes) {
|
|
|
|
const mediaProgress = this.getMediaProgress(libraryItem.id, episode.id)
|
|
|
|
if (!mediaProgress?.isFinished) {
|
|
|
|
numEpisodesIncomplete++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numEpisodesIncomplete
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = User
|