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,
|
db: DatabaseConnection,
|
||||||
dialogue: crate::handlers::MainDialogue,
|
dialogue: crate::handlers::MainDialogue,
|
||||||
check: F,
|
check: F,
|
||||||
|
no_text_message: impl Into<String>,
|
||||||
next: super::PackagedHandler<String>,
|
next: super::PackagedHandler<String>,
|
||||||
) -> crate::Result<()>
|
) -> crate::Result<()>
|
||||||
where
|
where
|
||||||
@ -30,11 +31,7 @@ where
|
|||||||
let text = match msg.text() {
|
let text = match msg.text() {
|
||||||
Some(text) => text.trim_end(),
|
Some(text) => text.trim_end(),
|
||||||
None => {
|
None => {
|
||||||
bot.send_message(
|
bot.send_message(msg.chat.id, no_text_message).await?;
|
||||||
msg.chat.id,
|
|
||||||
"Couldn't get the text of the message. Send the message again",
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
entity::prelude::Account,
|
entity::prelude::Account,
|
||||||
errors::NoUserInfo,
|
errors::{HandlerUsed, NoUserInfo},
|
||||||
handlers::{markups::account_markup, utils::validate_field, MainDialogue},
|
handlers::{
|
||||||
|
markups::{account_markup, deletion_markup},
|
||||||
|
utils::{delete_optional, validate_field},
|
||||||
|
MainDialogue,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use sea_orm::prelude::*;
|
use sea_orm::prelude::*;
|
||||||
use teloxide::{adaptors::Throttle, prelude::*};
|
use teloxide::{adaptors::Throttle, prelude::*};
|
||||||
@ -15,14 +19,14 @@ pub enum NameCheckKind {
|
|||||||
|
|
||||||
/// Validates the account name
|
/// Validates the account name
|
||||||
#[inline]
|
#[inline]
|
||||||
pub async fn check_name(
|
async fn check_name(
|
||||||
bot: &Throttle<Bot>,
|
bot: &Throttle<Bot>,
|
||||||
msg: &Message,
|
msg: &Message,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
name: &str,
|
name: &str,
|
||||||
check_kind: NameCheckKind,
|
check_kind: NameCheckKind,
|
||||||
|
user_id: u64,
|
||||||
) -> crate::Result<Option<Message>> {
|
) -> crate::Result<Option<Message>> {
|
||||||
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
|
||||||
match check_kind {
|
match check_kind {
|
||||||
NameCheckKind::MustExist => {
|
NameCheckKind::MustExist => {
|
||||||
if !Account::exists(user_id, name, db).await? {
|
if !Account::exists(user_id, name, db).await? {
|
||||||
@ -32,12 +36,11 @@ pub async fn check_name(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(Some(msg));
|
return Ok(Some(msg));
|
||||||
}
|
}
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
NameCheckKind::NewAccountName => {
|
NameCheckKind::NewAccountName => {
|
||||||
if Account::exists(user_id, name, db).await? {
|
if Account::exists(user_id, name, db).await? {
|
||||||
let msg = bot
|
let msg = bot
|
||||||
.send_message(msg.chat.id, "Account alreay exists")
|
.send_message(msg.chat.id, "Account already exists")
|
||||||
.await?;
|
.await?;
|
||||||
return Ok(Some(msg));
|
return Ok(Some(msg));
|
||||||
}
|
}
|
||||||
@ -47,10 +50,10 @@ pub async fn check_name(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(Some(msg));
|
return Ok(Some(msg));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Function to handle GetAccountName state
|
/// Function to handle GetAccountName state
|
||||||
pub async fn get_account_name(
|
pub async fn get_account_name(
|
||||||
@ -60,13 +63,50 @@ pub async fn get_account_name(
|
|||||||
dialogue: MainDialogue,
|
dialogue: MainDialogue,
|
||||||
(next, check_kind): (super::PackagedHandler<String>, NameCheckKind),
|
(next, check_kind): (super::PackagedHandler<String>, NameCheckKind),
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
super::generic::generic(
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
||||||
bot,
|
let mut handler = next.lock().await;
|
||||||
msg,
|
delete_optional(&bot, handler.previous.as_ref()).await;
|
||||||
db,
|
|
||||||
dialogue,
|
let text = match msg.text() {
|
||||||
|bot, msg, db, name| Box::pin(check_name(bot, msg, db, name, check_kind)),
|
Some(text) => text.trim_end(),
|
||||||
next,
|
None => {
|
||||||
)
|
let mut send = bot.send_message(
|
||||||
.await
|
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)
|
Ok(None)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
"Couldn't get the text of the message. Send the login again",
|
||||||
next,
|
next,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
@ -6,7 +6,7 @@ use tokio::task::spawn_blocking;
|
|||||||
|
|
||||||
/// Returns true if the provided master password is valid
|
/// Returns true if the provided master password is valid
|
||||||
#[inline]
|
#[inline]
|
||||||
pub async fn check_master_pass(
|
async fn check_master_pass(
|
||||||
bot: &Throttle<Bot>,
|
bot: &Throttle<Bot>,
|
||||||
msg: &Message,
|
msg: &Message,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
@ -54,6 +54,7 @@ pub async fn get_master_pass(
|
|||||||
db,
|
db,
|
||||||
dialogue,
|
dialogue,
|
||||||
|bot, msg, db, text| Box::pin(check_master_pass(bot, msg, db, text)),
|
|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,
|
next,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
@ -28,6 +28,7 @@ pub async fn get_password(
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
"Couldn't get the text of the message. Send the master password again",
|
||||||
next,
|
next,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
Loading…
x
Reference in New Issue
Block a user