2022-08-14 01:18:42 +02:00
|
|
|
const { Constants } = require('../plugins/constants')
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
export const state = () => ({
|
|
|
|
libraries: [],
|
|
|
|
lastLoad: 0,
|
|
|
|
listeners: [],
|
2022-05-15 00:23:22 +02:00
|
|
|
currentLibraryId: null,
|
2021-10-05 05:11:42 +02:00
|
|
|
folders: [],
|
2021-12-02 02:07:03 +01:00
|
|
|
issues: 0,
|
2021-12-01 03:02:40 +01:00
|
|
|
folderLastUpdate: 0,
|
2022-10-29 01:10:19 +02:00
|
|
|
filterData: null,
|
2022-11-27 21:34:27 +01:00
|
|
|
numUserPlaylists: 0,
|
2022-11-27 00:24:46 +01:00
|
|
|
collections: [],
|
2023-05-30 00:38:38 +02:00
|
|
|
userPlaylists: [],
|
|
|
|
ereaderDevices: []
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = {
|
2024-05-26 23:01:08 +02:00
|
|
|
getCurrentLibrary: (state) => {
|
|
|
|
return state.libraries.find((lib) => lib.id === state.currentLibraryId)
|
2021-10-13 03:07:42 +02:00
|
|
|
},
|
2021-12-29 22:53:19 +01:00
|
|
|
getCurrentLibraryName: (state, getters) => {
|
|
|
|
var currentLibrary = getters.getCurrentLibrary
|
|
|
|
if (!currentLibrary) return ''
|
|
|
|
return currentLibrary.name
|
|
|
|
},
|
2022-03-07 02:02:06 +01:00
|
|
|
getCurrentLibraryMediaType: (state, getters) => {
|
|
|
|
if (!getters.getCurrentLibrary) return null
|
|
|
|
return getters.getCurrentLibrary.mediaType
|
|
|
|
},
|
2024-05-26 23:01:08 +02:00
|
|
|
getSortedLibraries: (state) => () => {
|
|
|
|
return state.libraries.map((lib) => ({ ...lib })).sort((a, b) => a.displayOrder - b.displayOrder)
|
2022-02-15 23:15:09 +01:00
|
|
|
},
|
2024-05-26 23:01:08 +02:00
|
|
|
getLibraryProvider: (state) => (libraryId) => {
|
|
|
|
var library = state.libraries.find((l) => l.id === libraryId)
|
2022-02-15 23:15:09 +01:00
|
|
|
if (!library) return null
|
|
|
|
return library.provider
|
2022-04-30 01:57:46 +02:00
|
|
|
},
|
|
|
|
getNextAccessibleLibrary: (state, getters, rootState, rootGetters) => {
|
|
|
|
var librariesSorted = getters['getSortedLibraries']()
|
|
|
|
if (!librariesSorted.length) return null
|
|
|
|
|
|
|
|
var canAccessAllLibraries = rootGetters['user/getUserCanAccessAllLibraries']
|
|
|
|
var userAccessibleLibraries = rootGetters['user/getLibrariesAccessible']
|
|
|
|
if (canAccessAllLibraries) return librariesSorted[0]
|
|
|
|
librariesSorted = librariesSorted.filter((lib) => {
|
|
|
|
return userAccessibleLibraries.includes(lib.id)
|
|
|
|
})
|
|
|
|
if (!librariesSorted.length) return null
|
|
|
|
return librariesSorted[0]
|
2022-08-13 20:56:37 +02:00
|
|
|
},
|
|
|
|
getCurrentLibrarySettings: (state, getters) => {
|
|
|
|
if (!getters.getCurrentLibrary) return null
|
|
|
|
return getters.getCurrentLibrary.settings
|
|
|
|
},
|
|
|
|
getBookCoverAspectRatio: (state, getters) => {
|
|
|
|
if (!getters.getCurrentLibrarySettings || isNaN(getters.getCurrentLibrarySettings.coverAspectRatio)) return 1
|
2022-08-14 01:18:42 +02:00
|
|
|
return getters.getCurrentLibrarySettings.coverAspectRatio === Constants.BookCoverAspectRatio.STANDARD ? 1.6 : 1
|
2022-11-12 00:44:19 +01:00
|
|
|
},
|
2023-06-10 19:46:57 +02:00
|
|
|
getLibraryIsAudiobooksOnly: (state, getters) => {
|
|
|
|
return !!getters.getCurrentLibrarySettings?.audiobooksOnly
|
|
|
|
},
|
2024-05-26 23:01:08 +02:00
|
|
|
getLibraryEpubsAllowScriptedContent: (state, getters) => {
|
|
|
|
return !!getters.getCurrentLibrarySettings?.epubsAllowScriptedContent
|
2022-11-27 00:58:52 +01:00
|
|
|
},
|
2024-05-26 23:01:08 +02:00
|
|
|
getCollection: (state) => (id) => {
|
|
|
|
return state.collections.find((c) => c.id === id)
|
|
|
|
},
|
|
|
|
getPlaylist: (state) => (id) => {
|
|
|
|
return state.userPlaylists.find((p) => p.id === id)
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2022-03-18 17:51:55 +01:00
|
|
|
requestLibraryScan({ state, commit }, { libraryId, force }) {
|
2023-02-04 00:50:42 +01:00
|
|
|
return this.$axios.$post(`/api/libraries/${libraryId}/scan?force=${force ? 1 : 0}`)
|
2022-03-18 17:51:55 +01:00
|
|
|
},
|
2021-10-05 05:11:42 +02:00
|
|
|
loadFolders({ state, commit }) {
|
|
|
|
if (state.folders.length) {
|
2023-01-07 22:31:51 +01:00
|
|
|
const lastCheck = Date.now() - state.folderLastUpdate
|
2024-05-26 23:01:08 +02:00
|
|
|
if (lastCheck < 1000 * 5) {
|
|
|
|
// 5 seconds
|
2021-10-05 05:11:42 +02:00
|
|
|
// Folders up to date
|
|
|
|
return state.folders
|
|
|
|
}
|
|
|
|
}
|
|
|
|
commit('setFoldersLastUpdate')
|
|
|
|
|
|
|
|
return this.$axios
|
|
|
|
.$get('/api/filesystem')
|
|
|
|
.then((res) => {
|
2022-12-13 00:29:56 +01:00
|
|
|
commit('setFolders', res.directories)
|
|
|
|
return res.directories
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed to load dirs', error)
|
|
|
|
commit('setFolders', [])
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
},
|
2022-04-12 02:42:09 +02:00
|
|
|
fetch({ state, dispatch, commit, rootState, rootGetters }, libraryId) {
|
2021-10-05 05:11:42 +02:00
|
|
|
if (!rootState.user || !rootState.user.user) {
|
|
|
|
console.error('libraries/fetch - User not set')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
var canUserAccessLibrary = rootGetters['user/getCanAccessLibrary'](libraryId)
|
|
|
|
if (!canUserAccessLibrary) {
|
|
|
|
console.warn('Access not allowed to library')
|
|
|
|
return false
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2022-11-27 00:24:46 +01:00
|
|
|
const libraryChanging = state.currentLibraryId !== libraryId
|
2021-10-05 05:11:42 +02:00
|
|
|
return this.$axios
|
2021-12-01 03:02:40 +01:00
|
|
|
.$get(`/api/libraries/${libraryId}?include=filterdata`)
|
2021-10-05 05:11:42 +02:00
|
|
|
.then((data) => {
|
2022-11-27 21:34:27 +01:00
|
|
|
const library = data.library
|
|
|
|
const filterData = data.filterdata
|
|
|
|
const issues = data.issues || 0
|
2024-02-11 23:48:16 +01:00
|
|
|
const customMetadataProviders = data.customMetadataProviders || []
|
2022-11-27 21:34:27 +01:00
|
|
|
const numUserPlaylists = data.numUserPlaylists
|
2022-04-12 02:42:09 +02:00
|
|
|
|
|
|
|
dispatch('user/checkUpdateLibrarySortFilter', library.mediaType, { root: true })
|
|
|
|
|
2023-12-31 22:09:35 +01:00
|
|
|
if (libraryChanging) {
|
|
|
|
commit('setCollections', [])
|
|
|
|
commit('setUserPlaylists', [])
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
commit('addUpdate', library)
|
2021-12-02 02:07:03 +01:00
|
|
|
commit('setLibraryIssues', issues)
|
2021-12-01 03:02:40 +01:00
|
|
|
commit('setLibraryFilterData', filterData)
|
2022-11-27 21:34:27 +01:00
|
|
|
commit('setNumUserPlaylists', numUserPlaylists)
|
2024-02-11 23:48:16 +01:00
|
|
|
commit('scanners/setCustomMetadataProviders', customMetadataProviders, { root: true })
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
commit('setCurrentLibrary', libraryId)
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed', error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// Return true if calling load
|
|
|
|
load({ state, commit, rootState }) {
|
|
|
|
if (!rootState.user || !rootState.user.user) {
|
|
|
|
console.error('libraries/load - User not set')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't load again if already loaded in the last 5 minutes
|
|
|
|
var lastLoadDiff = Date.now() - state.lastLoad
|
|
|
|
if (lastLoadDiff < 5 * 60 * 1000) {
|
|
|
|
// Already up to date
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$axios
|
|
|
|
.$get(`/api/libraries`)
|
|
|
|
.then((data) => {
|
2022-12-13 00:29:56 +01:00
|
|
|
commit('set', data.libraries)
|
2021-10-05 05:11:42 +02:00
|
|
|
commit('setLastLoad')
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed', error)
|
|
|
|
commit('set', [])
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
},
|
2021-12-01 03:02:40 +01:00
|
|
|
loadLibraryFilterData({ state, commit, rootState }) {
|
|
|
|
if (!rootState.user || !rootState.user.user) {
|
|
|
|
console.error('libraries/loadLibraryFilterData - User not set')
|
|
|
|
return false
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
this.$axios
|
2022-03-11 01:45:02 +01:00
|
|
|
.$get(`/api/libraries/${state.currentLibraryId}/filterdata`)
|
2021-12-01 03:02:40 +01:00
|
|
|
.then((data) => {
|
|
|
|
commit('setLibraryFilterData', data)
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed', error)
|
|
|
|
commit('setLibraryFilterData', null)
|
|
|
|
})
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setFolders(state, folders) {
|
|
|
|
state.folders = folders
|
|
|
|
},
|
|
|
|
setFoldersLastUpdate(state) {
|
|
|
|
state.folderLastUpdate = Date.now()
|
|
|
|
},
|
|
|
|
setLastLoad(state) {
|
|
|
|
state.lastLoad = Date.now()
|
|
|
|
},
|
2021-12-02 02:07:03 +01:00
|
|
|
setLibraryIssues(state, val) {
|
|
|
|
state.issues = val
|
|
|
|
},
|
2021-10-05 05:11:42 +02:00
|
|
|
setCurrentLibrary(state, val) {
|
|
|
|
state.currentLibraryId = val
|
|
|
|
},
|
|
|
|
set(state, libraries) {
|
|
|
|
state.libraries = libraries
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
addUpdate(state, library) {
|
2024-05-26 23:01:08 +02:00
|
|
|
var index = state.libraries.findIndex((a) => a.id === library.id)
|
2021-10-05 05:11:42 +02:00
|
|
|
if (index >= 0) {
|
|
|
|
state.libraries.splice(index, 1, library)
|
|
|
|
} else {
|
|
|
|
state.libraries.push(library)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
remove(state, library) {
|
2024-05-26 23:01:08 +02:00
|
|
|
state.libraries = state.libraries.filter((a) => a.id !== library.id)
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
addListener(state, listener) {
|
2024-05-26 23:01:08 +02:00
|
|
|
var index = state.listeners.findIndex((l) => l.id === listener.id)
|
2021-10-05 05:11:42 +02:00
|
|
|
if (index >= 0) state.listeners.splice(index, 1, listener)
|
|
|
|
else state.listeners.push(listener)
|
|
|
|
},
|
|
|
|
removeListener(state, listenerId) {
|
2024-05-26 23:01:08 +02:00
|
|
|
state.listeners = state.listeners.filter((l) => l.id !== listenerId)
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
setLibraryFilterData(state, filterData) {
|
|
|
|
state.filterData = filterData
|
2021-12-04 00:25:09 +01:00
|
|
|
},
|
2022-11-27 21:34:27 +01:00
|
|
|
setNumUserPlaylists(state, numUserPlaylists) {
|
|
|
|
state.numUserPlaylists = numUserPlaylists
|
|
|
|
},
|
2023-08-18 21:40:36 +02:00
|
|
|
removeSeriesFromFilterData(state, seriesId) {
|
|
|
|
if (!seriesId || !state.filterData) return
|
2024-05-26 23:01:08 +02:00
|
|
|
state.filterData.series = state.filterData.series.filter((se) => se.id !== seriesId)
|
2023-08-18 21:40:36 +02:00
|
|
|
},
|
2022-05-13 23:51:54 +02:00
|
|
|
updateFilterDataWithItem(state, libraryItem) {
|
|
|
|
if (!libraryItem || !state.filterData) return
|
|
|
|
if (state.currentLibraryId !== libraryItem.libraryId) return
|
2021-12-04 00:25:09 +01:00
|
|
|
/*
|
2023-07-15 19:22:13 +02:00
|
|
|
structure of filterData:
|
|
|
|
{
|
2021-12-04 00:25:09 +01:00
|
|
|
authors: [],
|
|
|
|
genres: [],
|
|
|
|
tags: [],
|
|
|
|
series: [],
|
2022-01-10 01:37:16 +01:00
|
|
|
narrators: [],
|
2023-07-15 19:22:13 +02:00
|
|
|
languages: [],
|
|
|
|
publishers: []
|
2021-12-04 00:25:09 +01:00
|
|
|
}
|
|
|
|
*/
|
2023-07-15 19:22:13 +02:00
|
|
|
const mediaMetadata = libraryItem.media.metadata
|
2021-12-04 00:25:09 +01:00
|
|
|
|
2022-05-13 23:51:54 +02:00
|
|
|
// Add/update book authors
|
2023-07-15 19:22:13 +02:00
|
|
|
if (mediaMetadata.authors?.length) {
|
2022-05-13 23:51:54 +02:00
|
|
|
mediaMetadata.authors.forEach((author) => {
|
2024-05-26 23:01:08 +02:00
|
|
|
const indexOf = state.filterData.authors.findIndex((au) => au.id === author.id)
|
2022-05-13 23:51:54 +02:00
|
|
|
if (indexOf >= 0) {
|
|
|
|
state.filterData.authors.splice(indexOf, 1, author)
|
|
|
|
} else {
|
2021-12-04 00:25:09 +01:00
|
|
|
state.filterData.authors.push(author)
|
2024-05-26 23:01:08 +02:00
|
|
|
state.filterData.authors.sort((a, b) => (a.name || '').localeCompare(b.name || ''))
|
2021-12-04 00:25:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-05-13 23:51:54 +02:00
|
|
|
|
|
|
|
// Add/update series
|
2023-07-15 19:22:13 +02:00
|
|
|
if (mediaMetadata.series?.length) {
|
2022-05-13 23:51:54 +02:00
|
|
|
mediaMetadata.series.forEach((series) => {
|
2024-05-26 23:01:08 +02:00
|
|
|
const indexOf = state.filterData.series.findIndex((se) => se.id === series.id)
|
2022-05-13 23:51:54 +02:00
|
|
|
if (indexOf >= 0) {
|
|
|
|
state.filterData.series.splice(indexOf, 1, { id: series.id, name: series.name })
|
|
|
|
} else {
|
|
|
|
state.filterData.series.push({ id: series.id, name: series.name })
|
2024-05-26 23:01:08 +02:00
|
|
|
state.filterData.series.sort((a, b) => (a.name || '').localeCompare(b.name || ''))
|
2021-12-04 00:25:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-05-13 23:51:54 +02:00
|
|
|
|
|
|
|
// Add genres
|
2023-07-15 19:22:13 +02:00
|
|
|
if (mediaMetadata.genres?.length) {
|
2022-05-13 23:51:54 +02:00
|
|
|
mediaMetadata.genres.forEach((genre) => {
|
|
|
|
if (!state.filterData.genres.includes(genre)) {
|
|
|
|
state.filterData.genres.push(genre)
|
|
|
|
state.filterData.genres.sort((a, b) => a.localeCompare(b))
|
|
|
|
}
|
|
|
|
})
|
2021-12-04 00:25:09 +01:00
|
|
|
}
|
2022-05-13 23:51:54 +02:00
|
|
|
|
|
|
|
// Add tags
|
2023-07-15 19:22:13 +02:00
|
|
|
if (libraryItem.media.tags?.length) {
|
2022-05-13 23:51:54 +02:00
|
|
|
libraryItem.media.tags.forEach((tag) => {
|
|
|
|
if (!state.filterData.tags.includes(tag)) {
|
|
|
|
state.filterData.tags.push(tag)
|
|
|
|
state.filterData.tags.sort((a, b) => a.localeCompare(b))
|
|
|
|
}
|
2021-12-04 00:25:09 +01:00
|
|
|
})
|
|
|
|
}
|
2022-05-13 23:51:54 +02:00
|
|
|
|
|
|
|
// Add narrators
|
2023-07-15 19:22:13 +02:00
|
|
|
if (mediaMetadata.narrators?.length) {
|
2022-05-13 23:51:54 +02:00
|
|
|
mediaMetadata.narrators.forEach((narrator) => {
|
|
|
|
if (!state.filterData.narrators.includes(narrator)) {
|
|
|
|
state.filterData.narrators.push(narrator)
|
|
|
|
state.filterData.narrators.sort((a, b) => a.localeCompare(b))
|
|
|
|
}
|
2021-12-04 00:25:09 +01:00
|
|
|
})
|
|
|
|
}
|
2022-05-13 23:51:54 +02:00
|
|
|
|
2023-07-15 19:22:13 +02:00
|
|
|
// Add publishers
|
|
|
|
if (mediaMetadata.publisher && !state.filterData.publishers.includes(mediaMetadata.publisher)) {
|
|
|
|
state.filterData.publishers.push(mediaMetadata.publisher)
|
|
|
|
state.filterData.publishers.sort((a, b) => a.localeCompare(b))
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:51:54 +02:00
|
|
|
// Add language
|
2023-07-15 19:22:13 +02:00
|
|
|
if (mediaMetadata.language && !state.filterData.languages.includes(mediaMetadata.language)) {
|
|
|
|
state.filterData.languages.push(mediaMetadata.language)
|
|
|
|
state.filterData.languages.sort((a, b) => a.localeCompare(b))
|
2022-01-10 01:37:16 +01:00
|
|
|
}
|
2022-10-29 01:10:19 +02:00
|
|
|
},
|
2022-11-12 00:44:19 +01:00
|
|
|
setCollections(state, collections) {
|
|
|
|
state.collections = collections
|
|
|
|
},
|
|
|
|
addUpdateCollection(state, collection) {
|
2024-05-26 23:01:08 +02:00
|
|
|
var index = state.collections.findIndex((c) => c.id === collection.id)
|
2022-11-12 00:44:19 +01:00
|
|
|
if (index >= 0) {
|
|
|
|
state.collections.splice(index, 1, collection)
|
|
|
|
} else {
|
|
|
|
state.collections.push(collection)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
removeCollection(state, collection) {
|
2024-05-26 23:01:08 +02:00
|
|
|
state.collections = state.collections.filter((c) => c.id !== collection.id)
|
2022-11-27 00:24:46 +01:00
|
|
|
},
|
|
|
|
setUserPlaylists(state, playlists) {
|
|
|
|
state.userPlaylists = playlists
|
2022-11-27 21:34:27 +01:00
|
|
|
state.numUserPlaylists = playlists.length
|
2022-11-27 00:24:46 +01:00
|
|
|
},
|
|
|
|
addUpdateUserPlaylist(state, playlist) {
|
2024-05-26 23:01:08 +02:00
|
|
|
const index = state.userPlaylists.findIndex((p) => p.id === playlist.id)
|
2022-11-27 00:24:46 +01:00
|
|
|
if (index >= 0) {
|
|
|
|
state.userPlaylists.splice(index, 1, playlist)
|
|
|
|
} else {
|
|
|
|
state.userPlaylists.push(playlist)
|
2022-11-27 21:34:27 +01:00
|
|
|
state.numUserPlaylists++
|
2022-11-27 00:24:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
removeUserPlaylist(state, playlist) {
|
2024-05-26 23:01:08 +02:00
|
|
|
state.userPlaylists = state.userPlaylists.filter((p) => p.id !== playlist.id)
|
2022-11-27 21:34:27 +01:00
|
|
|
state.numUserPlaylists = state.userPlaylists.length
|
2023-05-30 00:38:38 +02:00
|
|
|
},
|
|
|
|
setEReaderDevices(state, ereaderDevices) {
|
|
|
|
state.ereaderDevices = ereaderDevices
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
2024-05-26 23:01:08 +02:00
|
|
|
}
|