1
//! Submit with the duplicate-name notice of a created or renamed account.
2

            
3
use forms::{ACCOUNT_EDIT, Form, FormValues};
4
use server::command::account_name::account_name_notice;
5
use server::user::User;
6
use uuid::Uuid;
7

            
8
use super::script_edit_load::load_script_edit;
9
use super::{Failure, Handler, Submitted, load, submit};
10

            
11
const NO_SOURCE: &str =
12
    "this script was uploaded as bytecode and has no source; saving it stores the source you enter";
13

            
14
/// [`load`] and the notice a script stored without source carries.
15
///
16
/// # Errors
17
/// The load's failure.
18
22685
pub async fn load_noticed(
19
22685
    user_id: Uuid,
20
22685
    form: &Form,
21
22685
    id: &str,
22
22685
) -> Result<(FormValues, Option<String>), Failure> {
23
22685
    if Handler::of(form)? != Handler::ScriptEdit {
24
19003
        return Ok((load(user_id, form, id).await?, None));
25
3682
    }
26
3682
    let (values, sourceless) = load_script_edit(user_id, id).await?;
27
3202
    Ok((values, sourceless.then(|| NO_SOURCE.to_string())))
28
22685
}
29

            
30
/// The name an `account-edit` submit would rename, read before it changes.
31
40335
async fn stored_name(user_id: Uuid, form: &Form, values: &FormValues) -> Option<String> {
32
40335
    if form.name != ACCOUNT_EDIT.name {
33
38891
        return None;
34
1444
    }
35
1444
    let id = Uuid::parse_str(values.text("id")?.trim()).ok()?;
36
1444
    let stored = User { id: user_id }.account_snapshot(id).await.ok()??;
37
1444
    stored
38
1444
        .tags
39
1444
        .into_iter()
40
1768
        .find(|tag| tag.is_named("name"))
41
1444
        .map(|tag| tag.normalized().1.to_string())
42
40335
}
43

            
44
964
fn renamed(before: Option<&str>, values: &FormValues) -> bool {
45
964
    let submitted = values.text("name").map(str::trim);
46
964
    matches!((before, submitted), (Some(old), Some(new)) if old != new)
47
964
}
48

            
49
/// [`submit`] and the notice a created account, or an edited one whose name
50
/// changed, carries when it shares its name with a sibling.
51
///
52
/// # Errors
53
/// The submit's failure; a notice that cannot be read is logged and omitted,
54
/// since the submit has already committed.
55
40335
pub async fn submit_noticed(
56
40335
    user_id: Uuid,
57
40335
    form: &Form,
58
40335
    values: &FormValues,
59
40335
) -> Result<(Submitted, Option<String>), Failure> {
60
40335
    let before = stored_name(user_id, form, values).await;
61
40335
    let submitted = submit(user_id, form, values).await?;
62
31691
    let wanted = match Handler::of(form)? {
63
1603
        Handler::AccountCreate => true,
64
964
        Handler::AccountEdit => renamed(before.as_deref(), values),
65
29124
        _ => false,
66
    };
67
31691
    let notice = match wanted {
68
2405
        true => account_name_notice(user_id, submitted.id)
69
2405
            .await
70
            .unwrap_or_else(|err| {
71
                tracing::error!("duplicate-name notice of a saved account failed: {err:?}");
72
                None
73
            }),
74
29286
        false => None,
75
    };
76
31691
    Ok((submitted, notice))
77
40335
}