Removed utils mod, moved hexing the name to entity library

This commit is contained in:
StNicolay 2024-02-03 16:31:19 +03:00
parent e8f0fcd819
commit a359364606
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
12 changed files with 18 additions and 26 deletions

2
Cargo.lock generated
View File

@ -438,6 +438,7 @@ name = "entity"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",
"hex",
"sqlx", "sqlx",
] ]
@ -1164,7 +1165,6 @@ dependencies = [
"dotenvy", "dotenvy",
"entity", "entity",
"futures", "futures",
"hex",
"itertools 0.12.1", "itertools 0.12.1",
"log", "log",
"parking_lot", "parking_lot",

View File

@ -30,7 +30,6 @@ cryptography = { version = "0.1", path = "cryptography" }
dotenvy = "0.15" dotenvy = "0.15"
entity = { version = "0.1", path = "entity" } entity = { version = "0.1", path = "entity" }
futures = "0.3" futures = "0.3"
hex = "0.4"
itertools = "0.12" itertools = "0.12"
log = "0.4" log = "0.4"
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -16,3 +16,4 @@ sqlx = { version = "0.7", features = [
"macros", "macros",
"migrate", "migrate",
], default-features = false } ], default-features = false }
hex = "0.4"

View File

@ -93,13 +93,14 @@ impl Account {
.map(|_| ()) .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] #[inline]
pub async fn get_name_by_hash( pub async fn get_name_by_hash(
user_id: u64, user_id: u64,
hash: String, hash: &[u8],
pool: &Pool, pool: &Pool,
) -> crate::Result<Option<String>> { ) -> crate::Result<Option<String>> {
let hash = hex::encode(hash);
let name = query_as::<_, (String,)>( let name = query_as::<_, (String,)>(
"SELECT `name` FROM `account` WHERE SHA2(`name`, 256) = ? AND `user_id` = ?;", "SELECT `name` FROM `account` WHERE SHA2(`name`, 256) = ? AND `user_id` = ?;",
) )

View File

@ -80,7 +80,7 @@ pub async fn alter(
let user_id = q.from.id.0; let user_id = q.from.id.0;
let mut ids: MessageIds = q.message.as_ref().unwrap().into(); 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!( try_join!(
bot.send_message(ids.0, "Account wasn't found") bot.send_message(ids.0, "Account wasn't found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -52,7 +52,7 @@ pub async fn decrypt(
let mut ids: MessageIds = q.message.as_ref().unwrap().into(); let mut ids: MessageIds = q.message.as_ref().unwrap().into();
let user_id = q.from.id.0; 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!( try_join!(
bot.send_message(ids.0, "Account wasn't found") bot.send_message(ids.0, "Account wasn't found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -40,7 +40,7 @@ pub async fn delete(
let mut ids: MessageIds = q.message.as_ref().unwrap().into(); let mut ids: MessageIds = q.message.as_ref().unwrap().into();
let user_id = q.from.id.0; 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!( try_join!(
bot.send_message(ids.0, "Account wasn't found") bot.send_message(ids.0, "Account wasn't found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -12,7 +12,7 @@ pub async fn get(
let user_id = q.from.id.0; let user_id = q.from.id.0;
let mut ids: MessageIds = q.message.as_ref().unwrap().into(); 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!( try_join!(
bot.send_message(ids.0, "Account wasn't found") bot.send_message(ids.0, "Account wasn't found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -0,0 +1,7 @@
use crate::prelude::*;
/// Deletes the message ignoring the errors
#[inline]
pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
}

View File

@ -1,6 +1,7 @@
mod callbacks; mod callbacks;
mod commands; mod commands;
mod default; mod default;
mod delete_mesage_handler;
mod errors; mod errors;
mod filter_user_info; mod filter_user_info;
mod macros; mod macros;
@ -9,7 +10,6 @@ mod master_password_check;
mod models; mod models;
mod prelude; mod prelude;
mod state; mod state;
mod utils;
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use dotenvy::dotenv; use dotenvy::dotenv;
@ -45,7 +45,7 @@ fn get_dispatcher(
.branch(case![Command::Import].endpoint(commands::import)); .branch(case![Command::Import].endpoint(commands::import));
let message_handler = Update::filter_message() 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 // Filters out the messages without user information
.branch(filter_user_info::get_handler()) .branch(filter_user_info::get_handler())
.branch(case![State::GetNewName(next)].endpoint(state::get_new_name)) .branch(case![State::GetNewName(next)].endpoint(state::get_new_name))

View File

@ -5,7 +5,6 @@ pub use crate::{
markups::*, markups::*,
models::*, models::*,
state::{Handler, MainDialogue, MessageIds, PackagedHandler, State}, state::{Handler, MainDialogue, MessageIds, PackagedHandler, State},
utils::*,
}; };
pub use cryptography::prelude::*; pub use cryptography::prelude::*;
pub use entity::{prelude::*, Pool}; pub use entity::{prelude::*, Pool};

View File

@ -1,15 +0,0 @@
use crate::prelude::*;
/// Deletes the message ignoring the errors
#[inline]
pub async fn delete_message(bot: Throttle<Bot>, 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<Option<String>> {
let hash = hex::encode(hash);
Account::get_name_by_hash(user_id, hash, db)
.await
.map_err(Into::into)
}