peach-workspace/peach-web/src/routes/scuttlebutt/search.rs

70 lines
2.7 KiB
Rust

use maud::{html, PreEscaped};
use rouille::{post_input, try_or_400, Request, Response};
use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
sbot, theme,
},
};
// ROUTE: /scuttlebutt/search
/// Scuttlebutt peer search template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
// check for flash cookies; will be (None, None) if no flash cookies are found
let (flash_name, flash_msg) = request.retrieve_flash();
let search_template = html! {
(PreEscaped("<!-- PEER SEARCH FORM -->"))
div class="card center" {
form id="sbotConfig" class="center" action="/scuttlebutt/search" method="post" {
div class="center" style="display: flex; flex-direction: column; margin-bottom: 2rem;" title="Public key (ID) of a peer" {
label for="publicKey" class="label-small font-gray" { "PUBLIC KEY" }
input type="text" id="publicKey" name="public_key" placeholder="@xYz...=.ed25519" autofocus;
}
(PreEscaped("<!-- BUTTONS -->"))
input id="search" class="button button-primary center" type="submit" title="Search for peer" value="Search";
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}
};
let body =
templates::nav::build_template(search_template, "Search", Some("/scuttlebutt/peers"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Parse the public key, verify that it's valid and then redirect to the
/// profile of the given key.
///
/// If the public key is invalid, set an error flash message and redirect.
pub fn handle_form(request: &Request) -> Response {
// query the request body for form data
// return a 400 error if the admin_id field is missing
let data = try_or_400!(post_input!(request, {
public_key: String,
}));
match sbot::validate_public_key(&data.public_key) {
Ok(_) => {
let url = format!("/scuttlebutt/profile/{}", &data.public_key);
Response::redirect_303(url)
}
Err(err) => {
let (flash_name, flash_msg) =
("flash_name=error".to_string(), format!("flash_msg={}", err));
Response::redirect_303("/scuttlebutt/search").add_flash(flash_name, flash_msg)
}
}
}