Updated altering accounts to handle errors

This commit is contained in:
StNicolay 2024-04-26 18:22:35 +03:00
parent 2eff5f3f4e
commit ff1d5a039d
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
5 changed files with 27 additions and 26 deletions

View File

@ -130,7 +130,7 @@ impl Account {
original_name: &str,
new_name: &str,
pool: &Pool,
) -> crate::Result<()> {
) -> crate::Result<bool> {
query!(
"UPDATE account SET name = ? WHERE user_id = ? AND name = ?",
new_name,
@ -139,7 +139,7 @@ impl Account {
)
.execute(pool)
.await
.map(|_| ())
.map(|result| result.rows_affected() != 0)
}
#[inline]
@ -148,7 +148,7 @@ impl Account {
name: &str,
login: Vec<u8>,
pool: &Pool,
) -> crate::Result<()> {
) -> crate::Result<bool> {
query!(
"UPDATE account SET enc_login = ? WHERE user_id = ? AND name = ?",
login,
@ -157,7 +157,7 @@ impl Account {
)
.execute(pool)
.await
.map(|_| ())
.map(|result| result.rows_affected() != 0)
}
#[inline]
@ -166,7 +166,7 @@ impl Account {
name: &str,
password: Vec<u8>,
pool: &Pool,
) -> crate::Result<()> {
) -> crate::Result<bool> {
query!(
"UPDATE account SET enc_password = ? WHERE user_id = ? AND name = ?",
password,
@ -175,6 +175,6 @@ impl Account {
)
.execute(pool)
.await
.map(|_| ())
.map(|result| result.rows_affected() != 0)
}
}

View File

@ -66,7 +66,6 @@ account_not_found: "Account wasn't found"
send_new_name: "Send new name"
send_new_login: "Send new login"
send_new_password: "Send new password"
success_choose_account_to_view: "Success. Chose an account to view"
send_json_file: "Send the json file to import"
send_master_pass_to_delete_everything: "Send master password to delete EVERY ACCOUNT. THIS ACTION CANNOT BE UNDONE"
everything_was_deleted: "Every account was deleted"

View File

@ -66,7 +66,6 @@ account_not_found: "Аккаунт не найден"
send_new_name: "Отправьте новое название"
send_new_login: "Отправьте новый логин"
send_new_password: "Отправьте новый пароль"
success_choose_account_to_view: "Успешно. Выберите аккаунт для просмотра"
send_json_file: "Отправьте файл json для импорта"
send_master_pass_to_delete_everything: "Отправьте мастер-пароль, чтобы удалить ВСЕ АККАУНТЫ. ЭТО ДЕЙСТВИЕ НЕЛЬЗЯ ОТМЕНИТЬ"
everything_was_deleted: "Все аккаунты были удалены"

View File

@ -11,14 +11,16 @@ async fn update_account(
field: AlterableField,
field_value: String,
master_pass: String,
) -> crate::Result<()> {
) -> crate::Result<bool> {
if field == Name {
Account::update_name(user_id, &name, &field_value, db).await?;
return Ok(());
return Account::update_name(user_id, &name, &field_value, db)
.await
.map_err(Into::into);
}
let salt = Account::get_salt(user_id, &name, db).await?.unwrap();
let Some(salt) = Account::get_salt(user_id, &name, db).await? else {
return Ok(false);
};
let field_value = spawn_blocking(move || {
let cipher = Cipher::new(master_pass.as_bytes(), &salt);
@ -28,13 +30,13 @@ async fn update_account(
})
.await?;
match field {
let updated = match field {
Login => Account::update_login(user_id, &name, field_value, db).await?,
Pass => Account::update_password(user_id, &name, field_value, db).await?,
Name => unreachable!(),
}
};
Ok(())
Ok(updated)
}
#[inline]
@ -54,15 +56,17 @@ async fn get_master_pass(
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
update_account(user_id, &db, name, field, field_value, master_pass).await?;
let text = match update_account(user_id, &db, name, field, field_value, master_pass).await {
Ok(true) => locale.success.as_str(),
Ok(false) => &locale.account_not_found,
Err(err) => {
log::error!("{err:?}");
&locale.something_went_wrong
}
};
ids.alter_message(
&bot,
&locale.success_choose_account_to_view,
menu_markup("get", user_id, &db).await?,
None,
)
.await?;
ids.alter_message(&bot, text, deletion_markup(locale), None)
.await?;
Ok(())
}
@ -82,7 +86,7 @@ pub async fn alter(
let mut ids: MessageIds = q.message.as_ref().unwrap().into();
let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else {
bot.send_message(ids.0, "Account wasn't found")
bot.send_message(ids.0, &locale.account_not_found)
.reply_markup(deletion_markup(locale))
.await?;
bot.answer_callback_query(q.id).await?;

View File

@ -96,7 +96,6 @@ pub struct Locale {
pub send_new_name: LocaleString,
pub send_new_login: LocaleString,
pub send_new_password: LocaleString,
pub success_choose_account_to_view: LocaleString,
pub send_json_file: LocaleString,
pub send_master_pass_to_delete_everything: LocaleString,
pub everything_was_deleted: LocaleString,