From a3279a4c916d0d5dba2252870545cd4c18888d1d Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Sat, 12 Oct 2024 20:00:50 +0100 Subject: [PATCH] completed #67 - "Cache" Loaded Channels --- apps/home/static/home/js/servers.js | 85 ++++++++++++++------------- apps/home/static/home/js/tabs/subs.js | 4 +- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/apps/home/static/home/js/servers.js b/apps/home/static/home/js/servers.js index d6e57f4..29ae384 100644 --- a/apps/home/static/home/js/servers.js +++ b/apps/home/static/home/js/servers.js @@ -50,14 +50,50 @@ function removeFromLoadedServers(id) { // region Loaded Channels var _loadedChannels = {}; -function loadedChannels(serverId) { +async function loadedChannels(serverId) { if (!(serverId in _loadedChannels)) { - throw new Error(`channels not loaded for server: ${serverId}`); + await fetchChannels(serverId); } return _loadedChannels[serverId] } +$(document).on("selectedServerChange", async function() { + // Try load channels to determine if bot has permissions + loadedChannels(selectedServer.id); +}); + +const fetchChannels = async serverId => { + $(".sidebar .sidebar-item").prop("disabled", true); + + try { + $(`.sidebar .sidebar-item[data-id="${serverId}"]`).removeClass("is-not-operational"); + channels = await ajaxRequest(`/generate-channels?guild=${serverId}`, "GET"); + _loadedChannels[serverId] = channels; + } + catch (error) { + logError(error); + + switch (error?.status) { + case 429: + rateLimitedLoadingChannels(error.responseJSON.retry_after); + break; + + case 403: + notAuthorisedLoadingChannels(serverId); + break; + + default: + alert("unknown error loading channels"); + break; + } + + } + finally { + $(".sidebar .sidebar-item").prop("disabled", false); + } +} + const rateLimitedLoadingChannels = retryAfterSeconds => { createModal({ title: "Failed to Fetch Server Channels", @@ -75,18 +111,18 @@ const rateLimitedLoadingChannels = retryAfterSeconds => { }); } -const notAuthorisedLoadingChannels = () => { +const notAuthorisedLoadingChannels = serverId => { // Mark the sidebar item as non-operational $(`.sidebar .sidebar-item[data-id="${serverId}"]`).addClass("is-not-operational"); const inviteBotToServer = () => { window.open( `https://discord.com/oauth2/authorize - ?client_id=${discordClientId} - &permissions=2147534848 - &scope=bot+applications.commands - &guild_id=${serverId} - &disable_guild_select=true`, +?client_id=${discordClientId} +&permissions=2147534848 +&scope=bot+applications.commands +&guild_id=${serverId} +&disable_guild_select=true`, "_blank" ); } @@ -115,39 +151,6 @@ const notAuthorisedLoadingChannels = () => { }); } -$(document).on("selectedServerChange", async function() { - serverId = selectedServer.id; // take note incase 'selectedServer' changes - - $(".sidebar .sidebar-item").prop("disabled", true); - - try { - $(`.sidebar .sidebar-item[data-id="${serverId}"]`).removeClass("is-not-operational"); - channels = await ajaxRequest(`/generate-channels?guild=${serverId}`, "GET"); - _loadedChannels[serverId] = channels; - } - catch (error) { - logError(error); - - switch (error?.status) { - case 429: - rateLimitedLoadingChannels(error.responseJSON.retry_after); - break; - - case 403: - notAuthorisedLoadingChannels(); - break; - - default: - alert("unknown error loading channels"); - break; - } - - } - finally { - $(".sidebar .sidebar-item").prop("disabled", false); - } -}); - // region UI Buttons diff --git a/apps/home/static/home/js/tabs/subs.js b/apps/home/static/home/js/tabs/subs.js index 2bdf984..62f0e90 100644 --- a/apps/home/static/home/js/tabs/subs.js +++ b/apps/home/static/home/js/tabs/subs.js @@ -270,7 +270,7 @@ async function loadSubModalOptions($input, url) { } // Channel options aren't loaded from an API, like other options. -function loadChannelOptions() { +async function loadChannelOptions() { $input = $(subModalId).find('[data-field="channels"]'); $input.val("").change(); @@ -282,7 +282,7 @@ function loadChannelOptions() { } }); - const data = loadedChannels(selectedServer.id); + const data = await loadedChannels(selectedServer.id); data.forEach(item => { $input.append($( "