diff --git a/src/callbacks/alter.rs b/src/callbacks/alter.rs index ebb3de7..fb33027 100644 --- a/src/callbacks/alter.rs +++ b/src/callbacks/alter.rs @@ -7,7 +7,7 @@ use tokio::{task::spawn_blocking, try_join}; #[inline] async fn update_account( user_id: u64, - db: &DatabaseConnection, + db: &Pool, name: String, field: AlterableField, field_value: String, @@ -43,7 +43,7 @@ async fn update_account( async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, name: String, @@ -73,7 +73,7 @@ handler!(get_field(name:String, field:AlterableField, field_value:String), "Send pub async fn alter( bot: Throttle, q: CallbackQuery, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, (hash, field): (super::NameHash, AlterableField), ) -> crate::Result<()> { diff --git a/src/callbacks/decrypt.rs b/src/callbacks/decrypt.rs index 45b8a69..135c520 100644 --- a/src/callbacks/decrypt.rs +++ b/src/callbacks/decrypt.rs @@ -6,7 +6,7 @@ use tokio::{task::spawn_blocking, try_join}; async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, name: String, @@ -45,7 +45,7 @@ async fn get_master_pass( pub async fn decrypt( bot: Throttle, q: CallbackQuery, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, hash: super::NameHash, ) -> crate::Result<()> { diff --git a/src/callbacks/delete.rs b/src/callbacks/delete.rs index b1ba1cf..b5523e4 100644 --- a/src/callbacks/delete.rs +++ b/src/callbacks/delete.rs @@ -7,7 +7,7 @@ use crate::{change_state, prelude::*}; async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, name: String, @@ -33,7 +33,7 @@ async fn get_master_pass( pub async fn delete( bot: Throttle, q: CallbackQuery, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, (hash, is_command): (super::NameHash, bool), ) -> crate::Result<()> { diff --git a/src/callbacks/get.rs b/src/callbacks/get.rs index 560a432..d68b433 100644 --- a/src/callbacks/get.rs +++ b/src/callbacks/get.rs @@ -6,7 +6,7 @@ use teloxide::types::ParseMode; pub async fn get( bot: Throttle, q: CallbackQuery, - db: DatabaseConnection, + db: Pool, hash: super::NameHash, ) -> crate::Result<()> { let user_id = q.from.id.0; diff --git a/src/callbacks/get_menu.rs b/src/callbacks/get_menu.rs index c57d14e..c9bccad 100644 --- a/src/callbacks/get_menu.rs +++ b/src/callbacks/get_menu.rs @@ -1,11 +1,7 @@ use crate::prelude::*; #[inline] -pub async fn get_menu( - bot: Throttle, - q: CallbackQuery, - db: DatabaseConnection, -) -> crate::Result<()> { +pub async fn get_menu(bot: Throttle, q: CallbackQuery, db: Pool) -> crate::Result<()> { let user_id = q.from.id.0; let mut ids: MessageIds = q.message.as_ref().unwrap().into(); diff --git a/src/commands/add_account.rs b/src/commands/add_account.rs index fc07715..9a55cc5 100644 --- a/src/commands/add_account.rs +++ b/src/commands/add_account.rs @@ -7,7 +7,7 @@ use tokio::task::spawn_blocking; async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, name: String, diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 25341ff..56fc6c0 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use tokio::task::spawn_blocking; #[inline] -pub async fn delete(bot: Throttle, msg: Message, db: DatabaseConnection) -> crate::Result<()> { +pub async fn delete(bot: Throttle, msg: Message, db: Pool) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let names: Vec = Account::get_names(user_id, &db).try_collect().await?; diff --git a/src/commands/delete_all.rs b/src/commands/delete_all.rs index b9ff0df..fd2e4b6 100644 --- a/src/commands/delete_all.rs +++ b/src/commands/delete_all.rs @@ -7,7 +7,7 @@ use log::error; async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, _: String, diff --git a/src/commands/export.rs b/src/commands/export.rs index 30f779c..2d740af 100644 --- a/src/commands/export.rs +++ b/src/commands/export.rs @@ -22,7 +22,7 @@ async fn decrypt_account( async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, ids: MessageIds, master_pass: String, diff --git a/src/commands/get_account.rs b/src/commands/get_account.rs index 0c45011..715d706 100644 --- a/src/commands/get_account.rs +++ b/src/commands/get_account.rs @@ -2,11 +2,7 @@ use crate::prelude::*; use tokio::task::spawn_blocking; #[inline] -pub async fn get_account( - bot: Throttle, - msg: Message, - db: DatabaseConnection, -) -> crate::Result<()> { +pub async fn get_account(bot: Throttle, msg: Message, db: Pool) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let names: Vec = Account::get_names(user_id, &db).try_collect().await?; diff --git a/src/commands/get_accounts.rs b/src/commands/get_accounts.rs index c8870b3..86593f2 100644 --- a/src/commands/get_accounts.rs +++ b/src/commands/get_accounts.rs @@ -5,11 +5,7 @@ use teloxide::types::ParseMode; /// Handles /`get_accounts` command by sending the list of copyable account names to the user #[inline] -pub async fn get_accounts( - bot: Throttle, - msg: Message, - db: DatabaseConnection, -) -> crate::Result<()> { +pub async fn get_accounts(bot: Throttle, msg: Message, db: Pool) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let mut account_names = Account::get_names(user_id, &db); diff --git a/src/commands/import.rs b/src/commands/import.rs index a47a651..ecd26ae 100644 --- a/src/commands/import.rs +++ b/src/commands/import.rs @@ -11,7 +11,7 @@ use tokio::task::spawn_blocking; async fn encrypt_account( account: DecryptedAccount, user_id: u64, - db: &DatabaseConnection, + db: &Pool, master_pass: Arc, failed: &Mutex<&mut Vec>, ) { @@ -30,7 +30,7 @@ async fn encrypt_account( async fn get_master_pass( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, user: User, diff --git a/src/commands/menu.rs b/src/commands/menu.rs index 037e2e9..c501341 100644 --- a/src/commands/menu.rs +++ b/src/commands/menu.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use tokio::task::spawn_blocking; #[inline] -pub async fn menu(bot: Throttle, msg: Message, db: DatabaseConnection) -> crate::Result<()> { +pub async fn menu(bot: Throttle, msg: Message, db: Pool) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let names: Vec = Account::get_names(user_id, &db).try_collect().await?; diff --git a/src/commands/set_master_pass.rs b/src/commands/set_master_pass.rs index 938d8db..9c786f6 100644 --- a/src/commands/set_master_pass.rs +++ b/src/commands/set_master_pass.rs @@ -6,7 +6,7 @@ use tokio::task::spawn_blocking; async fn get_master_pass2( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, mut ids: MessageIds, hash: HashedBytes<[u8; 64], [u8; 64]>, @@ -45,7 +45,7 @@ async fn get_master_pass2( async fn get_master_pass( bot: Throttle, _: Message, - _: DatabaseConnection, + _: Pool, dialogue: MainDialogue, mut ids: MessageIds, master_pass: String, @@ -71,7 +71,7 @@ pub async fn set_master_pass( bot: Throttle, msg: Message, dialogue: MainDialogue, - db: DatabaseConnection, + db: Pool, ) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; if MasterPass::exists(user_id, &db).await? { diff --git a/src/main.rs b/src/main.rs index b4f8dd4..4cdcff1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use crate::callbacks::CallbackCommand; fn get_dispatcher( token: String, - db: DatabaseConnection, + db: Pool, ) -> Dispatcher, crate::Error, teloxide::dispatching::DefaultKey> { use dptree::{case, deps}; @@ -87,7 +87,7 @@ async fn main() -> Result<()> { let token = env::var("TOKEN").expect("expected TOKEN in the enviroment"); let database_url = env::var("DATABASE_URL").expect("expected DATABASE_URL in the enviroment"); - let pool = DatabaseConnection::connect(&database_url).await?; + let pool = Pool::connect(&database_url).await?; entity::migrate(&pool).await?; diff --git a/src/markups.rs b/src/markups.rs index 403d77c..8b865c4 100644 --- a/src/markups.rs +++ b/src/markups.rs @@ -29,7 +29,7 @@ pub fn menu_markup_sync( pub async fn menu_markup( command: impl Into + Send, user_id: u64, - db: &DatabaseConnection, + db: &Pool, ) -> crate::Result { let command: String = command.into(); let names: Vec = Account::get_names(user_id, db).try_collect().await?; diff --git a/src/master_password_check.rs b/src/master_password_check.rs index abc1b6e..fb0447f 100644 --- a/src/master_password_check.rs +++ b/src/master_password_check.rs @@ -14,7 +14,7 @@ type DynError = Arc; /// Returns None if account exists, Some(None) if there's an account and Some(Some(String)) if an error occures. /// The String represents the error that occured #[inline] -async fn master_pass_exists(update: Update, db: DatabaseConnection) -> Option> { +async fn master_pass_exists(update: Update, db: Pool) -> Option> { let user_id = match update.user() { Some(user) => user.id.0, None => return Some(Some(Arc::new(NoUserInfo))), diff --git a/src/prelude.rs b/src/prelude.rs index e60f8ca..c24da32 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,6 +8,6 @@ pub use crate::{ utils::*, }; pub use cryptography::prelude::*; -pub use entity::{prelude::*, Pool as DatabaseConnection}; +pub use entity::{prelude::*, Pool}; pub use futures::{StreamExt, TryStreamExt}; pub use teloxide::{adaptors::Throttle, prelude::*}; diff --git a/src/state/generic.rs b/src/state/generic.rs index 9df33f0..a124f09 100644 --- a/src/state/generic.rs +++ b/src/state/generic.rs @@ -6,18 +6,14 @@ use futures::future::BoxFuture; pub async fn generic( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, check: F, no_text_message: impl Into + Send, handler: PackagedHandler, ) -> crate::Result<()> where - for<'a> F: FnOnce( - &'a Message, - &'a DatabaseConnection, - &'a str, - ) -> BoxFuture<'a, crate::Result>> + for<'a> F: FnOnce(&'a Message, &'a Pool, &'a str) -> BoxFuture<'a, crate::Result>> + Send, { let mut handler = handler.lock().await; diff --git a/src/state/get_login.rs b/src/state/get_login.rs index 39a80d0..6c9c15a 100644 --- a/src/state/get_login.rs +++ b/src/state/get_login.rs @@ -1,11 +1,7 @@ use crate::prelude::*; #[inline] -async fn check_login( - _: &Message, - _: &DatabaseConnection, - login: &str, -) -> crate::Result> { +async fn check_login(_: &Message, _: &Pool, login: &str) -> crate::Result> { let is_valid = validate_field(login); if !is_valid { return Ok(Some("Invalid login. Try again".to_owned())); diff --git a/src/state/get_master_pass.rs b/src/state/get_master_pass.rs index a0c46b2..f5e677c 100644 --- a/src/state/get_master_pass.rs +++ b/src/state/get_master_pass.rs @@ -7,7 +7,7 @@ use tokio::task::spawn_blocking; #[inline] async fn check_master_pass( msg: &Message, - db: &DatabaseConnection, + db: &Pool, master_pass: &str, ) -> crate::Result> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; diff --git a/src/state/get_new_master_pass.rs b/src/state/get_new_master_pass.rs index 0a87ed5..b33df6d 100644 --- a/src/state/get_new_master_pass.rs +++ b/src/state/get_new_master_pass.rs @@ -34,7 +34,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> { #[inline] async fn check_new_master_pass( _: &Message, - _: &DatabaseConnection, + _: &Pool, password: &str, ) -> crate::Result> { let validity = check_master_pass(password); diff --git a/src/state/get_new_name.rs b/src/state/get_new_name.rs index 0916674..ec76531 100644 --- a/src/state/get_new_name.rs +++ b/src/state/get_new_name.rs @@ -4,7 +4,7 @@ use crate::prelude::*; #[inline] async fn check_new_account_name( msg: &Message, - db: &DatabaseConnection, + db: &Pool, name: &str, ) -> crate::Result> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; diff --git a/src/state/get_password.rs b/src/state/get_password.rs index a883ea3..e214981 100644 --- a/src/state/get_password.rs +++ b/src/state/get_password.rs @@ -1,11 +1,7 @@ use crate::prelude::*; #[inline] -async fn check_password( - _: &Message, - _: &DatabaseConnection, - password: &str, -) -> crate::Result> { +async fn check_password(_: &Message, _: &Pool, password: &str) -> crate::Result> { let is_valid = validate_field(password); if !is_valid { return Ok(Some("Invalid password. Try again".to_owned())); diff --git a/src/state/get_user.rs b/src/state/get_user.rs index d4a7427..35efe17 100644 --- a/src/state/get_user.rs +++ b/src/state/get_user.rs @@ -132,7 +132,7 @@ fn user_from_vec( pub async fn get_user( bot: Throttle, msg: Message, - db: DatabaseConnection, + db: Pool, dialogue: MainDialogue, handler: PackagedHandler, ) -> crate::Result<()> { diff --git a/src/state/handler.rs b/src/state/handler.rs index a722f4d..2638428 100644 --- a/src/state/handler.rs +++ b/src/state/handler.rs @@ -65,7 +65,7 @@ type DynHanlder = Box< dyn FnOnce( Throttle, Message, - DatabaseConnection, + Pool, MainDialogue, MessageIds, T, @@ -89,7 +89,7 @@ impl Handler { H: FnOnce( Throttle, Message, - DatabaseConnection, + Pool, MainDialogue, MessageIds, T, diff --git a/src/utils.rs b/src/utils.rs index 1dd805f..fc56a2d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,11 +7,7 @@ pub async fn delete_message(bot: Throttle, msg: Message) { } #[inline] -pub async fn name_from_hash( - db: &DatabaseConnection, - user_id: u64, - hash: &[u8], -) -> crate::Result> { +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