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

@ -19,6 +19,7 @@ type wrappedGuild struct {
Id uint64 `json:"id,string"` Id uint64 `json:"id,string"`
Name string `json:"name"` Name string `json:"name"`
Icon string `json:"icon"` Icon string `json:"icon"`
PermissionLevel permission.PermissionLevel `json:"permission_level"`
} }
func GetGuilds(ctx *gin.Context) { func GetGuilds(ctx *gin.Context) {
@ -76,6 +77,7 @@ func GetGuilds(ctx *gin.Context) {
Id: g.GuildId, Id: g.GuildId,
Name: g.Name, Name: g.Name,
Icon: g.Icon, Icon: g.Icon,
PermissionLevel: permLevel,
} }
ch <- wrapped ch <- wrapped

View File

@ -38,8 +38,7 @@
} }
async function goto(guildId) { async function goto(guildId) {
const permissionLevel = await getPermissionLevel(guildId); if (guild.permission_level === 2) {
if (permissionLevel === 2) {
window.location.href = `/manage/${guildId}/settings`; window.location.href = `/manage/${guildId}/settings`;
} else { } else {
window.location.href = `/manage/${guildId}/transcripts`; window.location.href = `/manage/${guildId}/transcripts`;

View File

@ -22,3 +22,5 @@ export const notifyMessage = writable("");
export const isErrorPage = writable(false); 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 axios from "axios";
import {API_URL} from "../js/constants"; import {API_URL} from "../js/constants";
import {setDefaultHeaders} from '../includes/Auth.svelte' import {setDefaultHeaders} from '../includes/Auth.svelte'
import {permissionLevelCache} from '../js/stores';
import {get} from 'svelte/store';
export let currentRoute; export let currentRoute;
export let params = {}; export let params = {};
@ -56,6 +58,18 @@
setDefaultHeaders(); setDefaultHeaders();
async function loadPermissionLevel() { 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}`); const res = await axios.get(`${API_URL}/user/permissionlevel?guild=${guildId}`);
if (res.status !== 200 || !res.data.success) { if (res.status !== 200 || !res.data.success) {
notifyError(res.data.error); notifyError(res.data.error);
@ -63,6 +77,15 @@
} }
permissionLevel = res.data.permission_level; permissionLevel = res.data.permission_level;
permissionLevelCache.update(cache => {
cache[guildId] = {
permission_level: permissionLevel,
last_updated: new Date(),
};
return cache;
});
} }
withLoadingScreen(async () => { withLoadingScreen(async () => {

View File

@ -34,6 +34,7 @@
import Card from '../components/Card.svelte' import Card from '../components/Card.svelte'
import InviteBadge from '../components/InviteBadge.svelte' import InviteBadge from '../components/InviteBadge.svelte'
import Button from '../components/Button.svelte' import Button from '../components/Button.svelte'
import {permissionLevelCache} from "../js/stores";
setDefaultHeaders(); setDefaultHeaders();
@ -42,6 +43,16 @@
async function loadData() { async function loadData() {
const res = await axios.get(`${API_URL}/user/guilds`); const res = await axios.get(`${API_URL}/user/guilds`);
guilds = res.data; 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() { 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/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 h1:6lMp2fzZvLpIqSz/sThov2CDKaEFuJgqLwaydgDkM/k=
github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa/go.mod h1:zN6qXS5AYkt4JTHtq7mHT3eBHomUWZoZ29dZ/CPMjHQ= 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 h1:zw33VwckXiCO/k88BSdLUYB7qWfpaojfKSwemf45xxc=
github.com/TicketsBot/database v0.0.0-20230910170008-c25bc3ae5267/go.mod h1:gAtOoQKZfCkQ4AoNWQUSl51Fnlqk+odzD/hZ1e1sXyI= 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= github.com/TicketsBot/logarchiver v0.0.0-20220326162808-cdf0310f5e1c h1:OqGjFH6mbE6gd+NqI2ARJdtH3UUvhiAkD0r0fhGJK2s=