Introduced GetUserExt

This commit is contained in:
StNicolay 2024-08-17 18:35:29 +03:00
parent 374a9d91c7
commit 659965a744
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
19 changed files with 38 additions and 19 deletions

View File

@ -51,7 +51,7 @@ async fn get_master_pass(
master_pass: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let text = match update_account(user_id, &db, name, field, field_value, master_pass).await {
Ok(true) => locale.success.as_str(),

View File

@ -15,7 +15,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let Some(account) = Account::get(user_id, &name, &db).await? else {
bot.send_message(msg.chat.id, &locale.no_accounts_found)

View File

@ -13,7 +13,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
Account::delete(user_id, &name, &db).await?;
ids.alter_message(&bot, &locale.success, deletion_markup(locale), None)

View File

@ -17,7 +17,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let account = spawn_blocking(move || {
DecryptedAccount {

View File

@ -6,7 +6,7 @@ pub async fn delete(
db: Pool,
locale: LocaleRef,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let markup = menu_markup("delete1", user_id, &db).await?;

View File

@ -12,7 +12,7 @@ async fn get_master_pass(
_: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let mut txn = db.begin().await?;
let result = (

View File

@ -29,7 +29,7 @@ async fn get_master_pass(
dialogue.exit().await?;
ids.delete(&bot).await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let mut accounts = Vec::new();
{

View File

@ -6,7 +6,7 @@ pub async fn get_account(
db: Pool,
locale: LocaleRef,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let markup = menu_markup("decrypt", user_id, &db).await?;

View File

@ -9,7 +9,7 @@ pub async fn get_accounts(
db: Pool,
locale: LocaleRef,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let mut account_names = Account::get_names(user_id, &db);
let Some(mut text) = account_names.try_next().await? else {

View File

@ -38,7 +38,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let mut failed = Vec::new();
{

View File

@ -6,7 +6,7 @@ pub async fn menu(
db: Pool,
locale: LocaleRef,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let markup = menu_markup("get", user_id, &db).await?;

View File

@ -16,8 +16,8 @@ async fn get_master_pass2(
master_pass: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let from = msg.from.as_ref().ok_or(NoUserInfo)?;
let user_id = from.id.0;
let from = msg.get_user()?;
let user_id = msg.get_user_id()?;
if !hash.verify(master_pass.as_bytes()) {
ids.alter_message(
@ -82,7 +82,7 @@ pub async fn set_master_pass(
db: Pool,
locale: LocaleRef,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
if MasterPass::exists(user_id, &db).await? {
bot.send_message(msg.chat.id, &locale.master_password_is_set)
.reply_markup(deletion_markup(locale))

View File

@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{errors::NoUserInfo, prelude::*};
use std::sync::Arc;
use teloxide::{
dispatching::{dialogue::GetChatId, DpHandlerDescription},

View File

@ -10,6 +10,7 @@ mod markups;
mod models;
mod prelude;
mod state;
mod utils;
use anyhow::{Error, Result};

View File

@ -1,11 +1,12 @@
pub use crate::{
cryptography::{account::Decrypted as DecryptedAccount, validate_field},
entity::{account::Account, master_pass::MasterPass, Pool},
errors::{handle_error, NoUserInfo},
errors::handle_error,
first_handler, handler,
locales::LocaleRef,
markups::{deletion_markup, menu_markup},
state::{CallbackMessageIdExt as _, Handler, MainDialogue, MessageIds, PackagedHandler, State},
utils::GetUserExt as _,
};
pub use futures::{StreamExt as _, TryStreamExt as _};
pub use teloxide::{adaptors::Throttle, prelude::*};

View File

@ -8,7 +8,7 @@ async fn check_master_pass(
locale: LocaleRef,
master_pass: &str,
) -> crate::Result<Option<String>> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let Some(model) = MasterPass::get(user_id, db).await? else {
handle_error(anyhow::anyhow!(
"User was put into the GetMasterPass state with no master password set"

View File

@ -7,7 +7,7 @@ async fn check_new_account_name(
locale: LocaleRef,
name: &str,
) -> crate::Result<Option<String>> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
if Account::exists(user_id, name, db).await? {
Ok(Some(locale.account_already_exists.to_owned()))

View File

@ -181,7 +181,7 @@ pub async fn get_user(
locale: LocaleRef,
handler: PackagedHandler<User>,
) -> crate::Result<()> {
let user_id = msg.from.as_ref().ok_or(NoUserInfo)?.id.0;
let user_id = msg.get_user_id()?;
let mut handler = handler.lock().await;
if handler.func.is_none() {

17
src/utils.rs Normal file
View File

@ -0,0 +1,17 @@
use teloxide::types::User;
use crate::{errors::NoUserInfo, prelude::*};
pub trait GetUserExt {
fn get_user(&self) -> Result<&User, NoUserInfo>;
fn get_user_id(&self) -> Result<u64, NoUserInfo> {
self.get_user().map(|user| user.id.0)
}
}
impl GetUserExt for Message {
fn get_user(&self) -> Result<&User, NoUserInfo> {
self.from.as_ref().ok_or(NoUserInfo)
}
}