mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-06 20:08:48 +02:00
Finish lazy bookshelf,Fix:Async sql db,Fix:Load user progress
This commit is contained in:
+2
-127
@@ -1,17 +1,8 @@
|
||||
const STANDARD_GENRES = ['adventure', 'autobiography', 'biography', 'childrens', 'comedy', 'crime', 'dystopian', 'fantasy', 'fiction', 'health', 'history', 'horror', 'mystery', 'new_adult', 'nonfiction', 'philosophy', 'politics', 'religion', 'romance', 'sci-fi', 'self-help', 'short_story', 'technology', 'thriller', 'true_crime', 'western', 'young_adult']
|
||||
|
||||
export const state = () => ({
|
||||
audiobooks: [],
|
||||
listeners: [],
|
||||
loadedLibraryId: 'main',
|
||||
lastLoad: 0,
|
||||
isLoading: false
|
||||
|
||||
})
|
||||
|
||||
export const getters = {
|
||||
getAudiobook: state => id => {
|
||||
return state.audiobooks.find(ab => ab.id === id)
|
||||
},
|
||||
getBookCoverSrc: (state, getters, rootState, rootGetters) => (bookItem, placeholder = '/book_placeholder.jpg') => {
|
||||
var book = bookItem.book
|
||||
if (!book || !book.cover || book.cover === placeholder) return placeholder
|
||||
@@ -44,125 +35,9 @@ export const getters = {
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
useDownloaded({ commit, rootGetters }) {
|
||||
commit('set', rootGetters['downloads/getAudiobooks'])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const mutations = {
|
||||
setLoadedLibrary(state, val) {
|
||||
state.loadedLibraryId = val
|
||||
},
|
||||
setLoading(state, val) {
|
||||
state.isLoading = val
|
||||
},
|
||||
setLastLoad(state, val) {
|
||||
state.lastLoad = val
|
||||
},
|
||||
reset(state) {
|
||||
state.audiobooks = []
|
||||
state.genres = [...STANDARD_GENRES]
|
||||
state.tags = []
|
||||
state.series = []
|
||||
},
|
||||
addUpdate(state, audiobook) {
|
||||
var index = state.audiobooks.findIndex(a => a.id === audiobook.id)
|
||||
var origAudiobook = null
|
||||
if (index >= 0) {
|
||||
origAudiobook = { ...state.audiobooks[index] }
|
||||
state.audiobooks.splice(index, 1, audiobook)
|
||||
} else {
|
||||
state.audiobooks.push(audiobook)
|
||||
}
|
||||
|
||||
if (audiobook.book) {
|
||||
// GENRES
|
||||
var newGenres = []
|
||||
audiobook.book.genres.forEach((genre) => {
|
||||
if (!state.genres.includes(genre)) newGenres.push(genre)
|
||||
})
|
||||
if (newGenres.length) {
|
||||
state.genres = state.genres.concat(newGenres)
|
||||
state.genres.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
||||
}
|
||||
|
||||
// SERIES
|
||||
if (audiobook.book.series && !state.series.includes(audiobook.book.series)) {
|
||||
state.series.push(audiobook.book.series)
|
||||
state.series.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
||||
}
|
||||
if (origAudiobook && origAudiobook.book && origAudiobook.book.series) {
|
||||
var isInAB = state.audiobooks.find(ab => ab.book && ab.book.series === origAudiobook.book.series)
|
||||
if (!isInAB) state.series = state.series.filter(series => series !== origAudiobook.book.series)
|
||||
}
|
||||
}
|
||||
|
||||
// TAGS
|
||||
var newTags = []
|
||||
audiobook.tags.forEach((tag) => {
|
||||
if (!state.tags.includes(tag)) newTags.push(tag)
|
||||
})
|
||||
if (newTags.length) {
|
||||
state.tags = state.tags.concat(newTags)
|
||||
state.tags.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
||||
}
|
||||
|
||||
state.listeners.forEach((listener) => {
|
||||
if (!listener.audiobookId || listener.audiobookId === audiobook.id) {
|
||||
listener.meth()
|
||||
}
|
||||
})
|
||||
},
|
||||
remove(state, audiobook) {
|
||||
state.audiobooks = state.audiobooks.filter(a => a.id !== audiobook.id)
|
||||
|
||||
if (audiobook.book) {
|
||||
// GENRES
|
||||
audiobook.book.genres.forEach((genre) => {
|
||||
if (!STANDARD_GENRES.includes(genre)) {
|
||||
var isInOtherAB = state.audiobooks.find(ab => {
|
||||
return ab.book && ab.book.genres.includes(genre)
|
||||
})
|
||||
if (!isInOtherAB) {
|
||||
// Genre is not used by any other audiobook - remove it
|
||||
state.genres = state.genres.filter(g => g !== genre)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// SERIES
|
||||
if (audiobook.book.series) {
|
||||
var isInOtherAB = state.audiobooks.find(ab => ab.book && ab.book.series === audiobook.book.series)
|
||||
if (!isInOtherAB) {
|
||||
// Series not used in any other audiobook - remove it
|
||||
state.series = state.series.filter(s => s !== audiobook.book.series)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TAGS
|
||||
audiobook.tags.forEach((tag) => {
|
||||
var isInOtherAB = state.audiobooks.find(ab => {
|
||||
return ab.tags.includes(tag)
|
||||
})
|
||||
if (!isInOtherAB) {
|
||||
// Tag is not used by any other audiobook - remove it
|
||||
state.tags = state.tags.filter(t => t !== tag)
|
||||
}
|
||||
})
|
||||
|
||||
state.listeners.forEach((listener) => {
|
||||
if (!listener.audiobookId || listener.audiobookId === audiobook.id) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
+12
-13
@@ -10,6 +10,9 @@ export const getters = {
|
||||
getDownload: (state) => id => {
|
||||
return state.downloads.find(d => d.id === id)
|
||||
},
|
||||
getDownloads: state => {
|
||||
return state.downloads
|
||||
},
|
||||
getDownloadIfReady: (state) => id => {
|
||||
var download = state.downloads.find(d => d.id === id)
|
||||
return !!download && !download.isDownloading && !download.isPreparing ? download : null
|
||||
@@ -22,7 +25,7 @@ export const getters = {
|
||||
export const actions = {
|
||||
async loadFromStorage({ commit, state }) {
|
||||
var downloads = await this.$sqlStore.getAllDownloads()
|
||||
|
||||
console.log('Load downloads from storage', downloads.length)
|
||||
downloads.forEach(ab => {
|
||||
if (ab.isDownloading || ab.isPreparing) {
|
||||
ab.isIncomplete = true
|
||||
@@ -37,27 +40,23 @@ export const actions = {
|
||||
if (!state.mediaScanResults || !state.mediaScanResults.folders) {
|
||||
return
|
||||
}
|
||||
console.log('Link orphan downloads', JSON.stringify(state.mediaScanResults.folders))
|
||||
// state.mediaScanResults.folders.forEach((folder) => {
|
||||
|
||||
for (let i = 0; i < state.mediaScanResults.folders.length; i++) {
|
||||
var folder = state.mediaScanResults.folders[i]
|
||||
if (!folder.files || !folder.files.length) return
|
||||
|
||||
console.log('Link orphan downloads check folder', folder.name)
|
||||
var download = state.downloads.find(dl => dl.folderName === folder.name)
|
||||
if (!download) {
|
||||
// var matchingAb = audiobooks.find(ab => ab.book.title === folder.name)
|
||||
var results = await this.$axios.$get(`/libraries/${rootState.libraries.currentLibraryId}/search?q=${folder.name}`)
|
||||
var results = await this.$axios.$get(`/api/libraries/${rootState.libraries.currentLibraryId}/search?q=${folder.name}`)
|
||||
var matchingAb = null
|
||||
if (results && results.audiobooks) {
|
||||
console.log('has ab results', JSON.stringify(results.audiobooks))
|
||||
matchingAb = results.audiobooks.find(ab => ab.audiobook.book.title === folder.name)
|
||||
if (matchingAb) console.log('Found matching ab for ' + folder.name, matchingAb)
|
||||
else console.warn('did not find mathcing ab for ' + folder.name)
|
||||
} else {
|
||||
console.error('Invalid results payload', JSON.stringify(results))
|
||||
matchingAb = results.audiobooks.find(ab => {
|
||||
return ab.audiobook.book.title === folder.name
|
||||
})
|
||||
}
|
||||
|
||||
if (matchingAb) {
|
||||
matchingAb = matchingAb.audiobook
|
||||
// Found matching download for ab
|
||||
var audioFile = folder.files.find(f => f.isAudio)
|
||||
if (!audioFile) {
|
||||
@@ -80,7 +79,6 @@ export const actions = {
|
||||
coverSize: coverImg ? coverImg.size : 0,
|
||||
coverBasePath: ''
|
||||
}
|
||||
console.log('Linking orphan download: ' + JSON.stringify(downloadObj))
|
||||
commit('addUpdateDownload', downloadObj)
|
||||
}
|
||||
}
|
||||
@@ -105,6 +103,7 @@ export const mutations = {
|
||||
},
|
||||
addUpdateDownload(state, download) {
|
||||
if (!download || !download.id) {
|
||||
console.error('Orphan invalid download ' + download.id)
|
||||
return
|
||||
}
|
||||
var index = state.downloads.findIndex(d => d.id === download.id)
|
||||
|
||||
@@ -135,5 +135,6 @@ export const mutations = {
|
||||
},
|
||||
setServerSettings(state, val) {
|
||||
state.serverSettings = val
|
||||
this.$localStore.setServerSettings(state.serverSettings)
|
||||
}
|
||||
}
|
||||
@@ -136,4 +136,45 @@ export const mutations = {
|
||||
setLibraryFilterData(state, filterData) {
|
||||
state.filterData = filterData
|
||||
},
|
||||
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) => {
|
||||
if (genre && !state.filterData.genres.includes(genre)) state.filterData.genres.push(genre)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-35
@@ -12,9 +12,7 @@ export const state = () => ({
|
||||
bookshelfCoverSize: 120
|
||||
},
|
||||
settingsListeners: [],
|
||||
userAudiobooksListeners: [],
|
||||
collections: [],
|
||||
collectionsLoaded: false
|
||||
userAudiobooksListeners: []
|
||||
})
|
||||
|
||||
export const getters = {
|
||||
@@ -33,9 +31,6 @@ export const getters = {
|
||||
},
|
||||
getFilterOrderKey: (state) => {
|
||||
return Object.values(state.settings).join('-')
|
||||
},
|
||||
getCollection: state => id => {
|
||||
return state.collections.find(c => c.id === id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,23 +56,14 @@ export const actions = {
|
||||
commit('setSettings', payload)
|
||||
}
|
||||
},
|
||||
loadUserCollections({ state, commit }) {
|
||||
if (!this.$server.connected) {
|
||||
console.error('Not loading collections - not connected')
|
||||
return []
|
||||
async loadOfflineUserAudiobookData({ state, commit }) {
|
||||
var localUserAudiobookData = await this.$sqlStore.getAllUserAudiobookData() || []
|
||||
if (localUserAudiobookData.length) {
|
||||
console.log('loadOfflineUserAudiobookData found', localUserAudiobookData.length, 'user audiobook data')
|
||||
commit('setAllUserAudiobookData', localUserAudiobookData)
|
||||
} else {
|
||||
console.log('loadOfflineUserAudiobookData No user audiobook data')
|
||||
}
|
||||
if (state.collectionsLoaded) {
|
||||
console.log('Collections already loaded')
|
||||
return state.collections
|
||||
}
|
||||
|
||||
return this.$axios.$get('/api/collections').then((collections) => {
|
||||
commit('setCollections', collections)
|
||||
return collections
|
||||
}).catch((error) => {
|
||||
console.error('Failed to get collections', error)
|
||||
return []
|
||||
})
|
||||
},
|
||||
async syncUserAudiobookData({ state, commit }) {
|
||||
if (!state.user) {
|
||||
@@ -167,17 +153,5 @@ export const mutations = {
|
||||
},
|
||||
removeUserAudiobookListener(state, listenerId) {
|
||||
state.userAudiobooksListeners = state.userAudiobooksListeners.filter(l => l.id !== listenerId)
|
||||
},
|
||||
setCollections(state, collections) {
|
||||
state.collections = collections
|
||||
state.collectionsLoaded = true
|
||||
},
|
||||
addUpdateCollection(state, collection) {
|
||||
var index = state.collections.findIndex(c => c.id === collection.id)
|
||||
if (index >= 0) {
|
||||
state.collections.splice(index, 1, collection)
|
||||
} else {
|
||||
state.collections.push(collection)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user