Code cleanup

This commit is contained in:
2024-02-22 12:44:02 +03:00
parent 8979cc5b25
commit b526eb0c12
18 changed files with 124 additions and 148 deletions

View File

@@ -1,20 +1,18 @@
use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
let markup = menu_markup("get", user_id, &db).await?;
if names.is_empty() {
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let markup = spawn_blocking(|| menu_markup_sync("delete1", names)).await?;
bot.send_message(msg.chat.id, "Choose the account to delete")
.reply_markup(markup)
.await?;

View File

@@ -14,6 +14,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut txn = db.begin().await?;
let result = (
Account::delete_all(user_id, &mut *txn).await,

View File

@@ -1,20 +1,18 @@
use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn get_account(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
let markup = menu_markup("decrypt", user_id, &db).await?;
if names.is_empty() {
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let markup = spawn_blocking(|| menu_markup_sync("decrypt", names)).await?;
bot.send_message(msg.chat.id, "Choose the account to get")
.reply_markup(markup)
.await?;

View File

@@ -1,5 +1,4 @@
use crate::prelude::*;
use futures::future;
use std::fmt::Write;
use teloxide::types::ParseMode;
@@ -18,13 +17,9 @@ pub async fn get_accounts(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::
return Ok(());
};
account_names
.map_err(crate::Error::from)
.try_for_each(|name| {
let result = write!(text, "\n`{name}`").map_err(Into::into);
future::ready(result)
})
.await?;
while let Some(name) = account_names.try_next().await? {
write!(text, "\n`{name}`")?;
}
bot.send_message(msg.chat.id, text)
.parse_mode(ParseMode::MarkdownV2)

View File

@@ -1,20 +1,18 @@
use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;
let markup = menu_markup("get", user_id, &db).await?;
if names.is_empty() {
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, "You don't have any accounts")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let markup = spawn_blocking(|| menu_markup_sync("get", names)).await?;
bot.send_message(msg.chat.id, "Choose your account")
.reply_markup(markup)
.await?;

View File

@@ -40,7 +40,6 @@ async fn get_master_pass2(
Ok(())
}
/// Actually sets the master password
#[inline]
async fn get_master_pass(
bot: Throttle<Bot>,

View File

@@ -6,7 +6,7 @@ pub async fn start(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {
bot.send_message(
msg.chat.id,
"Hi! This bot can be used to store the passwords securely. \
Use /help command to get the list of commands",
Use /help command to get the list of commands",
)
.await?;
Ok(())