Updated generic to get the text to send when there's no text
This commit is contained in:
parent
40f7194cbe
commit
a0920ba3d3
@ -14,6 +14,7 @@ pub async fn generic<F>(
|
||||
db: DatabaseConnection,
|
||||
dialogue: crate::handlers::MainDialogue,
|
||||
check: F,
|
||||
no_text_message: impl Into<String>,
|
||||
next: super::PackagedHandler<String>,
|
||||
) -> 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(());
|
||||
}
|
||||
};
|
||||
|
@ -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<Bot>,
|
||||
msg: &Message,
|
||||
db: &DatabaseConnection,
|
||||
name: &str,
|
||||
check_kind: NameCheckKind,
|
||||
user_id: u64,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
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<String>, 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(())
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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<Bot>,
|
||||
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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user