2021-10-05 05:11:42 +02:00
|
|
|
export const state = () => ({
|
|
|
|
libraries: [],
|
|
|
|
lastLoad: 0,
|
|
|
|
listeners: [],
|
|
|
|
currentLibraryId: 'main',
|
|
|
|
folders: [],
|
2021-12-02 02:07:03 +01:00
|
|
|
issues: 0,
|
2021-12-01 03:02:40 +01:00
|
|
|
folderLastUpdate: 0,
|
|
|
|
filterData: null
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getCurrentLibrary: state => {
|
|
|
|
return state.libraries.find(lib => lib.id === state.currentLibraryId)
|
2021-10-13 03:07:42 +02:00
|
|
|
},
|
|
|
|
getSortedLibraries: state => () => {
|
|
|
|
return state.libraries.map(lib => ({ ...lib })).sort((a, b) => a.displayOrder - b.displayOrder)
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
loadFolders({ state, commit }) {
|
|
|
|
if (state.folders.length) {
|
|
|
|
var lastCheck = Date.now() - state.folderLastUpdate
|
|
|
|
if (lastCheck < 1000 * 60 * 10) { // 10 minutes
|
|
|
|
// Folders up to date
|
|
|
|
return state.folders
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('Loading folders')
|
|
|
|
commit('setFoldersLastUpdate')
|
|
|
|
|
|
|
|
return this.$axios
|
|
|
|
.$get('/api/filesystem')
|
|
|
|
.then((res) => {
|
|
|
|
console.log('Settings folders', res)
|
|
|
|
commit('setFolders', res)
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed to load dirs', error)
|
|
|
|
commit('setFolders', [])
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
},
|
2021-10-23 03:08:02 +02:00
|
|
|
fetch({ state, 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-12-01 03:02:40 +01:00
|
|
|
// var library = state.libraries.find(lib => lib.id === libraryId)
|
|
|
|
// if (library) {
|
|
|
|
// commit('setCurrentLibrary', libraryId)
|
|
|
|
// return library
|
|
|
|
// }
|
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) => {
|
2021-12-01 03:02:40 +01:00
|
|
|
var library = data.library
|
|
|
|
var filterData = data.filterdata
|
2021-12-02 02:07:03 +01:00
|
|
|
var issues = data.issues || 0
|
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)
|
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) => {
|
|
|
|
commit('set', data)
|
|
|
|
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
|
|
|
|
.$get(`/api/libraries/${state.currentLibraryId}/filters`)
|
|
|
|
.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) {
|
|
|
|
var index = state.libraries.findIndex(a => a.id === library.id)
|
|
|
|
if (index >= 0) {
|
|
|
|
state.libraries.splice(index, 1, library)
|
|
|
|
} else {
|
|
|
|
state.libraries.push(library)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
remove(state, library) {
|
|
|
|
state.libraries = state.libraries.filter(a => a.id !== library.id)
|
|
|
|
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
addListener(state, listener) {
|
|
|
|
var index = state.listeners.findIndex(l => l.id === listener.id)
|
|
|
|
if (index >= 0) state.listeners.splice(index, 1, listener)
|
|
|
|
else state.listeners.push(listener)
|
|
|
|
},
|
|
|
|
removeListener(state, listenerId) {
|
|
|
|
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
|
|
|
},
|
|
|
|
updateFilterDataWithAudiobook(state, audiobook) {
|
|
|
|
if (!audiobook || !audiobook.book || !state.filterData) return
|
|
|
|
if (state.currentLibraryId !== audiobook.libraryId) return
|
|
|
|
/*
|
|
|
|
var filterdata = {
|
|
|
|
authors: [],
|
|
|
|
genres: [],
|
|
|
|
tags: [],
|
|
|
|
series: [],
|
|
|
|
narrators: []
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (audiobook.book.authorFL) {
|
|
|
|
audiobook.book.authorFL.split(', ').forEach((author) => {
|
|
|
|
if (author && !state.filterData.authors.includes(author)) {
|
|
|
|
state.filterData.authors.push(author)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (audiobook.book.narratorFL) {
|
|
|
|
audiobook.book.narratorFL.split(', ').forEach((narrator) => {
|
|
|
|
if (narrator && !state.filterData.narrators.includes(narrator)) {
|
|
|
|
state.filterData.narrators.push(narrator)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (audiobook.book.series && !state.filterData.series.includes(audiobook.book.series)) {
|
|
|
|
state.filterData.series.push(audiobook.book.series)
|
|
|
|
}
|
|
|
|
if (audiobook.tags && audiobook.tags.length) {
|
|
|
|
audiobook.tags.forEach((tag) => {
|
|
|
|
if (tag && !state.filterData.tags.includes(tag)) state.filterData.tags.push(tag)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (audiobook.book.genres && audiobook.book.genres.length) {
|
|
|
|
audiobook.book.genres.forEach((genre) => {
|
2021-12-05 16:52:23 +01:00
|
|
|
if (genre && !state.filterData.genres.includes(genre)) state.filterData.genres.push(genre)
|
2021-12-04 00:25:09 +01:00
|
|
|
})
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
}
|