From 332356a95bbed0e34d42f5d2e17e1632b0e90c5b Mon Sep 17 00:00:00 2001 From: StNicolay Date: Thu, 4 May 2023 21:15:41 +0300 Subject: [PATCH] Created markups.rs --- src/handlers/commands/get_account.rs | 22 ++++++---------------- src/handlers/markups.rs | 21 +++++++++++++++++++++ src/handlers/mod.rs | 1 + src/handlers/state/generic.rs | 1 + 4 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 src/handlers/markups.rs diff --git a/src/handlers/commands/get_account.rs b/src/handlers/commands/get_account.rs index 0bb62f3..a48aee5 100644 --- a/src/handlers/commands/get_account.rs +++ b/src/handlers/commands/get_account.rs @@ -1,13 +1,9 @@ -use crate::entity::{account, prelude::Account}; -use crate::handlers::{utils::package_handler, MainDialogue, State}; -use futures::TryStreamExt; -use sea_orm::prelude::*; -use teloxide::{ - adaptors::Throttle, - prelude::*, - types::ParseMode, - types::{KeyboardButton, KeyboardMarkup}, +use crate::{ + entity::{account, prelude::Account}, + handlers::{markups, utils::package_handler, MainDialogue, State}, }; +use sea_orm::prelude::*; +use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode}; use tokio::task::spawn_blocking; async fn get_master_pass( @@ -64,13 +60,7 @@ pub async fn get_account( dialogue: MainDialogue, db: DatabaseConnection, ) -> crate::Result<()> { - let account_names: Vec> = Account::get_names(msg.from().unwrap().id.0, &db) - .await? - .map_ok(|account| KeyboardButton::new(account)) - .try_chunks(3) - .try_collect() - .await?; - let markup = KeyboardMarkup::new(account_names).resize_keyboard(true); + let markup = markups::account_markup(msg.from().unwrap().id.0, &db).await?; let previous = bot .send_message(msg.chat.id, "Send account name") .reply_markup(markup) diff --git a/src/handlers/markups.rs b/src/handlers/markups.rs new file mode 100644 index 0000000..15a995e --- /dev/null +++ b/src/handlers/markups.rs @@ -0,0 +1,21 @@ +use crate::entity::prelude::Account; +use futures::TryStreamExt; +use sea_orm::prelude::*; +use teloxide::types::{KeyboardButton, KeyboardMarkup}; + +#[inline] +pub async fn account_markup( + user_id: u64, + db: &DatabaseConnection, +) -> crate::Result { + let account_names: Vec> = Account::get_names(user_id, db) + .await? + .map_ok(|account| KeyboardButton::new(account)) + .try_chunks(3) + .try_collect() + .await?; + let markup = KeyboardMarkup::new(account_names) + .resize_keyboard(true) + .one_time_keyboard(true); + Ok(markup) +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 0e30f18..031f774 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -10,6 +10,7 @@ use teloxide::{ use tokio::sync::Mutex; mod commands; +mod markups; mod state; mod utils; diff --git a/src/handlers/state/generic.rs b/src/handlers/state/generic.rs index 7358103..f561f13 100644 --- a/src/handlers/state/generic.rs +++ b/src/handlers/state/generic.rs @@ -24,6 +24,7 @@ where dialogue.exit().await?; bot.send_message(msg.chat.id, "Successfully cancelled") .await?; + return Ok(()); } match check(&bot, &msg, &db, &text).await { Ok(true) => (),