From ad17fb64f11c849cd533455fc6897f7104c1bae8 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 15 Mar 2024 00:02:24 -0400 Subject: [PATCH] use api for whitelist admin page --- relay/frontend/page/admin-whitelist.haml | 19 +++++---- relay/frontend/static/instance.js | 2 +- relay/frontend/static/whitelist.js | 49 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 relay/frontend/static/whitelist.js diff --git a/relay/frontend/page/admin-whitelist.haml b/relay/frontend/page/admin-whitelist.haml index e0dffd6..77fad7a 100644 --- a/relay/frontend/page/admin-whitelist.haml +++ b/relay/frontend/page/admin-whitelist.haml @@ -1,19 +1,22 @@ -extends "base.haml" -set page="Whitelist" + +-block head + %script(type="application/javascript" src="/static/whitelist.js" nonce="{{view.request['hash']}}") + -block content %details.section %summary << Add Domain - %form(action="/admin/whitelist" method="POST") - #add-item - %label(for="domain") << Domain - %input(type="domain" id="domain" name="domain" placeholder="Domain") + #add-item + %label(for="new-domain") << Domain + %input(type="domain" id="new-domain" placeholder="Domain") - %input(type="submit" value="Add Domain") + %input(type="button" value="Add Domain", onclick="add_whitelist()") %fieldset.data-table.section %legend << Whitelist - %table + %table#whitelist %thead %tr %td.domain << Domain @@ -22,7 +25,7 @@ %tbody -for item in whitelist - %tr + %tr(id="{{item.domain}}") %td.domain =item.domain @@ -30,4 +33,4 @@ =item.created.strftime("%Y-%m-%d") %td.remove - %a(href="/admin/whitelist/delete/{{item.domain}}" title="Remove whitlisted domain") << ✖ + %a(href="#" onclick="del_whitelist('{{item.domain}}')" title="Remove whitlisted domain") << ✖ diff --git a/relay/frontend/static/instance.js b/relay/frontend/static/instance.js index e5f422c..27351eb 100644 --- a/relay/frontend/static/instance.js +++ b/relay/frontend/static/instance.js @@ -36,7 +36,7 @@ async function add_instance() { } if (values.actor === "") { - alert("Domain, actor, and inbox are required"); + alert("Actor is required"); return; } diff --git a/relay/frontend/static/whitelist.js b/relay/frontend/static/whitelist.js new file mode 100644 index 0000000..86f02fb --- /dev/null +++ b/relay/frontend/static/whitelist.js @@ -0,0 +1,49 @@ +async function add_whitelist() { + var domain_elem = document.getElementById("new-domain"); + var domain = domain_elem.value.trim(); + + if (domain === "") { + alert("Domain is required"); + return; + } + + try { + var item = await client.request("POST", "v1/whitelist", {"domain": domain}); + + } catch (err) { + alert(err); + return + } + + var table = document.getElementById("whitelist"); + var row = table.insertRow(-1); + row.id = item.domain; + + var domain = row.insertCell(0); + domain.className = "domain"; + domain.innerHTML = item.domain; + + var date = row.insertCell(1); + date.className = "date"; + date.innerHTML = get_date_string(item.created); + + var remove = row.insertCell(2); + remove.className = "remove"; + remove.innerHTML = ``; + + domain_elem.value = null; + document.querySelector("details.section").open = false; +} + + +async function del_whitelist(domain) { + try { + await client.request("DELETE", "v1/whitelist", {"domain": domain}); + + } catch (error) { + alert(error); + return; + } + + document.getElementById(domain).remove(); +}