better fetchUserIfMissing & fix notifications plugin
This commit is contained in:
parent
92ef3b1cd1
commit
5223c00af4
5 changed files with 25 additions and 35 deletions
|
|
@ -9,9 +9,7 @@ import { useUsersStore } from 'src/stores/users.js'
|
|||
const StaffPanel = {
|
||||
created() {
|
||||
const nicknames = useInstanceStore().staffAccounts
|
||||
nicknames.forEach((name) =>
|
||||
useUsersStore().fetchUserIfMissing({ name }),
|
||||
)
|
||||
nicknames.forEach((name) => useUsersStore().fetchUserIfMissing({ name }))
|
||||
},
|
||||
components: {
|
||||
BasicUserCard,
|
||||
|
|
|
|||
|
|
@ -4,20 +4,16 @@ import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
|||
import { useUsersStore } from 'src/stores/users.js'
|
||||
|
||||
export const piniaPushNotificationsPlugin = ({ store }) => {
|
||||
if (
|
||||
store.$id !== 'sync_config' &&
|
||||
store.$id !== 'instance' &&
|
||||
store.$id !== 'interface'
|
||||
)
|
||||
return
|
||||
const validActions = {
|
||||
sync_config: new Set(['setPreference']),
|
||||
interface: new Set(['setNotificationPermission', 'setLoginStatus']),
|
||||
user: new Set(['setCurrentUser', 'clearCurrentUser']),
|
||||
}
|
||||
|
||||
if (!validActions[store.$id]) return // Not applicable to the store
|
||||
|
||||
store.$onAction(({ name: actionName, args }) => {
|
||||
if (
|
||||
store.$id === 'interface' &&
|
||||
actionName !== 'setNotificationPermission' &&
|
||||
actionName !== 'setLoginStatus'
|
||||
)
|
||||
return
|
||||
if (!validActions[store.$id].has(actionName)) return // Not applicable to action
|
||||
|
||||
// Initial state
|
||||
let vapidPublicKey = useInstanceStore().vapidPublicKey
|
||||
|
|
@ -60,9 +56,9 @@ export const piniaPushNotificationsPlugin = ({ store }) => {
|
|||
}
|
||||
|
||||
if (permissionGranted && enabled && user) {
|
||||
return window.vuex.dispatch('registerPushNotifications')
|
||||
return useUsersStore().registerPushNotifications()
|
||||
} else {
|
||||
return window.vuex.dispatch('unregisterPushNotifications')
|
||||
return useUsersStore().unregisterPushNotifications()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,7 @@ import messages from './i18n/messages.js'
|
|||
import createPersistedState, {
|
||||
piniaPersistPlugin,
|
||||
} from './lib/persisted_state.js'
|
||||
import {
|
||||
piniaPushNotificationsPlugin,
|
||||
vuexPushNotificationsPlugin,
|
||||
} from './lib/push_notifications_plugin.js'
|
||||
import { piniaPushNotificationsPlugin } from './lib/push_notifications_plugin.js'
|
||||
import vuexModules from './modules/index.js'
|
||||
|
||||
import { piniaLanguagePlugin } from 'src/lib/language.js'
|
||||
|
|
@ -72,7 +69,7 @@ const persistedStateOptions = {
|
|||
|
||||
try {
|
||||
let storageError
|
||||
const plugins = [vuexPushNotificationsPlugin]
|
||||
const plugins = []
|
||||
const pinia = createPinia()
|
||||
pinia.use(piniaPersistPlugin())
|
||||
pinia.use(piniaLanguagePlugin)
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ export const useChatsStore = defineStore('chats', {
|
|||
data: result.data.map((k) => k.account).filter(Boolean),
|
||||
})
|
||||
|
||||
chats.forEach((updatedChat) => {
|
||||
result.data.forEach((updatedChat) => {
|
||||
const chat = getChatById(this, updatedChat.id)
|
||||
|
||||
if (chat) {
|
||||
|
|
|
|||
|
|
@ -233,7 +233,6 @@ export const useUsersStore = defineStore('users', {
|
|||
let findFunc
|
||||
let fetchFunc
|
||||
let map
|
||||
let otherMap
|
||||
let identifier
|
||||
|
||||
if (id) {
|
||||
|
|
@ -250,14 +249,19 @@ export const useUsersStore = defineStore('users', {
|
|||
throw new TypeError('No identifier provided')
|
||||
}
|
||||
|
||||
// Search in cache
|
||||
const user = findFunc(identifier)
|
||||
|
||||
// not found => fetch
|
||||
if (!user) {
|
||||
let promise
|
||||
|
||||
// Did we already search for this user?
|
||||
if (map.has(identifier)) {
|
||||
// if so, reuse the promise
|
||||
promise = map.get(identifier)
|
||||
} else {
|
||||
// if not, make a new one
|
||||
promise = fetchFunc(identifier)
|
||||
}
|
||||
|
||||
|
|
@ -268,6 +272,7 @@ export const useUsersStore = defineStore('users', {
|
|||
if (result?.data) {
|
||||
const { id, screen_name } = result.data
|
||||
|
||||
// Save promise for future use
|
||||
this.fetchesIds.set(id, promise)
|
||||
this.fetchesNames.set(screen_name, promise)
|
||||
this.addNewUsers(result)
|
||||
|
|
@ -288,11 +293,8 @@ export const useUsersStore = defineStore('users', {
|
|||
|
||||
this.addNewUsers(result)
|
||||
return this.users.get(result.data.id)
|
||||
} catch(error) {
|
||||
if (
|
||||
error.name === 'StatusCodeError' &&
|
||||
error.statusCode === 404
|
||||
) {
|
||||
} catch (error) {
|
||||
if (error.name === 'StatusCodeError' && error.statusCode === 404) {
|
||||
console.warn(`User ${id} not found`)
|
||||
return null
|
||||
} else {
|
||||
|
|
@ -310,12 +312,9 @@ export const useUsersStore = defineStore('users', {
|
|||
this.addNewUsers(result)
|
||||
|
||||
return this.users.get(result.data.id)
|
||||
} catch(error) {
|
||||
if (
|
||||
error.name === 'StatusCodeError' &&
|
||||
error.statusCode === 404
|
||||
) {
|
||||
console.warn(`User ${id} not found`)
|
||||
} catch (error) {
|
||||
if (error.name === 'StatusCodeError' && error.statusCode === 404) {
|
||||
console.warn(`User ${name} not found`)
|
||||
return null
|
||||
} else {
|
||||
throw error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue