Lines
91.67 %
Functions
90.91 %
Branches
100 %
//! Submit with the duplicate-name notice of a created or renamed account.
use forms::{ACCOUNT_EDIT, Form, FormValues};
use server::command::account_name::account_name_notice;
use server::user::User;
use uuid::Uuid;
use super::script_edit_load::load_script_edit;
use super::{Failure, Handler, Submitted, load, submit};
const NO_SOURCE: &str =
"this script was uploaded as bytecode and has no source; saving it stores the source you enter";
/// [`load`] and the notice a script stored without source carries.
///
/// # Errors
/// The load's failure.
pub async fn load_noticed(
user_id: Uuid,
form: &Form,
id: &str,
) -> Result<(FormValues, Option<String>), Failure> {
if Handler::of(form)? != Handler::ScriptEdit {
return Ok((load(user_id, form, id).await?, None));
}
let (values, sourceless) = load_script_edit(user_id, id).await?;
Ok((values, sourceless.then(|| NO_SOURCE.to_string())))
/// The name an `account-edit` submit would rename, read before it changes.
async fn stored_name(user_id: Uuid, form: &Form, values: &FormValues) -> Option<String> {
if form.name != ACCOUNT_EDIT.name {
return None;
let id = Uuid::parse_str(values.text("id")?.trim()).ok()?;
let stored = User { id: user_id }.account_snapshot(id).await.ok()??;
stored
.tags
.into_iter()
.find(|tag| tag.is_named("name"))
.map(|tag| tag.normalized().1.to_string())
fn renamed(before: Option<&str>, values: &FormValues) -> bool {
let submitted = values.text("name").map(str::trim);
matches!((before, submitted), (Some(old), Some(new)) if old != new)
/// [`submit`] and the notice a created account, or an edited one whose name
/// changed, carries when it shares its name with a sibling.
/// The submit's failure; a notice that cannot be read is logged and omitted,
/// since the submit has already committed.
pub async fn submit_noticed(
values: &FormValues,
) -> Result<(Submitted, Option<String>), Failure> {
let before = stored_name(user_id, form, values).await;
let submitted = submit(user_id, form, values).await?;
let wanted = match Handler::of(form)? {
Handler::AccountCreate => true,
Handler::AccountEdit => renamed(before.as_deref(), values),
_ => false,
};
let notice = match wanted {
true => account_name_notice(user_id, submitted.id)
.await
.unwrap_or_else(|err| {
tracing::error!("duplicate-name notice of a saved account failed: {err:?}");
None
}),
false => None,
Ok((submitted, notice))