Switched from sea-orm to sqlx

This commit is contained in:
2023-11-25 19:29:06 +03:00
parent e4e33f52b1
commit 9f967e82d5
40 changed files with 434 additions and 1110 deletions

View File

@@ -5,10 +5,7 @@ use tokio::task::spawn_blocking;
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db)
.await?
.try_collect()
.await?;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
if names.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")

View File

@@ -1,7 +1,5 @@
use crate::prelude::*;
use log::error;
use sea_orm::TransactionTrait;
use tokio::try_join;
/// Gets the master password, deletes the accounts and the master password from DB.
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
@@ -16,17 +14,17 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let txn = db.begin().await?;
let result = try_join!(
Account::delete_all(user_id, &txn),
MasterPass::remove(user_id, &txn),
let mut txn = db.begin().await?;
let result = (
Account::delete_all(user_id, &mut *txn).await,
MasterPass::remove(user_id, &mut *txn).await,
);
let text = match result {
Ok(_) => {
(Ok(()), Ok(())) => {
txn.commit().await?;
"Everything was deleted"
}
Err(err) => {
(Err(err), _) | (_, Err(err)) => {
error!("{}", crate::Error::from(err));
txn.rollback().await?;
"Something went wrong. Try again later"

View File

@@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
/// Decryptes the account on a worker thread and adds it to the accounts vector
#[inline]
async fn decrypt_account(
account: account::Model,
account: Account,
master_pass: Arc<str>,
accounts: &Mutex<&mut Vec<DecryptedAccount>>,
) -> crate::Result<()> {
@@ -38,7 +38,6 @@ async fn get_master_pass(
let master_pass: Arc<str> = master_pass.into();
Account::get_all(user_id, &db)
.await?
.err_into::<crate::Error>()
.try_for_each_concurrent(3, |account| {
decrypt_account(account, master_pass.clone(), &accounts)

View File

@@ -9,10 +9,7 @@ pub async fn get_account(
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db)
.await?
.try_collect()
.await?;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
if names.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")

View File

@@ -11,7 +11,7 @@ pub async fn get_accounts(
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut account_names = Account::get_names(user_id, &db).await?;
let mut account_names = Account::get_names(user_id, &db);
let mut text = if let Some(name) = account_names.try_next().await? {
format!("Accounts:\n`{name}`")

View File

@@ -18,7 +18,7 @@ async fn encrypt_account(
let name = account.name.clone();
match spawn_blocking(move || account.into_account(user_id, &master_pass)).await {
Ok(account) => match account.insert(db).await {
Ok(_) => (),
Ok(()) => (),
Err(_) => failed.lock().push(name),
},
_ => failed.lock().push(name),

View File

@@ -5,10 +5,7 @@ use tokio::task::spawn_blocking;
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db)
.await?
.try_collect()
.await?;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
if names.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")

View File

@@ -1,6 +1,5 @@
use crate::{change_state, prelude::*};
use cryptography::hashing::HashedBytes;
use sea_orm::ActiveValue::Set;
use tokio::task::spawn_blocking;
#[inline]
@@ -14,7 +13,7 @@ async fn get_master_pass2(
master_pass: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = Set(msg.from().ok_or(NoUserInfo)?.id.0);
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if !hash.verify(master_pass.as_bytes()) {
ids.alter_message(
@@ -28,10 +27,10 @@ async fn get_master_pass2(
return Ok(());
}
let model = master_pass::ActiveModel {
let model = MasterPass {
user_id,
password_hash: Set(hash.hash.to_vec()),
salt: Set(hash.salt.to_vec()),
password_hash: hash.hash.to_vec(),
salt: hash.salt.to_vec(),
};
model.insert(&db).await?;