diff --git a/Cargo.lock b/Cargo.lock index 6a48f5d..5a4fc45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -438,6 +438,7 @@ name = "entity" version = "0.1.0" dependencies = [ "futures", + "hex", "sqlx", ] @@ -1164,7 +1165,6 @@ dependencies = [ "dotenvy", "entity", "futures", - "hex", "itertools 0.12.1", "log", "parking_lot", diff --git a/Cargo.toml b/Cargo.toml index adfdb6e..24bd1ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ cryptography = { version = "0.1", path = "cryptography" } dotenvy = "0.15" entity = { version = "0.1", path = "entity" } futures = "0.3" -hex = "0.4" itertools = "0.12" log = "0.4" parking_lot = "0.12" diff --git a/entity/Cargo.toml b/entity/Cargo.toml index e0a49ba..742a3d0 100644 --- a/entity/Cargo.toml +++ b/entity/Cargo.toml @@ -16,3 +16,4 @@ sqlx = { version = "0.7", features = [ "macros", "migrate", ], default-features = false } +hex = "0.4" diff --git a/entity/src/account.rs b/entity/src/account.rs index fa4ed7b..14c8ac7 100644 --- a/entity/src/account.rs +++ b/entity/src/account.rs @@ -93,13 +93,14 @@ impl Account { .map(|_| ()) } - /// Gets a name by a hex of a SHA256 hash of the name + /// Gets a name by a SHA256 hash of the name #[inline] pub async fn get_name_by_hash( user_id: u64, - hash: String, + hash: &[u8], pool: &Pool, ) -> crate::Result> { + let hash = hex::encode(hash); let name = query_as::<_, (String,)>( "SELECT `name` FROM `account` WHERE SHA2(`name`, 256) = ? AND `user_id` = ?;", ) diff --git a/src/callbacks/alter.rs b/src/callbacks/alter.rs index fb33027..ba2130d 100644 --- a/src/callbacks/alter.rs +++ b/src/callbacks/alter.rs @@ -80,7 +80,7 @@ pub async fn alter( let user_id = q.from.id.0; let mut ids: MessageIds = q.message.as_ref().unwrap().into(); - let Some(name) = name_from_hash(&db, user_id, &hash).await? else { + let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else { try_join!( bot.send_message(ids.0, "Account wasn't found") .reply_markup(deletion_markup()) diff --git a/src/callbacks/decrypt.rs b/src/callbacks/decrypt.rs index 135c520..f1c78f7 100644 --- a/src/callbacks/decrypt.rs +++ b/src/callbacks/decrypt.rs @@ -52,7 +52,7 @@ pub async fn decrypt( let mut ids: MessageIds = q.message.as_ref().unwrap().into(); let user_id = q.from.id.0; - let Some(name) = name_from_hash(&db, user_id, &hash).await? else { + let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else { try_join!( bot.send_message(ids.0, "Account wasn't found") .reply_markup(deletion_markup()) diff --git a/src/callbacks/delete.rs b/src/callbacks/delete.rs index b5523e4..7d448a3 100644 --- a/src/callbacks/delete.rs +++ b/src/callbacks/delete.rs @@ -40,7 +40,7 @@ pub async fn delete( let mut ids: MessageIds = q.message.as_ref().unwrap().into(); let user_id = q.from.id.0; - let Some(name) = name_from_hash(&db, user_id, &hash).await? else { + let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else { try_join!( bot.send_message(ids.0, "Account wasn't found") .reply_markup(deletion_markup()) diff --git a/src/callbacks/get.rs b/src/callbacks/get.rs index d68b433..acb76e3 100644 --- a/src/callbacks/get.rs +++ b/src/callbacks/get.rs @@ -12,7 +12,7 @@ pub async fn get( let user_id = q.from.id.0; let mut ids: MessageIds = q.message.as_ref().unwrap().into(); - let Some(name) = name_from_hash(&db, user_id, &hash).await? else { + let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else { try_join!( bot.send_message(ids.0, "Account wasn't found") .reply_markup(deletion_markup()) diff --git a/src/delete_mesage_handler.rs b/src/delete_mesage_handler.rs new file mode 100644 index 0000000..466db04 --- /dev/null +++ b/src/delete_mesage_handler.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +/// Deletes the message ignoring the errors +#[inline] +pub async fn delete_message(bot: Throttle, msg: Message) { + let _ = bot.delete_message(msg.chat.id, msg.id).await; +} diff --git a/src/main.rs b/src/main.rs index 4cdcff1..01fbf1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod callbacks; mod commands; mod default; +mod delete_mesage_handler; mod errors; mod filter_user_info; mod macros; @@ -9,7 +10,6 @@ mod master_password_check; mod models; mod prelude; mod state; -mod utils; use anyhow::{Error, Result}; use dotenvy::dotenv; @@ -45,7 +45,7 @@ fn get_dispatcher( .branch(case![Command::Import].endpoint(commands::import)); let message_handler = Update::filter_message() - .map_async(utils::delete_message) + .map_async(delete_mesage_handler::delete_message) // Filters out the messages without user information .branch(filter_user_info::get_handler()) .branch(case![State::GetNewName(next)].endpoint(state::get_new_name)) diff --git a/src/prelude.rs b/src/prelude.rs index c24da32..15bad65 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -5,7 +5,6 @@ pub use crate::{ markups::*, models::*, state::{Handler, MainDialogue, MessageIds, PackagedHandler, State}, - utils::*, }; pub use cryptography::prelude::*; pub use entity::{prelude::*, Pool}; diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index fc56a2d..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::prelude::*; - -/// Deletes the message ignoring the errors -#[inline] -pub async fn delete_message(bot: Throttle, msg: Message) { - let _ = bot.delete_message(msg.chat.id, msg.id).await; -} - -#[inline] -pub async fn name_from_hash(db: &Pool, user_id: u64, hash: &[u8]) -> crate::Result> { - let hash = hex::encode(hash); - Account::get_name_by_hash(user_id, hash, db) - .await - .map_err(Into::into) -}