this repo has no description
1use anyhow::Result;
2use axum::{extract::State, response::IntoResponse, Form};
3use axum_extra::response::Html;
4use serde::Deserialize;
5
6use crate::{
7 errors::SupercellError,
8 storage::{denylist_remove, denylist_upsert, feed_content_purge_aturi},
9};
10
11use super::context::WebContext;
12
13#[derive(Deserialize, Default)]
14pub struct AdminForm {
15 pub action: Option<String>,
16 pub did: Option<String>,
17 pub reason: Option<String>,
18 pub aturi: Option<String>,
19 pub feed: Option<String>,
20}
21
22pub async fn handle_admin(
23 State(web_context): State<WebContext>,
24 Form(form): Form<AdminForm>,
25) -> Result<impl IntoResponse, SupercellError> {
26 if let Some(action) = form.action {
27 match action.as_str() {
28 "purge" => {
29 if let Some(aturi) = form.aturi {
30 let feed = form.feed.filter(|s| !s.is_empty());
31 tracing::debug!("purging at-uri: {:?} with feed: {:?}", aturi, feed);
32 feed_content_purge_aturi(&web_context.pool, &aturi, &feed).await?;
33 }
34 }
35 "deny" => {
36 if let Some(did) = form.did {
37 let reason = form.reason.unwrap_or("n/a".to_string());
38 denylist_upsert(&web_context.pool, &did, &reason).await?;
39 }
40 }
41 "allow" => {
42 if let Some(did) = form.did {
43 denylist_remove(&web_context.pool, &did).await?;
44 }
45 }
46 _ => {}
47 }
48 }
49
50 Ok(Html(
51 r#"
52 <!doctype html>
53 <html>
54 <head><title>Supercell Admin</title></head>
55 <body>
56 <p>Purge AT-URI</p>
57 <form action="/admin" method="post">
58 <input type="hidden" name="action" value="purge">
59 <label for="purge_aturi">AT-URI: <input type="text" id="purge_aturi" name="aturi" required="required"></label>
60 <label for="purge_feed">Feed (optional): <input type="text" id="purge_feed" name="feed"></label>
61 <input type="submit" name="submit" value="Submit">
62 </form>
63 <hr/>
64 <p>Denylist Add</p>
65 <form action="/admin" method="post">
66 <input type="hidden" name="action" value="deny">
67 <label for="deny_did">DID: <input type="text" id="deny_did" name="did" required="required"></label>
68 <label for="deny_reason">Reason (optional): <input type="text" id="deny_reason" name="reason"></label>
69 <input type="submit" name="submit" value="Submit">
70 </form>
71 <hr/>
72 <p>Denylist Remove</p>
73 <form action="/admin" method="post">
74 <input type="hidden" name="action" value="allow">
75 <label for="allow_did">DID: <input type="text" id="allow_did" name="did" required="required"></label>
76 <input type="submit" name="submit" value="Submit">
77 </form>
78 <hr/>
79 </body>
80 </html>
81 "#,
82 ))
83}