added get endpoint, updated state_management
This commit is contained in:
@ -14,11 +14,10 @@ pub async fn generic<F>(
|
||||
) -> crate::Result<()>
|
||||
where
|
||||
for<'a> F: FnOnce(
|
||||
&'a Throttle<Bot>,
|
||||
&'a Message,
|
||||
&'a DatabaseConnection,
|
||||
&'a str,
|
||||
) -> BoxFuture<'a, crate::Result<Option<Message>>>,
|
||||
) -> BoxFuture<'a, crate::Result<Option<String>>>,
|
||||
{
|
||||
let mut handler = next.lock().await;
|
||||
if handler.func.is_none() {
|
||||
@ -26,8 +25,6 @@ where
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
let text = match msg.text() {
|
||||
Some(text) => text.trim(),
|
||||
None => {
|
||||
@ -45,16 +42,18 @@ where
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(failure_message) = check(&bot, &msg, &db, text).await? {
|
||||
if let Some(text) = check(&msg, &db, text).await? {
|
||||
let failure_message = bot.send_message(msg.chat.id, text).await?;
|
||||
handler.previous = MessageIds::from(&failure_message);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let previous = handler.previous;
|
||||
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 {
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), previous, text).await {
|
||||
let _ = dialogue.exit().await;
|
||||
return Err(err);
|
||||
}
|
||||
|
@ -1,24 +1,5 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Checks that the account with that name exists
|
||||
#[inline]
|
||||
async fn check_name(
|
||||
bot: &Throttle<Bot>,
|
||||
msg: &Message,
|
||||
db: &DatabaseConnection,
|
||||
name: &str,
|
||||
user_id: u64,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
if !Account::exists(user_id, name, db).await? {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Account doesn't exists. Try again")
|
||||
.reply_markup(account_markup(user_id, db).await?)
|
||||
.await?;
|
||||
return Ok(Some(msg));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Function to handle GetExistingName state
|
||||
pub async fn get_existing_name(
|
||||
bot: Throttle<Bot>,
|
||||
@ -35,8 +16,6 @@ pub async fn get_existing_name(
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
let text = match msg.text() {
|
||||
Some(text) => text.trim(),
|
||||
None => {
|
||||
@ -45,7 +24,7 @@ pub async fn get_existing_name(
|
||||
msg.chat.id,
|
||||
"Couldn't get the text of the message. Send the name again",
|
||||
)
|
||||
.reply_markup(account_markup(user_id, &db).await?)
|
||||
.reply_markup(account_list_markup(user_id, &db).await?)
|
||||
.await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
return Ok(());
|
||||
@ -60,16 +39,21 @@ pub async fn get_existing_name(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(failure_message) = check_name(&bot, &msg, &db, text, user_id).await? {
|
||||
handler.previous = MessageIds::from(&failure_message);
|
||||
if !Account::exists(user_id, text, &db).await? {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Account doesn't exists. Try again")
|
||||
.reply_markup(account_list_markup(user_id, &db).await?)
|
||||
.await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let previous = handler.previous;
|
||||
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 {
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), previous, text).await {
|
||||
let _ = dialogue.exit().await;
|
||||
return Err(err);
|
||||
}
|
||||
|
@ -2,17 +2,13 @@ use crate::prelude::*;
|
||||
|
||||
#[inline]
|
||||
async fn check_login(
|
||||
bot: &Throttle<Bot>,
|
||||
msg: &Message,
|
||||
_: &Message,
|
||||
_: &DatabaseConnection,
|
||||
login: &str,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
) -> crate::Result<Option<String>> {
|
||||
let is_valid = validate_field(login);
|
||||
if !is_valid {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Invalid login. Try again")
|
||||
.await?;
|
||||
return Ok(Some(msg));
|
||||
return Ok(Some("Invalid login. Try again".to_owned()));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
@ -5,11 +5,10 @@ use tokio::task::spawn_blocking;
|
||||
/// Returns true if the provided master password is valid
|
||||
#[inline]
|
||||
async fn check_master_pass(
|
||||
bot: &Throttle<Bot>,
|
||||
msg: &Message,
|
||||
db: &DatabaseConnection,
|
||||
master_pass: &str,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
) -> crate::Result<Option<String>> {
|
||||
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
||||
let model = MasterPass::get(user_id, db).await?;
|
||||
|
||||
@ -20,21 +19,15 @@ async fn check_master_pass(
|
||||
}
|
||||
None => {
|
||||
error!("User was put into the GetMasterPass state with no master password set");
|
||||
let msg = bot
|
||||
.send_message(
|
||||
msg.chat.id,
|
||||
"No master password set. Use /cancel and set it by using /set_master_pass",
|
||||
)
|
||||
.await?;
|
||||
return Ok(Some(msg));
|
||||
return Ok(Some(
|
||||
"No master password set. Use /cancel and set it by using /set_master_pass"
|
||||
.to_owned(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
if !is_valid {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Wrong master password. Try again")
|
||||
.await?;
|
||||
return Ok(Some(msg));
|
||||
return Ok(Some("Wrong master password. Try again".to_owned()));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
@ -33,20 +33,13 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> {
|
||||
/// Checks that the account with that name exists
|
||||
#[inline]
|
||||
async fn check_new_master_pass(
|
||||
bot: &Throttle<Bot>,
|
||||
msg: &Message,
|
||||
_: &Message,
|
||||
_: &DatabaseConnection,
|
||||
password: &str,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
) -> crate::Result<Option<String>> {
|
||||
let validity = check_master_pass(password);
|
||||
|
||||
match process_validity(validity) {
|
||||
Ok(()) => Ok(None),
|
||||
Err(error_text) => {
|
||||
let msg = bot.send_message(msg.chat.id, error_text).await?;
|
||||
Ok(Some(msg))
|
||||
}
|
||||
}
|
||||
Ok(process_validity(validity).err())
|
||||
}
|
||||
|
||||
crate::simple_state_handler!(
|
||||
|
@ -3,26 +3,19 @@ use crate::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>> {
|
||||
) -> crate::Result<Option<String>> {
|
||||
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)
|
||||
if Account::exists(user_id, name, db).await? {
|
||||
Ok(Some("Account already exists".to_owned()))
|
||||
} else if !validate_field(name) {
|
||||
Ok(Some("Invalid account name. Try again".to_owned()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
crate::simple_state_handler!(
|
||||
|
@ -2,17 +2,13 @@ use crate::prelude::*;
|
||||
|
||||
#[inline]
|
||||
async fn check_password(
|
||||
bot: &Throttle<Bot>,
|
||||
msg: &Message,
|
||||
_: &Message,
|
||||
_: &DatabaseConnection,
|
||||
login: &str,
|
||||
) -> crate::Result<Option<Message>> {
|
||||
let is_valid = validate_field(login);
|
||||
password: &str,
|
||||
) -> crate::Result<Option<String>> {
|
||||
let is_valid = validate_field(password);
|
||||
if !is_valid {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Invalid password. Try again")
|
||||
.await?;
|
||||
return Ok(Some(msg));
|
||||
return Ok(Some("Invalid password. Try again".to_owned()));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
@ -145,8 +145,6 @@ pub async fn get_user(
|
||||
return Err(HandlerUsed.into());
|
||||
}
|
||||
|
||||
handler.previous.delete(&bot).await;
|
||||
|
||||
if let Some("/cancel") = msg.text().map(str::trim) {
|
||||
dialogue.exit().await?;
|
||||
bot.send_message(msg.chat.id, "Successfully cancelled")
|
||||
@ -190,10 +188,11 @@ pub async fn get_user(
|
||||
}
|
||||
};
|
||||
|
||||
let previous = handler.previous;
|
||||
let func = handler.func.take().unwrap();
|
||||
drop(handler);
|
||||
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), user).await {
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), previous, user).await {
|
||||
let _ = dialogue.exit().await;
|
||||
return Err(err);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::prelude::*;
|
||||
use futures::future::BoxFuture;
|
||||
use std::sync::Arc;
|
||||
use teloxide::types::MessageId;
|
||||
use teloxide::types::{InlineKeyboardMarkup, MessageId};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@ -27,6 +27,7 @@ type DynHanlder<T> = Box<
|
||||
Message,
|
||||
DatabaseConnection,
|
||||
MainDialogue,
|
||||
MessageIds,
|
||||
T,
|
||||
) -> BoxFuture<'static, crate::Result<()>>
|
||||
+ Send,
|
||||
@ -50,6 +51,7 @@ impl<T> Handler<T> {
|
||||
Message,
|
||||
DatabaseConnection,
|
||||
MainDialogue,
|
||||
MessageIds,
|
||||
T,
|
||||
) -> BoxFuture<'static, crate::Result<()>>
|
||||
+ Send
|
||||
|
Reference in New Issue
Block a user