From 5223c00af43edac305adb146aa1ddec264711be9 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 10 Aug 2026 16:37:36 +0300 Subject: [PATCH] better fetchUserIfMissing & fix notifications plugin --- src/components/staff_panel/staff_panel.js | 4 +--- src/lib/push_notifications_plugin.js | 24 ++++++++++------------- src/main.js | 7 ++----- src/stores/chats.js | 2 +- src/stores/users.js | 23 +++++++++++----------- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/components/staff_panel/staff_panel.js b/src/components/staff_panel/staff_panel.js index ea2a0194f..7ceda1e41 100644 --- a/src/components/staff_panel/staff_panel.js +++ b/src/components/staff_panel/staff_panel.js @@ -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, diff --git a/src/lib/push_notifications_plugin.js b/src/lib/push_notifications_plugin.js index 5f8d65aba..ec47d702c 100644 --- a/src/lib/push_notifications_plugin.js +++ b/src/lib/push_notifications_plugin.js @@ -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() } }) } diff --git a/src/main.js b/src/main.js index ebe3feb1d..735725250 100644 --- a/src/main.js +++ b/src/main.js @@ -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) diff --git a/src/stores/chats.js b/src/stores/chats.js index d9c077eba..2a65fc25f 100644 --- a/src/stores/chats.js +++ b/src/stores/chats.js @@ -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) { diff --git a/src/stores/users.js b/src/stores/users.js index c1f85d5f8..100c090d2 100644 --- a/src/stores/users.js +++ b/src/stores/users.js @@ -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