2021-09-05 20:21:02 +02:00
|
|
|
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']
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
export const state = () => ({
|
|
|
|
audiobooks: [],
|
2021-10-05 05:11:42 +02:00
|
|
|
loadedLibraryId: '',
|
2021-08-20 00:29:36 +02:00
|
|
|
listeners: [],
|
|
|
|
genres: [...STANDARD_GENRES],
|
2021-08-22 15:52:37 +02:00
|
|
|
tags: [],
|
2021-09-05 20:21:02 +02:00
|
|
|
series: [],
|
2021-09-27 13:52:21 +02:00
|
|
|
keywordFilter: null,
|
2021-12-02 02:07:03 +01:00
|
|
|
selectedSeries: null
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = {
|
2021-10-05 05:11:42 +02:00
|
|
|
getBookCoverSrc: (state, getters, rootState, rootGetters) => (bookItem, placeholder = '/book_placeholder.jpg') => {
|
2021-12-01 03:02:40 +01:00
|
|
|
if (!bookItem) return placeholder
|
2021-10-05 05:11:42 +02:00
|
|
|
var book = bookItem.book
|
2021-09-24 14:32:38 +02:00
|
|
|
if (!book || !book.cover || book.cover === placeholder) return placeholder
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
// Absolute URL covers (should no longer be used)
|
2021-12-13 00:15:37 +01:00
|
|
|
if (book.cover.startsWith('http:') || book.cover.startsWith('https:')) return book.cover
|
2021-09-24 14:32:38 +02:00
|
|
|
|
2021-12-13 00:15:37 +01:00
|
|
|
var userToken = rootGetters['user/getToken']
|
|
|
|
var bookLastUpdate = book.lastUpdate || Date.now()
|
2021-09-24 14:32:38 +02:00
|
|
|
|
2021-12-13 00:15:37 +01:00
|
|
|
if (process.env.NODE_ENV !== 'production') { // Testing
|
|
|
|
return `http://localhost:3333/api/books/${bookItem.id}/cover?token=${userToken}&ts=${bookLastUpdate}`
|
2021-09-24 14:32:38 +02:00
|
|
|
}
|
2021-12-13 00:15:37 +01:00
|
|
|
return `/api/books/${bookItem.id}/cover?token=${userToken}`
|
2021-08-19 03:18:44 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
2021-09-05 20:21:02 +02:00
|
|
|
setKeywordFilter(state, val) {
|
|
|
|
state.keywordFilter = val
|
|
|
|
},
|
2021-09-27 13:52:21 +02:00
|
|
|
setSelectedSeries(state, val) {
|
|
|
|
state.selectedSeries = val
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
set(state, audiobooks) {
|
2021-08-20 00:29:36 +02:00
|
|
|
// GENRES
|
|
|
|
var genres = [...state.genres]
|
|
|
|
audiobooks.forEach((ab) => {
|
|
|
|
if (!ab.book) return
|
|
|
|
genres = genres.concat(ab.book.genres)
|
|
|
|
})
|
|
|
|
state.genres = [...new Set(genres)] // Remove Duplicates
|
2021-08-22 15:52:37 +02:00
|
|
|
state.genres.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
2021-08-20 00:29:36 +02:00
|
|
|
|
|
|
|
// TAGS
|
|
|
|
var tags = []
|
|
|
|
audiobooks.forEach((ab) => {
|
|
|
|
tags = tags.concat(ab.tags)
|
|
|
|
})
|
|
|
|
state.tags = [...new Set(tags)] // Remove Duplicates
|
2021-08-22 15:52:37 +02:00
|
|
|
state.tags.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
|
|
|
|
|
|
|
// SERIES
|
|
|
|
var series = []
|
|
|
|
audiobooks.forEach((ab) => {
|
|
|
|
if (!ab.book || !ab.book.series || series.includes(ab.book.series)) return
|
|
|
|
series.push(ab.book.series)
|
|
|
|
})
|
|
|
|
state.series = series
|
|
|
|
state.series.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
state.audiobooks = audiobooks
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
listener.meth()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
addUpdate(state, audiobook) {
|
2021-10-07 04:08:52 +02:00
|
|
|
if (state.loadedLibraryId && audiobook.libraryId !== state.loadedLibraryId) {
|
|
|
|
console.warn('Invalid library', audiobook, 'loaded library', state.loadedLibraryId, '"')
|
2021-10-05 05:11:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
var index = state.audiobooks.findIndex(a => a.id === audiobook.id)
|
2021-08-22 15:52:37 +02:00
|
|
|
var origAudiobook = null
|
2021-08-18 00:01:11 +02:00
|
|
|
if (index >= 0) {
|
2021-08-22 15:52:37 +02:00
|
|
|
origAudiobook = { ...state.audiobooks[index] }
|
2021-08-18 00:01:11 +02:00
|
|
|
state.audiobooks.splice(index, 1, audiobook)
|
|
|
|
} else {
|
|
|
|
state.audiobooks.push(audiobook)
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
if (audiobook.book) {
|
2021-08-22 15:52:37 +02:00
|
|
|
// GENRES
|
2021-08-20 00:29:36 +02:00
|
|
|
var newGenres = []
|
|
|
|
audiobook.book.genres.forEach((genre) => {
|
|
|
|
if (!state.genres.includes(genre)) newGenres.push(genre)
|
|
|
|
})
|
2021-08-22 15:52:37 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-08-20 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TAGS
|
|
|
|
var newTags = []
|
|
|
|
audiobook.tags.forEach((tag) => {
|
|
|
|
if (!state.tags.includes(tag)) newTags.push(tag)
|
|
|
|
})
|
2021-08-22 15:52:37 +02:00
|
|
|
if (newTags.length) {
|
|
|
|
state.tags = state.tags.concat(newTags)
|
|
|
|
state.tags.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
|
|
|
|
}
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
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)
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
if (audiobook.book) {
|
2021-08-22 15:52:37 +02:00
|
|
|
// GENRES
|
2021-08-20 00:29:36 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-08-22 15:52:37 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
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)
|
2021-12-02 02:07:03 +01:00
|
|
|
},
|
|
|
|
audiobookUpdated(state, audiobook) {
|
|
|
|
state.listeners.forEach((listener) => {
|
|
|
|
if (!listener.audiobookId || listener.audiobookId === audiobook.id) {
|
|
|
|
listener.meth()
|
|
|
|
}
|
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|