pass_manager/src/state/get_new_name.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

use crate::{errors::NoUserInfo, utils::validate_field, MainDialogue};
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Validates a new account
#[inline]
async fn check_new_account_name(
bot: &Throttle<Bot>,
msg: &Message,
db: &DatabaseConnection,
name: &str,
) -> crate::Result<Option<Message>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if Account::exists(user_id, name, db).await? {
let msg = bot
.send_message(msg.chat.id, "Account already exists")
.await?;
return Ok(Some(msg));
}
if !validate_field(name) {
let msg = bot
.send_message(msg.chat.id, "Invalid account name. Try again")
.await?;
return Ok(Some(msg));
}
Ok(None)
}
/// Handles GetNewName state
pub async fn get_new_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, db, name| Box::pin(check_new_account_name(bot, msg, db, name)),
"Couldn't get the text of the message. Send the name of the new account again",
next,
)
.await
}