Reduce permission level requests

This commit is contained in:
rxdn 2023-09-10 19:05:13 +01:00
parent dbc512237a
commit 2cfa5e9750
6 changed files with 46 additions and 11 deletions

View File

@ -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

View File

@ -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`;

View File

@ -21,4 +21,6 @@ export const notifyTitle = writable("");
export const notifyMessage = writable("");
export const isErrorPage = writable(false);
export const errorMessage = writable("");
export const errorMessage = writable("");
export const permissionLevelCache = writable({});

View File

@ -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 () => {

View File

@ -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() {

2
go.sum
View File

@ -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=