2021-08-24 01:31:04 +02:00
|
|
|
|
2021-10-24 22:53:51 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
export const state = () => ({
|
|
|
|
user: null,
|
|
|
|
settings: {
|
|
|
|
orderBy: 'book.title',
|
|
|
|
orderDesc: false,
|
|
|
|
filterBy: 'all',
|
2021-08-26 16:47:51 +02:00
|
|
|
playbackRate: 1,
|
2022-01-25 01:03:54 +01:00
|
|
|
bookshelfCoverSize: 120,
|
|
|
|
collapseSeries: false
|
2021-08-24 01:31:04 +02:00
|
|
|
},
|
2021-11-06 02:24:02 +01:00
|
|
|
settingsListeners: [],
|
|
|
|
collections: [],
|
2021-11-07 02:31:46 +01:00
|
|
|
collectionsLoaded: false,
|
|
|
|
collectionsListeners: []
|
2021-08-24 01:31:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = {
|
2021-08-27 14:01:47 +02:00
|
|
|
getIsRoot: (state) => state.user && state.user.type === 'root',
|
2021-08-24 01:31:04 +02:00
|
|
|
getToken: (state) => {
|
|
|
|
return state.user ? state.user.token : null
|
|
|
|
},
|
2022-03-26 23:41:26 +01:00
|
|
|
getUserMediaProgress: (state) => (libraryItemId, episodeId = null) => {
|
2022-03-26 17:59:34 +01:00
|
|
|
if (!state.user.mediaProgress) return null
|
2022-03-26 23:41:26 +01:00
|
|
|
return state.user.mediaProgress.find(li => {
|
|
|
|
if (episodeId && li.episodeId !== episodeId) return false
|
2022-04-09 02:27:35 +02:00
|
|
|
return li.libraryItemId == libraryItemId
|
2022-03-26 23:41:26 +01:00
|
|
|
})
|
2021-08-24 01:31:04 +02:00
|
|
|
},
|
2022-03-18 02:28:04 +01:00
|
|
|
getUserBookmarksForItem: (state) => (libraryItemId) => {
|
|
|
|
if (!state.user.bookmarks) return []
|
|
|
|
return state.user.bookmarks.filter(bm => bm.libraryItemId === libraryItemId)
|
|
|
|
},
|
2021-08-24 01:31:04 +02:00
|
|
|
getUserSetting: (state) => (key) => {
|
2021-12-01 03:02:40 +01:00
|
|
|
return state.settings ? state.settings[key] : null
|
2021-08-24 01:31:04 +02:00
|
|
|
},
|
2021-09-07 00:42:15 +02:00
|
|
|
getUserCanUpdate: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.update : false
|
|
|
|
},
|
|
|
|
getUserCanDelete: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.delete : false
|
|
|
|
},
|
|
|
|
getUserCanDownload: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.download : false
|
2021-09-18 19:45:34 +02:00
|
|
|
},
|
|
|
|
getUserCanUpload: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.upload : false
|
2021-10-23 03:08:02 +02:00
|
|
|
},
|
|
|
|
getUserCanAccessAllLibraries: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.accessAllLibraries : false
|
|
|
|
},
|
|
|
|
getLibrariesAccessible: (state, getters) => {
|
|
|
|
if (!state.user) return []
|
|
|
|
if (getters.getUserCanAccessAllLibraries) return []
|
|
|
|
return state.user.librariesAccessible || []
|
|
|
|
},
|
|
|
|
getCanAccessLibrary: (state, getters) => (libraryId) => {
|
|
|
|
if (!state.user) return false
|
|
|
|
if (getters.getUserCanAccessAllLibraries) return true
|
|
|
|
return getters.getLibrariesAccessible.includes(libraryId)
|
2021-11-07 22:28:06 +01:00
|
|
|
},
|
|
|
|
getCollection: state => id => {
|
|
|
|
return state.collections.find(c => c.id === id)
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
updateUserSettings({ commit }, payload) {
|
|
|
|
var updatePayload = {
|
|
|
|
...payload
|
|
|
|
}
|
2021-09-21 23:42:01 +02:00
|
|
|
// Immediately update
|
|
|
|
commit('setSettings', updatePayload)
|
2021-11-22 03:00:40 +01:00
|
|
|
return this.$axios.$patch('/api/me/settings', updatePayload).then((result) => {
|
2021-08-24 01:31:04 +02:00
|
|
|
if (result.success) {
|
|
|
|
commit('setSettings', result.settings)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
console.error('Failed to update settings', error)
|
|
|
|
return false
|
|
|
|
})
|
2021-11-06 02:24:02 +01:00
|
|
|
},
|
|
|
|
loadUserCollections({ state, commit }) {
|
|
|
|
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 []
|
|
|
|
})
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setUser(state, user) {
|
|
|
|
state.user = user
|
2021-08-24 02:37:40 +02:00
|
|
|
if (user) {
|
|
|
|
if (user.token) localStorage.setItem('token', user.token)
|
|
|
|
} else {
|
2021-08-24 01:31:04 +02:00
|
|
|
localStorage.removeItem('token')
|
|
|
|
}
|
|
|
|
},
|
2022-03-26 17:59:34 +01:00
|
|
|
updateMediaProgress(state, { id, data }) {
|
2021-10-24 22:53:51 +02:00
|
|
|
if (!state.user) return
|
2022-03-17 19:33:22 +01:00
|
|
|
if (!data) {
|
2022-03-26 17:59:34 +01:00
|
|
|
state.user.mediaProgress = state.user.mediaProgress.filter(lip => lip.id != id)
|
2022-03-17 19:33:22 +01:00
|
|
|
} else {
|
2022-03-26 17:59:34 +01:00
|
|
|
var indexOf = state.user.mediaProgress.findIndex(lip => lip.id == id)
|
2022-03-17 19:33:22 +01:00
|
|
|
if (indexOf >= 0) {
|
2022-03-26 17:59:34 +01:00
|
|
|
state.user.mediaProgress.splice(indexOf, 1, data)
|
2022-03-17 19:33:22 +01:00
|
|
|
} else {
|
2022-03-26 17:59:34 +01:00
|
|
|
state.user.mediaProgress.push(data)
|
2022-03-17 19:33:22 +01:00
|
|
|
}
|
2021-10-24 22:53:51 +02:00
|
|
|
}
|
|
|
|
},
|
2021-08-24 01:31:04 +02:00
|
|
|
setSettings(state, settings) {
|
|
|
|
if (!settings) return
|
|
|
|
|
|
|
|
var hasChanges = false
|
|
|
|
for (const key in settings) {
|
|
|
|
if (state.settings[key] !== settings[key]) {
|
|
|
|
hasChanges = true
|
|
|
|
state.settings[key] = settings[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasChanges) {
|
|
|
|
state.settingsListeners.forEach((listener) => {
|
|
|
|
listener.meth(state.settings)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addSettingsListener(state, listener) {
|
|
|
|
var index = state.settingsListeners.findIndex(l => l.id === listener.id)
|
|
|
|
if (index >= 0) state.settingsListeners.splice(index, 1, listener)
|
|
|
|
else state.settingsListeners.push(listener)
|
|
|
|
},
|
|
|
|
removeSettingsListener(state, listenerId) {
|
|
|
|
state.settingsListeners = state.settingsListeners.filter(l => l.id !== listenerId)
|
2021-11-06 02:24:02 +01:00
|
|
|
},
|
|
|
|
setCollections(state, collections) {
|
|
|
|
state.collectionsLoaded = true
|
|
|
|
state.collections = collections
|
2021-11-08 01:11:29 +01:00
|
|
|
state.collectionsListeners.forEach((listener) => listener.meth())
|
2021-11-06 02:24:02 +01:00
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
2021-11-07 02:31:46 +01:00
|
|
|
state.collectionsListeners.forEach((listener) => listener.meth())
|
2021-11-06 02:24:02 +01:00
|
|
|
},
|
|
|
|
removeCollection(state, collection) {
|
|
|
|
state.collections = state.collections.filter(c => c.id !== collection.id)
|
2021-11-07 02:31:46 +01:00
|
|
|
state.collectionsListeners.forEach((listener) => listener.meth())
|
|
|
|
},
|
|
|
|
addCollectionsListener(state, listener) {
|
|
|
|
var index = state.collectionsListeners.findIndex(l => l.id === listener.id)
|
|
|
|
if (index >= 0) state.collectionsListeners.splice(index, 1, listener)
|
|
|
|
else state.collectionsListeners.push(listener)
|
|
|
|
},
|
|
|
|
removeCollectionsListener(state, listenerId) {
|
|
|
|
state.collectionsListeners = state.collectionsListeners.filter(l => l.id !== listenerId)
|
|
|
|
},
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|