fix: 🐛 Fix infinite refresh of account cache

This commit is contained in:
Jesse Wierzbinski 2024-11-29 21:43:41 +01:00
parent dc004601b0
commit e50a6d6f2b
No known key found for this signature in database

View file

@ -7,62 +7,66 @@ export const useCacheRefresh = (client: MaybeRef<Client | null>) => {
} }
// Refresh custom emojis and instance data and me on every reload // Refresh custom emojis and instance data and me on every reload
watchEffect(async () => { watch(
console.info("Refreshing emoji, instance and account cache"); [identity, client],
if (identity.value) { async () => {
toValue(client) console.info("Refreshing emoji, instance and account cache");
?.verifyAccountCredentials() if (identity.value) {
.then((res) => { toValue(client)
if (identity.value) { ?.verifyAccountCredentials()
identity.value.account = res.data; .then((res) => {
} if (identity.value) {
}) identity.value.account = res.data;
.catch((err) => { }
const code = err.response.status; })
.catch((err) => {
const code = err.response.status;
if (code === 401) { if (code === 401) {
// Reset tokenData // Reset tokenData
identity.value = null; identity.value = null;
useEvent("notification:new", { useEvent("notification:new", {
type: "error", type: "error",
title: "Your session has expired", title: "Your session has expired",
description: description:
"You have been logged out. Please log in again.", "You have been logged out. Please log in again.",
}); });
} }
}); });
toValue(client)
?.getInstanceCustomEmojis()
.then((res) => {
if (identity.value) {
identity.value.emojis = res.data;
}
});
toValue(client)
?.getAccountRoles(identity.value.account.id)
.then((res) => {
const roles = res.data;
// Get all permissions and deduplicate
const permissions = roles
.flatMap((r) => r.permissions)
.filter((p, i, arr) => arr.indexOf(p) === i);
if (identity.value) {
identity.value.permissions =
permissions as unknown as RolePermission[];
}
});
}
toValue(client) toValue(client)
?.getInstanceCustomEmojis() ?.getInstance()
.then((res) => { .then((res) => {
if (identity.value) { if (identity.value) {
identity.value.emojis = res.data; identity.value.instance = res.data;
} }
}); });
},
toValue(client) { flush: "sync" },
?.getAccountRoles(identity.value.account.id) );
.then((res) => {
const roles = res.data;
// Get all permissions and deduplicate
const permissions = roles
.flatMap((r) => r.permissions)
.filter((p, i, arr) => arr.indexOf(p) === i);
if (identity.value) {
identity.value.permissions =
permissions as unknown as RolePermission[];
}
});
}
toValue(client)
?.getInstance()
.then((res) => {
if (identity.value) {
identity.value.instance = res.data;
}
});
});
}; };