diff --git a/app/http/endpoints/api/guilds.go b/app/http/endpoints/api/guilds.go index 9403789..62828eb 100644 --- a/app/http/endpoints/api/guilds.go +++ b/app/http/endpoints/api/guilds.go @@ -16,9 +16,10 @@ import ( ) type wrappedGuild struct { - Id uint64 `json:"id,string"` - Name string `json:"name"` - Icon string `json:"icon"` + Id uint64 `json:"id,string"` + Name string `json:"name"` + Icon string `json:"icon"` + PermissionLevel permission.PermissionLevel `json:"permission_level"` } func GetGuilds(ctx *gin.Context) { @@ -73,9 +74,10 @@ func GetGuilds(ctx *gin.Context) { if permLevel >= permission.Support { wrapped := wrappedGuild{ - Id: g.GuildId, - Name: g.Name, - Icon: g.Icon, + Id: g.GuildId, + Name: g.Name, + Icon: g.Icon, + PermissionLevel: permLevel, } ch <- wrapped diff --git a/frontend/src/components/Guild.svelte b/frontend/src/components/Guild.svelte index 208666d..9a38aa4 100644 --- a/frontend/src/components/Guild.svelte +++ b/frontend/src/components/Guild.svelte @@ -38,8 +38,7 @@ } async function goto(guildId) { - const permissionLevel = await getPermissionLevel(guildId); - if (permissionLevel === 2) { + if (guild.permission_level === 2) { window.location.href = `/manage/${guildId}/settings`; } else { window.location.href = `/manage/${guildId}/transcripts`; diff --git a/frontend/src/js/stores.js b/frontend/src/js/stores.js index 3c58d70..b944f2e 100644 --- a/frontend/src/js/stores.js +++ b/frontend/src/js/stores.js @@ -21,4 +21,6 @@ export const notifyTitle = writable(""); export const notifyMessage = writable(""); export const isErrorPage = writable(false); -export const errorMessage = writable(""); \ No newline at end of file +export const errorMessage = writable(""); + +export const permissionLevelCache = writable({}); \ No newline at end of file diff --git a/frontend/src/layouts/ManageLayout.svelte b/frontend/src/layouts/ManageLayout.svelte index a6ddb98..be4d335 100644 --- a/frontend/src/layouts/ManageLayout.svelte +++ b/frontend/src/layouts/ManageLayout.svelte @@ -46,6 +46,8 @@ import axios from "axios"; import {API_URL} from "../js/constants"; import {setDefaultHeaders} from '../includes/Auth.svelte' + import {permissionLevelCache} from '../js/stores'; + import {get} from 'svelte/store'; export let currentRoute; export let params = {}; @@ -56,6 +58,18 @@ setDefaultHeaders(); async function loadPermissionLevel() { + const cache = get(permissionLevelCache); + if (cache && cache[guildId]) { + const data = cache[guildId]; + if (data.last_updated) { + const date = new Date(data.last_updated.getTime() + 60000); + if (date > new Date()) { + permissionLevel = data.permission_level; + return; + } + } + } + const res = await axios.get(`${API_URL}/user/permissionlevel?guild=${guildId}`); if (res.status !== 200 || !res.data.success) { notifyError(res.data.error); @@ -63,6 +77,15 @@ } permissionLevel = res.data.permission_level; + + permissionLevelCache.update(cache => { + cache[guildId] = { + permission_level: permissionLevel, + last_updated: new Date(), + }; + + return cache; + }); } withLoadingScreen(async () => { diff --git a/frontend/src/views/Index.svelte b/frontend/src/views/Index.svelte index 0715c8f..36a50c9 100644 --- a/frontend/src/views/Index.svelte +++ b/frontend/src/views/Index.svelte @@ -34,6 +34,7 @@ import Card from '../components/Card.svelte' import InviteBadge from '../components/InviteBadge.svelte' import Button from '../components/Button.svelte' + import {permissionLevelCache} from "../js/stores"; setDefaultHeaders(); @@ -42,6 +43,16 @@ async function loadData() { const res = await axios.get(`${API_URL}/user/guilds`); guilds = res.data; + + permissionLevelCache.update(cache => { + for (const guild of guilds) { + cache[guild.id] = { + permission_level: guild.permission_level, + last_updated: new Date(), + }; + } + return cache; + }) } async function refreshGuilds() { diff --git a/go.sum b/go.sum index dfc2a9f..2ac153d 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,6 @@ github.com/TicketsBot/archiverclient v0.0.0-20220326163414-558fd52746dc h1:n15W8 github.com/TicketsBot/archiverclient v0.0.0-20220326163414-558fd52746dc/go.mod h1:2KcfHS0JnSsgcxZBs3NyWMXNQzEo67mBSGOyzHPWOCc= github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa h1:6lMp2fzZvLpIqSz/sThov2CDKaEFuJgqLwaydgDkM/k= github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa/go.mod h1:zN6qXS5AYkt4JTHtq7mHT3eBHomUWZoZ29dZ/CPMjHQ= -github.com/TicketsBot/database v0.0.0-20230821182620-0130c7c2c5ad h1:tg/KYNLExb8MJbrxxlVgSjIzSEOibduE3ZV1S0uz+7Y= -github.com/TicketsBot/database v0.0.0-20230821182620-0130c7c2c5ad/go.mod h1:gAtOoQKZfCkQ4AoNWQUSl51Fnlqk+odzD/hZ1e1sXyI= github.com/TicketsBot/database v0.0.0-20230910170008-c25bc3ae5267 h1:zw33VwckXiCO/k88BSdLUYB7qWfpaojfKSwemf45xxc= github.com/TicketsBot/database v0.0.0-20230910170008-c25bc3ae5267/go.mod h1:gAtOoQKZfCkQ4AoNWQUSl51Fnlqk+odzD/hZ1e1sXyI= github.com/TicketsBot/logarchiver v0.0.0-20220326162808-cdf0310f5e1c h1:OqGjFH6mbE6gd+NqI2ARJdtH3UUvhiAkD0r0fhGJK2s=