From a0920ba3d3bc7a06defc1222df037c497f127b03 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Tue, 30 May 2023 14:21:04 +0300 Subject: [PATCH] Updated generic to get the text to send when there's no text --- src/handlers/state/generic.rs | 7 +-- src/handlers/state/get_account_name.rs | 72 ++++++++++++++++++++------ src/handlers/state/get_login.rs | 1 + src/handlers/state/get_master_pass.rs | 3 +- src/handlers/state/get_password.rs | 1 + 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/handlers/state/generic.rs b/src/handlers/state/generic.rs index a8d3277..8940809 100644 --- a/src/handlers/state/generic.rs +++ b/src/handlers/state/generic.rs @@ -14,6 +14,7 @@ pub async fn generic( db: DatabaseConnection, dialogue: crate::handlers::MainDialogue, check: F, + no_text_message: impl Into, next: super::PackagedHandler, ) -> crate::Result<()> where @@ -30,11 +31,7 @@ where let text = match msg.text() { Some(text) => text.trim_end(), None => { - bot.send_message( - msg.chat.id, - "Couldn't get the text of the message. Send the message again", - ) - .await?; + bot.send_message(msg.chat.id, no_text_message).await?; return Ok(()); } }; diff --git a/src/handlers/state/get_account_name.rs b/src/handlers/state/get_account_name.rs index 3735802..360fafb 100644 --- a/src/handlers/state/get_account_name.rs +++ b/src/handlers/state/get_account_name.rs @@ -1,7 +1,11 @@ use crate::{ entity::prelude::Account, - errors::NoUserInfo, - handlers::{markups::account_markup, utils::validate_field, MainDialogue}, + errors::{HandlerUsed, NoUserInfo}, + handlers::{ + markups::{account_markup, deletion_markup}, + utils::{delete_optional, validate_field}, + MainDialogue, + }, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; @@ -15,14 +19,14 @@ pub enum NameCheckKind { /// Validates the account name #[inline] -pub async fn check_name( +async fn check_name( bot: &Throttle, msg: &Message, db: &DatabaseConnection, name: &str, check_kind: NameCheckKind, + user_id: u64, ) -> crate::Result> { - let user_id = msg.from().ok_or(NoUserInfo)?.id.0; match check_kind { NameCheckKind::MustExist => { if !Account::exists(user_id, name, db).await? { @@ -32,12 +36,11 @@ pub async fn check_name( .await?; return Ok(Some(msg)); } - Ok(None) } NameCheckKind::NewAccountName => { if Account::exists(user_id, name, db).await? { let msg = bot - .send_message(msg.chat.id, "Account alreay exists") + .send_message(msg.chat.id, "Account already exists") .await?; return Ok(Some(msg)); } @@ -47,9 +50,9 @@ pub async fn check_name( .await?; return Ok(Some(msg)); } - Ok(None) } } + Ok(None) } /// Function to handle GetAccountName state @@ -60,13 +63,50 @@ pub async fn get_account_name( dialogue: MainDialogue, (next, check_kind): (super::PackagedHandler, NameCheckKind), ) -> crate::Result<()> { - super::generic::generic( - bot, - msg, - db, - dialogue, - |bot, msg, db, name| Box::pin(check_name(bot, msg, db, name, check_kind)), - next, - ) - .await + let user_id = msg.from().ok_or(NoUserInfo)?.id.0; + let mut handler = next.lock().await; + delete_optional(&bot, handler.previous.as_ref()).await; + + let text = match msg.text() { + Some(text) => text.trim_end(), + None => { + let mut send = bot.send_message( + msg.chat.id, + "Couldn't get the text of the message. Send the name again", + ); + if let NameCheckKind::MustExist = check_kind { + send = send.reply_markup(account_markup(user_id, &db).await?) + } + send.await?; + return Ok(()); + } + }; + + if text == "/cancel" { + dialogue.exit().await?; + bot.send_message(msg.chat.id, "Successfully cancelled") + .reply_markup(deletion_markup()) + .await?; + return Ok(()); + } + + if handler.func.is_none() { + let _ = dialogue.exit().await; + return Err(HandlerUsed.into()); + } + + if let Some(failure_message) = check_name(&bot, &msg, &db, text, check_kind, user_id).await? { + handler.previous = Some(failure_message); + return Ok(()); + } + + let func = handler.func.take().unwrap(); + drop(handler); + let text = text.to_owned(); + + if let Err(err) = func(bot, msg, db, dialogue.clone(), text).await { + let _ = dialogue.exit().await; + return Err(err); + } + Ok(()) } diff --git a/src/handlers/state/get_login.rs b/src/handlers/state/get_login.rs index 0294e57..b5d8a85 100644 --- a/src/handlers/state/get_login.rs +++ b/src/handlers/state/get_login.rs @@ -27,6 +27,7 @@ pub async fn get_login( Ok(None) }) }, + "Couldn't get the text of the message. Send the login again", next, ) .await diff --git a/src/handlers/state/get_master_pass.rs b/src/handlers/state/get_master_pass.rs index 94795cd..dd620de 100644 --- a/src/handlers/state/get_master_pass.rs +++ b/src/handlers/state/get_master_pass.rs @@ -6,7 +6,7 @@ use tokio::task::spawn_blocking; /// Returns true if the provided master password is valid #[inline] -pub async fn check_master_pass( +async fn check_master_pass( bot: &Throttle, msg: &Message, db: &DatabaseConnection, @@ -54,6 +54,7 @@ pub async fn get_master_pass( db, dialogue, |bot, msg, db, text| Box::pin(check_master_pass(bot, msg, db, text)), + "Couldn't get the text of the message. Send the master password again", next, ) .await diff --git a/src/handlers/state/get_password.rs b/src/handlers/state/get_password.rs index ece1e56..0affc1b 100644 --- a/src/handlers/state/get_password.rs +++ b/src/handlers/state/get_password.rs @@ -28,6 +28,7 @@ pub async fn get_password( Ok(None) }) }, + "Couldn't get the text of the message. Send the master password again", next, ) .await