Renamed DatabaseConnection into Pool

This commit is contained in:
StNicolay 2024-02-03 16:23:39 +03:00
parent bd10acb438
commit e8f0fcd819
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
27 changed files with 37 additions and 65 deletions

View File

@ -7,7 +7,7 @@ use tokio::{task::spawn_blocking, try_join};
#[inline] #[inline]
async fn update_account( async fn update_account(
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &Pool,
name: String, name: String,
field: AlterableField, field: AlterableField,
field_value: String, field_value: String,
@ -43,7 +43,7 @@ async fn update_account(
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
name: String, name: String,
@ -73,7 +73,7 @@ handler!(get_field(name:String, field:AlterableField, field_value:String), "Send
pub async fn alter( pub async fn alter(
bot: Throttle<Bot>, bot: Throttle<Bot>,
q: CallbackQuery, q: CallbackQuery,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
(hash, field): (super::NameHash, AlterableField), (hash, field): (super::NameHash, AlterableField),
) -> crate::Result<()> { ) -> crate::Result<()> {

View File

@ -6,7 +6,7 @@ use tokio::{task::spawn_blocking, try_join};
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
name: String, name: String,
@ -45,7 +45,7 @@ async fn get_master_pass(
pub async fn decrypt( pub async fn decrypt(
bot: Throttle<Bot>, bot: Throttle<Bot>,
q: CallbackQuery, q: CallbackQuery,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
hash: super::NameHash, hash: super::NameHash,
) -> crate::Result<()> { ) -> crate::Result<()> {

View File

@ -7,7 +7,7 @@ use crate::{change_state, prelude::*};
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
name: String, name: String,
@ -33,7 +33,7 @@ async fn get_master_pass(
pub async fn delete( pub async fn delete(
bot: Throttle<Bot>, bot: Throttle<Bot>,
q: CallbackQuery, q: CallbackQuery,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
(hash, is_command): (super::NameHash, bool), (hash, is_command): (super::NameHash, bool),
) -> crate::Result<()> { ) -> crate::Result<()> {

View File

@ -6,7 +6,7 @@ use teloxide::types::ParseMode;
pub async fn get( pub async fn get(
bot: Throttle<Bot>, bot: Throttle<Bot>,
q: CallbackQuery, q: CallbackQuery,
db: DatabaseConnection, db: Pool,
hash: super::NameHash, hash: super::NameHash,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = q.from.id.0; let user_id = q.from.id.0;

View File

@ -1,11 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
#[inline] #[inline]
pub async fn get_menu( pub async fn get_menu(bot: Throttle<Bot>, q: CallbackQuery, db: Pool) -> crate::Result<()> {
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
) -> crate::Result<()> {
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();

View File

@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
name: String, name: String,

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
#[inline] #[inline]
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> { pub async fn delete(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?; let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -7,7 +7,7 @@ use log::error;
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
_: String, _: String,

View File

@ -22,7 +22,7 @@ async fn decrypt_account(
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
ids: MessageIds, ids: MessageIds,
master_pass: String, master_pass: String,

View File

@ -2,11 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
#[inline] #[inline]
pub async fn get_account( pub async fn get_account(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?; let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -5,11 +5,7 @@ use teloxide::types::ParseMode;
/// Handles /`get_accounts` command by sending the list of copyable account names to the user /// Handles /`get_accounts` command by sending the list of copyable account names to the user
#[inline] #[inline]
pub async fn get_accounts( pub async fn get_accounts(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut account_names = Account::get_names(user_id, &db); let mut account_names = Account::get_names(user_id, &db);

View File

@ -11,7 +11,7 @@ use tokio::task::spawn_blocking;
async fn encrypt_account( async fn encrypt_account(
account: DecryptedAccount, account: DecryptedAccount,
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &Pool,
master_pass: Arc<str>, master_pass: Arc<str>,
failed: &Mutex<&mut Vec<String>>, failed: &Mutex<&mut Vec<String>>,
) { ) {
@ -30,7 +30,7 @@ async fn encrypt_account(
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
user: User, user: User,

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
#[inline] #[inline]
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> { pub async fn menu(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?; let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -6,7 +6,7 @@ use tokio::task::spawn_blocking;
async fn get_master_pass2( async fn get_master_pass2(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
hash: HashedBytes<[u8; 64], [u8; 64]>, hash: HashedBytes<[u8; 64], [u8; 64]>,
@ -45,7 +45,7 @@ async fn get_master_pass2(
async fn get_master_pass( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
_: Message, _: Message,
_: DatabaseConnection, _: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
mut ids: MessageIds, mut ids: MessageIds,
master_pass: String, master_pass: String,
@ -71,7 +71,7 @@ pub async fn set_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
dialogue: MainDialogue, dialogue: MainDialogue,
db: DatabaseConnection, db: Pool,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if MasterPass::exists(user_id, &db).await? { if MasterPass::exists(user_id, &db).await? {

View File

@ -21,7 +21,7 @@ use crate::callbacks::CallbackCommand;
fn get_dispatcher( fn get_dispatcher(
token: String, token: String,
db: DatabaseConnection, db: Pool,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> { ) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps}; use dptree::{case, deps};
@ -87,7 +87,7 @@ async fn main() -> Result<()> {
let token = env::var("TOKEN").expect("expected TOKEN in the enviroment"); 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 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?; entity::migrate(&pool).await?;

View File

@ -29,7 +29,7 @@ pub fn menu_markup_sync(
pub async fn menu_markup( pub async fn menu_markup(
command: impl Into<String> + Send, command: impl Into<String> + Send,
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &Pool,
) -> crate::Result<InlineKeyboardMarkup> { ) -> crate::Result<InlineKeyboardMarkup> {
let command: String = command.into(); let command: String = command.into();
let names: Vec<String> = Account::get_names(user_id, db).try_collect().await?; let names: Vec<String> = Account::get_names(user_id, db).try_collect().await?;

View File

@ -14,7 +14,7 @@ type DynError = Arc<dyn std::error::Error + Send + Sync>;
/// Returns None if account exists, Some(None) if there's an account and Some(Some(String)) if an error occures. /// 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 /// The String represents the error that occured
#[inline] #[inline]
async fn master_pass_exists(update: Update, db: DatabaseConnection) -> Option<Option<DynError>> { async fn master_pass_exists(update: Update, db: Pool) -> Option<Option<DynError>> {
let user_id = match update.user() { let user_id = match update.user() {
Some(user) => user.id.0, Some(user) => user.id.0,
None => return Some(Some(Arc::new(NoUserInfo))), None => return Some(Some(Arc::new(NoUserInfo))),

View File

@ -8,6 +8,6 @@ pub use crate::{
utils::*, utils::*,
}; };
pub use cryptography::prelude::*; pub use cryptography::prelude::*;
pub use entity::{prelude::*, Pool as DatabaseConnection}; pub use entity::{prelude::*, Pool};
pub use futures::{StreamExt, TryStreamExt}; pub use futures::{StreamExt, TryStreamExt};
pub use teloxide::{adaptors::Throttle, prelude::*}; pub use teloxide::{adaptors::Throttle, prelude::*};

View File

@ -6,18 +6,14 @@ use futures::future::BoxFuture;
pub async fn generic<F>( pub async fn generic<F>(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
check: F, check: F,
no_text_message: impl Into<String> + Send, no_text_message: impl Into<String> + Send,
handler: PackagedHandler<String>, handler: PackagedHandler<String>,
) -> crate::Result<()> ) -> crate::Result<()>
where where
for<'a> F: FnOnce( for<'a> F: FnOnce(&'a Message, &'a Pool, &'a str) -> BoxFuture<'a, crate::Result<Option<String>>>
&'a Message,
&'a DatabaseConnection,
&'a str,
) -> BoxFuture<'a, crate::Result<Option<String>>>
+ Send, + Send,
{ {
let mut handler = handler.lock().await; let mut handler = handler.lock().await;

View File

@ -1,11 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
#[inline] #[inline]
async fn check_login( async fn check_login(_: &Message, _: &Pool, login: &str) -> crate::Result<Option<String>> {
_: &Message,
_: &DatabaseConnection,
login: &str,
) -> crate::Result<Option<String>> {
let is_valid = validate_field(login); let is_valid = validate_field(login);
if !is_valid { if !is_valid {
return Ok(Some("Invalid login. Try again".to_owned())); return Ok(Some("Invalid login. Try again".to_owned()));

View File

@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
#[inline] #[inline]
async fn check_master_pass( async fn check_master_pass(
msg: &Message, msg: &Message,
db: &DatabaseConnection, db: &Pool,
master_pass: &str, master_pass: &str,
) -> crate::Result<Option<String>> { ) -> crate::Result<Option<String>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;

View File

@ -34,7 +34,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> {
#[inline] #[inline]
async fn check_new_master_pass( async fn check_new_master_pass(
_: &Message, _: &Message,
_: &DatabaseConnection, _: &Pool,
password: &str, password: &str,
) -> crate::Result<Option<String>> { ) -> crate::Result<Option<String>> {
let validity = check_master_pass(password); let validity = check_master_pass(password);

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
#[inline] #[inline]
async fn check_new_account_name( async fn check_new_account_name(
msg: &Message, msg: &Message,
db: &DatabaseConnection, db: &Pool,
name: &str, name: &str,
) -> crate::Result<Option<String>> { ) -> crate::Result<Option<String>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;

View File

@ -1,11 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
#[inline] #[inline]
async fn check_password( async fn check_password(_: &Message, _: &Pool, password: &str) -> crate::Result<Option<String>> {
_: &Message,
_: &DatabaseConnection,
password: &str,
) -> crate::Result<Option<String>> {
let is_valid = validate_field(password); let is_valid = validate_field(password);
if !is_valid { if !is_valid {
return Ok(Some("Invalid password. Try again".to_owned())); return Ok(Some("Invalid password. Try again".to_owned()));

View File

@ -132,7 +132,7 @@ fn user_from_vec(
pub async fn get_user( pub async fn get_user(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: Pool,
dialogue: MainDialogue, dialogue: MainDialogue,
handler: PackagedHandler<User>, handler: PackagedHandler<User>,
) -> crate::Result<()> { ) -> crate::Result<()> {

View File

@ -65,7 +65,7 @@ type DynHanlder<T> = Box<
dyn FnOnce( dyn FnOnce(
Throttle<Bot>, Throttle<Bot>,
Message, Message,
DatabaseConnection, Pool,
MainDialogue, MainDialogue,
MessageIds, MessageIds,
T, T,
@ -89,7 +89,7 @@ impl<T> Handler<T> {
H: FnOnce( H: FnOnce(
Throttle<Bot>, Throttle<Bot>,
Message, Message,
DatabaseConnection, Pool,
MainDialogue, MainDialogue,
MessageIds, MessageIds,
T, T,

View File

@ -7,11 +7,7 @@ pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
} }
#[inline] #[inline]
pub async fn name_from_hash( pub async fn name_from_hash(db: &Pool, user_id: u64, hash: &[u8]) -> crate::Result<Option<String>> {
db: &DatabaseConnection,
user_id: u64,
hash: &[u8],
) -> crate::Result<Option<String>> {
let hash = hex::encode(hash); let hash = hex::encode(hash);
Account::get_name_by_hash(user_id, hash, db) Account::get_name_by_hash(user_id, hash, db)
.await .await