2024-08-28 00:23:29 +02:00
|
|
|
import type { Client } from "@versia/client";
|
2025-05-26 11:19:15 +02:00
|
|
|
import type { RolePermission } from "@versia/client/schemas";
|
2024-12-07 22:17:22 +01:00
|
|
|
import { toast } from "vue-sonner";
|
|
|
|
|
import * as m from "~/paraglide/messages.js";
|
2024-05-17 08:25:59 +02:00
|
|
|
|
2024-08-28 00:23:29 +02:00
|
|
|
export const useCacheRefresh = (client: MaybeRef<Client | null>) => {
|
2024-05-17 08:25:59 +02:00
|
|
|
// Refresh custom emojis and instance data and me on every reload
|
2024-11-29 21:43:41 +01:00
|
|
|
watch(
|
|
|
|
|
[identity, client],
|
|
|
|
|
async () => {
|
|
|
|
|
console.info("Refreshing emoji, instance and account cache");
|
|
|
|
|
if (identity.value) {
|
|
|
|
|
toValue(client)
|
|
|
|
|
?.verifyAccountCredentials()
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (identity.value) {
|
|
|
|
|
identity.value.account = res.data;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
const code = err.response.status;
|
2024-05-17 09:19:06 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
if (code === 401) {
|
|
|
|
|
// Reset tokenData
|
|
|
|
|
identity.value = null;
|
2024-12-07 22:17:22 +01:00
|
|
|
toast.error(m.fancy_this_wasp_renew(), {
|
|
|
|
|
description: m.real_weird_deer_stop(),
|
2024-11-29 21:43:41 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-05-17 08:25:59 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
toValue(client)
|
|
|
|
|
?.getInstanceCustomEmojis()
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (identity.value) {
|
|
|
|
|
identity.value.emojis = res.data;
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-06-10 05:24:55 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
toValue(client)
|
|
|
|
|
?.getAccountRoles(identity.value.account.id)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
const roles = res.data;
|
2024-06-10 05:24:55 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
// Get all permissions and deduplicate
|
|
|
|
|
const permissions = roles
|
2025-01-29 04:39:33 +01:00
|
|
|
?.flatMap((r) => r.permissions)
|
2024-11-29 21:43:41 +01:00
|
|
|
.filter((p, i, arr) => arr.indexOf(p) === i);
|
2024-06-10 05:24:55 +02:00
|
|
|
|
2024-11-29 21:43:41 +01:00
|
|
|
if (identity.value) {
|
|
|
|
|
identity.value.permissions =
|
|
|
|
|
permissions as unknown as RolePermission[];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toValue(client)
|
|
|
|
|
?.getInstance()
|
|
|
|
|
.then((res) => {
|
2024-06-20 02:07:56 +02:00
|
|
|
if (identity.value) {
|
2024-11-29 21:43:41 +01:00
|
|
|
identity.value.instance = res.data;
|
2024-06-20 02:07:56 +02:00
|
|
|
}
|
2024-05-17 09:19:06 +02:00
|
|
|
});
|
2024-11-29 21:43:41 +01:00
|
|
|
},
|
2024-12-04 15:04:08 +01:00
|
|
|
{ flush: "sync", immediate: true },
|
2024-11-29 21:43:41 +01:00
|
|
|
);
|
2024-05-17 08:25:59 +02:00
|
|
|
};
|