Skip to main content

server/command/
account_name.rs

1//! The duplicate-name notice of account create and rename: a warning, never
2//! a refusal.
3
4use crate::command::connection;
5use sqlx::types::Uuid;
6use supp_macro::command;
7
8use super::{CmdError, CmdResult};
9use crate::user::User;
10
11/// The one text every client shows when an account shares its name with a
12/// sibling.
13#[must_use]
14pub fn duplicate_name_notice(name: &str) -> String {
15    format!("Another account named '{name}' already exists under the same parent")
16}
17
18command! {
19    AccountNameNotice {
20        #[required]
21        user_id: Uuid,
22        #[required]
23        account_id: Uuid,
24    } => {
25        let user = User { id: user_id };
26        let mut conn = connection(&user).await?;
27        let row = sqlx::query_file!("sql/select/accounts/name_twins.sql", account_id)
28            .fetch_optional(&mut *conn)
29            .await?;
30        Ok(row
31            .filter(|row| row.twins.unwrap_or_default() > 0)
32            .map(|row| CmdResult::String(duplicate_name_notice(&row.account_name))))
33    }
34}
35
36/// The notice for `account_id` after a create or rename, if it has a twin.
37///
38/// # Errors
39/// The command's database error.
40pub async fn account_name_notice(
41    user_id: Uuid,
42    account_id: Uuid,
43) -> Result<Option<String>, CmdError> {
44    match AccountNameNotice::new()
45        .user_id(user_id)
46        .account_id(account_id)
47        .run()
48        .await?
49    {
50        Some(CmdResult::String(notice)) => Ok(Some(notice)),
51        _ => Ok(None),
52    }
53}