98 lines
3.8 KiB
Rust
98 lines
3.8 KiB
Rust
use maud::{html, Markup, PreEscaped};
|
|
use peach_lib::sbot::SbotStatus;
|
|
use rouille::{post_input, try_or_400, Request, Response};
|
|
|
|
use crate::{
|
|
templates,
|
|
utils::{
|
|
flash::{FlashRequest, FlashResponse},
|
|
sbot, theme,
|
|
},
|
|
};
|
|
|
|
// ROUTE: /scuttlebutt/invites
|
|
|
|
/// Render the invite form template.
|
|
fn invite_form_template(
|
|
flash_name: Option<&str>,
|
|
flash_msg: Option<&str>,
|
|
invite_code: Option<&str>,
|
|
) -> Markup {
|
|
html! {
|
|
(PreEscaped("<!-- SCUTTLEBUTT INVITE FORM -->"))
|
|
div class="card center" {
|
|
form id="invites" class="center" action="/scuttlebutt/invites" method="post" {
|
|
div class="center" style="width: 80%;" {
|
|
label for="inviteUses" class="label-small font-gray" title="Number of times the invite code can be reused" { "USES" }
|
|
input type="number" id="inviteUses" name="uses" min="1" max="150" size="3" value="1";
|
|
@if let Some(code) = invite_code {
|
|
p class="card-text" style="margin-top: 1rem; user-select: all;" title="Invite code" {
|
|
(code)
|
|
}
|
|
}
|
|
}
|
|
(PreEscaped("<!-- BUTTONS -->"))
|
|
input id="createInvite" class="button button-primary center" style="margin-top: 1rem;" type="submit" title="Create a new invite code" value="Create";
|
|
a id="cancel" class="button button-secondary center" href="/scuttlebutt/peers" title="Cancel" { "Cancel" }
|
|
}
|
|
// render flash message if cookies were found in the request
|
|
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
|
|
// avoid displaying the invite code-containing flash msg
|
|
@if name != "code" {
|
|
(PreEscaped("<!-- FLASH MESSAGE -->"))
|
|
(templates::flash::build_template(name, msg))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Scuttlebutt invite 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();
|
|
|
|
// if flash_name is "code" then flash_msg will be an invite code
|
|
let invite_code = if flash_name == Some("code") {
|
|
flash_msg
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let invite_form_template = match SbotStatus::read() {
|
|
// only render the invite form template if the sbot is active
|
|
Ok(status) if status.state == Some("active".to_string()) => {
|
|
html! { (invite_form_template(flash_name, flash_msg, invite_code)) }
|
|
}
|
|
_ => {
|
|
// the sbot is not active; render a message instead of the invite form
|
|
templates::inactive::build_template("Invite creation is unavailable.")
|
|
}
|
|
};
|
|
|
|
let body =
|
|
templates::nav::build_template(invite_form_template, "Invites", 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 invite uses data and attempt to generate an invite code.
|
|
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, {
|
|
// the number of times the invite code can be used
|
|
uses: u16,
|
|
}));
|
|
|
|
let (flash_name, flash_msg) = match sbot::create_invite(data.uses) {
|
|
Ok(code) => ("flash_name=code".to_string(), format!("flash_msg={}", code)),
|
|
Err(e) => ("flash_name=error".to_string(), format!("flash_msg={}", e)),
|
|
};
|
|
|
|
Response::redirect_303("/scuttlebutt/invites").add_flash(flash_name, flash_msg)
|
|
}
|