From 57dc57786c5305987498db9be58c633ece6955d6 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Tue, 24 Sep 2024 22:47:26 +0100 Subject: [PATCH] refactor for server changes --- apps/home/views.py | 8 +++++--- apps/static/js/api.js | 22 +++++++++++----------- apps/static/js/home/content.js | 9 ++++----- apps/static/js/home/filters.js | 11 +++++------ apps/static/js/home/index.js | 9 ++++----- apps/static/js/home/servers.js | 3 ++- apps/static/js/home/settings.js | 2 +- apps/static/js/home/subscriptions.js | 21 ++++++++++----------- apps/static/js/home/tabs/subscriptions.js | 0 apps/static/js/table.js | 14 +++++++------- 10 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 apps/static/js/home/tabs/subscriptions.js diff --git a/apps/home/views.py b/apps/home/views.py index babbec7..dbd4613 100644 --- a/apps/home/views.py +++ b/apps/home/views.py @@ -68,10 +68,12 @@ class GuildsView(View): await self.delete_member(user, data["id"]) return - server = await Server.objects.aget_or_create( + server = await Server.objects.aupdate_or_create( id=data["id"], - name=data["name"], - icon_hash=data["icon"] + defaults={ + "name": data["name"], + "icon_hash": data["icon"] + } ) await ServerMember.objects.aupdate_or_create( diff --git a/apps/static/js/api.js b/apps/static/js/api.js index 686a7b3..dde50e9 100644 --- a/apps/static/js/api.js +++ b/apps/static/js/api.js @@ -182,27 +182,27 @@ async function getServers() { async function getSubscriptions(filters, sort) { let querystring = makeQuerystring(filters, sort); - return await ajaxRequest(`/api/r_subscriptions/${querystring}`, "GET"); + return await ajaxRequest(`/api/subscriptions/${querystring}`, "GET"); } -async function getSubscription(id) { - return await ajaxRequest(`/api/r_subscriptions/${id}/`, "GET"); +async function getSubscription(serverId, subId) { + return await ajaxRequest(`/api/${serverId}/subscriptions/${subId}/`, "GET"); } -async function newSubscription(formData) { - return await ajaxRequest("/api/r_subscriptions/", "POST", formData); +async function newSubscription(serverId, formData) { + return await ajaxRequest(`/api/servers/${serverId}/subscriptions/`, "POST", formData); } -async function deleteSubscription(id) { - return await ajaxRequest(`/api/r_subscriptions/${id}/`, "DELETE"); +async function deleteSubscription(serverId, subId) { + return await ajaxRequest(`/api/servers/${serverId}/subscriptions/${subId}/`, "DELETE"); } -async function editSubscription(id, formData) { - return await ajaxRequest(`/api/r_subscriptions/${id}/`, "PUT", formData); +async function editSubscription(serverId, subId, formData) { + return await ajaxRequest(`/api/servers/${serverId}/subscriptions/${subId}/`, "PUT", formData); } -async function getSubscriptionOptions() { - return await ajaxRequest("/api/r_subscriptions/", "OPTIONS") +async function getSubscriptionOptions(serverId) { + return await ajaxRequest(`/api/${serverId}/subscriptions/`, "OPTIONS") } // #endregion diff --git a/apps/static/js/home/content.js b/apps/static/js/home/content.js index 31c57d5..fc82637 100644 --- a/apps/static/js/home/content.js +++ b/apps/static/js/home/content.js @@ -126,7 +126,7 @@ function restartResolveChannelNamesTask() { } function startResolveChannelNamesTask() { - const guildId = getCurrentlyActiveServer().guild_id; + const guildId = selectedServer.id; channelResolveInterval = setInterval(function() { if (resolveChannelNames(guildId)) clearInterval(channelResolveInterval); @@ -190,7 +190,7 @@ async function deleteSelectedContent() { // Multi-deletion can take time, this timeout ensures the refresh is accurate setTimeout(async () => { - await loadContent(getCurrentlyActiveServer().guild_id); + await loadContent(selectedServer.id); }, 600); }, null @@ -206,7 +206,7 @@ function clearExistingContentRows() { } $("#contentTabPane").on("click", ".table-refresh-btn", async function() { - await loadContent(getCurrentlyActiveServer().guild_id); + await loadContent(selectedServer.id); }); @@ -248,8 +248,7 @@ async function loadContent(guildId) { } $(document).on("selectedServerChange", async function() { - const activeServer = getCurrentlyActiveServer(); - await loadContent(activeServer.guild_id); + await loadContent(selectedServer.id); }); // #endregion diff --git a/apps/static/js/home/filters.js b/apps/static/js/home/filters.js index c5ca0f1..7496c7c 100644 --- a/apps/static/js/home/filters.js +++ b/apps/static/js/home/filters.js @@ -129,7 +129,7 @@ $("#filterForm").on("submit", async function(event) { match = $("#filterMatch").val(); isWhitelist = $("#filterWhitelist").prop("checked"); isInsensitive = $("#filterInsensitive").prop("checked"); - guildId = getCurrentlyActiveServer().guild_id; + guildId = selectedServer.id; var filterPrimaryKey = await saveFilter(id, name, algorithm, match, isWhitelist, isInsensitive, guildId); @@ -171,7 +171,7 @@ function clearExistingFilterRows() { } $("#filtersTabPane").on("click", ".table-refresh-btn", async function() { - loadFilters(getCurrentlyActiveServer().guild_id); + loadFilters(selectedServer.id); }); async function loadFilters(guildId) { @@ -244,8 +244,7 @@ async function loadMatchingAlgorithms() { }; $(document).on("selectedServerChange", async function() { - const activeServer = getCurrentlyActiveServer(); - await loadFilters(activeServer.guild_id); + await loadFilters(selectedServer.id); }); // #region Delete Filters @@ -263,7 +262,7 @@ $("#deleteEditFilter").on("click", async function() { "danger", async () => { await deleteFilter(filterId); - await loadFilters(getCurrentlyActiveServer().guild_id); + await loadFilters(selectedServer.id); showToast( "danger", @@ -300,7 +299,7 @@ async function deleteSelectedFilters() { // Multi-deletion can take time, this timeout ensures the refresh is accurate setTimeout(async () => { - await loadFilters(getCurrentlyActiveServer().guild_id); + await loadFilters(selectedServer.id); }, 600); }, null diff --git a/apps/static/js/home/index.js b/apps/static/js/home/index.js index 77d84e0..ba0b836 100644 --- a/apps/static/js/home/index.js +++ b/apps/static/js/home/index.js @@ -1,10 +1,9 @@ $(document).ready(async function() { + // await initSubscriptionTable(); + // await initFiltersTable(); + // await initContentTable(); + await loadServers(); - - await initSubscriptionTable(); - await initFiltersTable(); - await initContentTable(); - $("#subscriptionsTab").click(); }); diff --git a/apps/static/js/home/servers.js b/apps/static/js/home/servers.js index c28cb25..a139c88 100644 --- a/apps/static/js/home/servers.js +++ b/apps/static/js/home/servers.js @@ -82,7 +82,8 @@ function removeSelectButton(id) { $("#backToSelectServer").on("click", function() { $("#noSelectedServer").show(); $("#selectedServerContainer").hide(); - selectServer = null; + $("#serverList .server-item > .server-item-selector.active").removeClass("active"); + selectedServer = null; }); // #endregion diff --git a/apps/static/js/home/settings.js b/apps/static/js/home/settings.js index d8f5612..d87ef39 100644 --- a/apps/static/js/home/settings.js +++ b/apps/static/js/home/settings.js @@ -4,7 +4,7 @@ $("#serverSettingsBtn").on("click", async function() { }); async function showServerSettingsModal() { - const server = getCurrentlyActiveServer(); + const server = selectedServer; var guildSettings; try { guildSettings = (await getGuildSettings(server.guild_id)).results[0] } diff --git a/apps/static/js/home/subscriptions.js b/apps/static/js/home/subscriptions.js index 9d3def6..8265ab1 100644 --- a/apps/static/js/home/subscriptions.js +++ b/apps/static/js/home/subscriptions.js @@ -5,8 +5,8 @@ var subTable = null; // Create subscription table async function initSubscriptionTable() { - subOptions = await getSubscriptionOptions(); - await initTable("#subscriptionsTabPane", "subTable", loadSubscriptions, showEditSubModal, deleteSelectedSubscriptions, subOptions); + // subOptions = await getSubscriptionOptions(); + await initTable("#subscriptionsTabPane", "subTable", loadSubscriptions, showEditSubModal, deleteSelectedSubscriptions); subTable = $("#subTable").DataTable({ info: false, @@ -301,7 +301,7 @@ $("#subForm").on("submit", async function(event) { event.preventDefault(); let subId = $("#subId").val(); - let guildId = getCurrentlyActiveServer().guild_id; + let guildId = selectedServer.id; // TODO: move this into a function, so I can fix the active toggle switches which are broken due to this change @@ -425,7 +425,7 @@ function clearExistingSubRows() { } $("#subscriptionsTabPane").on("click", ".table-refresh-btn", async function() { - loadSubscriptions(getCurrentlyActiveServer().guild_id); + loadSubscriptions(selectedServer.id); }); async function loadSubscriptions(guildId) { @@ -465,10 +465,9 @@ async function loadSubscriptions(guildId) { // #region Server Change Event Handler $(document).on("selectedServerChange", async function() { - let server = getCurrentlyActiveServer(); - guildId = server.guild_id; + let guildId = selectedServer.id; - await updateDefaultSubEmbedColour(); + // await updateDefaultSubEmbedColour(); await loadSubscriptions(guildId); await loadChannelOptions(guildId); @@ -479,7 +478,7 @@ $(document).on("selectedServerChange", async function() { async function updateDefaultSubEmbedColour(settings=null) { if (!settings){ - settings = (await getGuildSettings(guildId)).results[0] + settings = (await getGuildSettings(selectedServer.id)).results[0] } $("#subEmbedColour .colour-reset").attr("data-defaultcolour", "#" + settings.default_embed_colour); } @@ -503,7 +502,7 @@ $("#deleteEditSub").on("click", async function() { "danger", async () => { await deleteSubscription(subId); - await loadSubscriptions(getCurrentlyActiveServer().guild_id); + await loadSubscriptions(selectedServer.id); showToast( "danger", @@ -540,7 +539,7 @@ async function deleteSelectedSubscriptions() { // Multi-deletion can take time, this timeout ensures the refresh is accurate setTimeout(async () => { - await loadSubscriptions(getCurrentlyActiveServer().guild_id); + await loadSubscriptions(selectedServer.id); }, 600); }, null @@ -582,7 +581,7 @@ async function loadChannelOptions(guildId) { // Also check that the user hasn't changed the currently active guild, otherwise // the alert will show under the wrong server. - if (getCurrentlyActiveServer().guild_id === guildId) + if (selectedServer.id === guildId) $("#serverJoinAlert").show(); // Warning icon on sidebar server select diff --git a/apps/static/js/home/tabs/subscriptions.js b/apps/static/js/home/tabs/subscriptions.js new file mode 100644 index 0000000..e69de29 diff --git a/apps/static/js/table.js b/apps/static/js/table.js index 07d3f62..4d08bbc 100644 --- a/apps/static/js/table.js +++ b/apps/static/js/table.js @@ -81,7 +81,7 @@ function createSearchRow(containingSelector, searchId, sortDropdownId, filterDro } // Show the sort dropdown - if (options.sort && options.actions.GET) { + if (options && options.sort && options.actions.GET) { $(`${containingSelector} .table-search-row .table-search-buttons`).append(`