better fetchUserIfMissing & fix notifications plugin

This commit is contained in:
Henry Jameson 2026-08-10 16:37:36 +03:00
commit 5223c00af4
5 changed files with 25 additions and 35 deletions

View file

@ -9,9 +9,7 @@ import { useUsersStore } from 'src/stores/users.js'
const StaffPanel = { const StaffPanel = {
created() { created() {
const nicknames = useInstanceStore().staffAccounts const nicknames = useInstanceStore().staffAccounts
nicknames.forEach((name) => nicknames.forEach((name) => useUsersStore().fetchUserIfMissing({ name }))
useUsersStore().fetchUserIfMissing({ name }),
)
}, },
components: { components: {
BasicUserCard, BasicUserCard,

View file

@ -4,20 +4,16 @@ import { useMergedConfigStore } from 'src/stores/merged_config.js'
import { useUsersStore } from 'src/stores/users.js' import { useUsersStore } from 'src/stores/users.js'
export const piniaPushNotificationsPlugin = ({ store }) => { export const piniaPushNotificationsPlugin = ({ store }) => {
if ( const validActions = {
store.$id !== 'sync_config' && sync_config: new Set(['setPreference']),
store.$id !== 'instance' && interface: new Set(['setNotificationPermission', 'setLoginStatus']),
store.$id !== 'interface' user: new Set(['setCurrentUser', 'clearCurrentUser']),
) }
return
if (!validActions[store.$id]) return // Not applicable to the store
store.$onAction(({ name: actionName, args }) => { store.$onAction(({ name: actionName, args }) => {
if ( if (!validActions[store.$id].has(actionName)) return // Not applicable to action
store.$id === 'interface' &&
actionName !== 'setNotificationPermission' &&
actionName !== 'setLoginStatus'
)
return
// Initial state // Initial state
let vapidPublicKey = useInstanceStore().vapidPublicKey let vapidPublicKey = useInstanceStore().vapidPublicKey
@ -60,9 +56,9 @@ export const piniaPushNotificationsPlugin = ({ store }) => {
} }
if (permissionGranted && enabled && user) { if (permissionGranted && enabled && user) {
return window.vuex.dispatch('registerPushNotifications') return useUsersStore().registerPushNotifications()
} else { } else {
return window.vuex.dispatch('unregisterPushNotifications') return useUsersStore().unregisterPushNotifications()
} }
}) })
} }

View file

@ -20,10 +20,7 @@ import messages from './i18n/messages.js'
import createPersistedState, { import createPersistedState, {
piniaPersistPlugin, piniaPersistPlugin,
} from './lib/persisted_state.js' } from './lib/persisted_state.js'
import { import { piniaPushNotificationsPlugin } from './lib/push_notifications_plugin.js'
piniaPushNotificationsPlugin,
vuexPushNotificationsPlugin,
} from './lib/push_notifications_plugin.js'
import vuexModules from './modules/index.js' import vuexModules from './modules/index.js'
import { piniaLanguagePlugin } from 'src/lib/language.js' import { piniaLanguagePlugin } from 'src/lib/language.js'
@ -72,7 +69,7 @@ const persistedStateOptions = {
try { try {
let storageError let storageError
const plugins = [vuexPushNotificationsPlugin] const plugins = []
const pinia = createPinia() const pinia = createPinia()
pinia.use(piniaPersistPlugin()) pinia.use(piniaPersistPlugin())
pinia.use(piniaLanguagePlugin) pinia.use(piniaLanguagePlugin)

View file

@ -65,7 +65,7 @@ export const useChatsStore = defineStore('chats', {
data: result.data.map((k) => k.account).filter(Boolean), data: result.data.map((k) => k.account).filter(Boolean),
}) })
chats.forEach((updatedChat) => { result.data.forEach((updatedChat) => {
const chat = getChatById(this, updatedChat.id) const chat = getChatById(this, updatedChat.id)
if (chat) { if (chat) {

View file

@ -233,7 +233,6 @@ export const useUsersStore = defineStore('users', {
let findFunc let findFunc
let fetchFunc let fetchFunc
let map let map
let otherMap
let identifier let identifier
if (id) { if (id) {
@ -250,14 +249,19 @@ export const useUsersStore = defineStore('users', {
throw new TypeError('No identifier provided') throw new TypeError('No identifier provided')
} }
// Search in cache
const user = findFunc(identifier) const user = findFunc(identifier)
// not found => fetch
if (!user) { if (!user) {
let promise let promise
// Did we already search for this user?
if (map.has(identifier)) { if (map.has(identifier)) {
// if so, reuse the promise
promise = map.get(identifier) promise = map.get(identifier)
} else { } else {
// if not, make a new one
promise = fetchFunc(identifier) promise = fetchFunc(identifier)
} }
@ -268,6 +272,7 @@ export const useUsersStore = defineStore('users', {
if (result?.data) { if (result?.data) {
const { id, screen_name } = result.data const { id, screen_name } = result.data
// Save promise for future use
this.fetchesIds.set(id, promise) this.fetchesIds.set(id, promise)
this.fetchesNames.set(screen_name, promise) this.fetchesNames.set(screen_name, promise)
this.addNewUsers(result) this.addNewUsers(result)
@ -288,11 +293,8 @@ export const useUsersStore = defineStore('users', {
this.addNewUsers(result) this.addNewUsers(result)
return this.users.get(result.data.id) return this.users.get(result.data.id)
} catch(error) { } catch (error) {
if ( if (error.name === 'StatusCodeError' && error.statusCode === 404) {
error.name === 'StatusCodeError' &&
error.statusCode === 404
) {
console.warn(`User ${id} not found`) console.warn(`User ${id} not found`)
return null return null
} else { } else {
@ -310,12 +312,9 @@ export const useUsersStore = defineStore('users', {
this.addNewUsers(result) this.addNewUsers(result)
return this.users.get(result.data.id) return this.users.get(result.data.id)
} catch(error) { } catch (error) {
if ( if (error.name === 'StatusCodeError' && error.statusCode === 404) {
error.name === 'StatusCodeError' && console.warn(`User ${name} not found`)
error.statusCode === 404
) {
console.warn(`User ${id} not found`)
return null return null
} else { } else {
throw error throw error