Code cleanup

Moved migrations/ into entity/
MessageIds now return errors except for API ones
This commit is contained in:
2024-02-03 16:21:16 +03:00
parent 64e3210cc5
commit bd10acb438
13 changed files with 67 additions and 62 deletions

View File

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

View File

@ -36,7 +36,7 @@ macro_rules! handler {
async fn $function_name(
bot: Throttle<Bot>,
_: Message,
_: DatabaseConnection,
_: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
$($param: $type),*
@ -57,7 +57,7 @@ macro_rules! simple_state_handler {
pub async fn $function_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> $crate::Result<()> {

View File

@ -87,9 +87,9 @@ async fn main() -> Result<()> {
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 pool = sqlx::mysql::MySqlPool::connect(&database_url).await?;
let pool = DatabaseConnection::connect(&database_url).await?;
sqlx::migrate!().run(&pool).await?;
entity::migrate(&pool).await?;
get_dispatcher(token, pool).dispatch().await;
Ok(())

View File

@ -1,4 +1,5 @@
use crate::prelude::*;
use futures::TryFutureExt;
use itertools::Itertools;
use std::{fmt::Write, path::Path};
use teloxide::{
@ -35,7 +36,7 @@ async fn download_file(bot: &Throttle<Bot>, file: &FileMeta) -> crate::Result<Ve
let mut data = Vec::with_capacity(file.size as usize);
bot.download_file_stream(&path)
.try_for_each(|bytes| {
data.extend(bytes);
data.extend_from_slice(&bytes);
async { Ok(()) }
})
.await?;
@ -163,12 +164,9 @@ pub async fn get_user(
}
};
let existing_names = async {
Account::get_names(user_id, &db)
.try_collect()
.await
.map_err(Into::into)
};
let existing_names = Account::get_names(user_id, &db)
.try_collect()
.map_err(Into::into);
let (data, existing_names) = try_join!(download_file(&bot, file), existing_names)?;

View File

@ -1,27 +1,27 @@
use crate::prelude::*;
use futures::future::BoxFuture;
use futures::{future::BoxFuture, TryFutureExt};
use std::{mem, sync::Arc};
use teloxide::{
requests::HasPayload,
types::{InlineKeyboardMarkup, MessageId, ParseMode},
RequestError,
};
use tokio::{join, sync::Mutex};
use tokio::{sync::Mutex, try_join};
#[derive(Clone, Copy)]
pub struct MessageIds(pub ChatId, pub MessageId);
impl MessageIds {
// Tries to delete the message while ignoring API errors
#[inline]
pub async fn delete(self, bot: &Throttle<Bot>) {
let _ = bot.delete_message(self.0, self.1).await;
pub async fn delete(self, bot: &Throttle<Bot>) -> crate::Result<()> {
match bot.delete_message(self.0, self.1).await {
Ok(_) | Err(RequestError::Api(_)) => Ok(()),
Err(err) => Err(err.into()),
}
}
/// Tries to alter the message or sends a new one
///
/// # Returns
///
/// Returns true if the message was edited successfully. Returns false otherwise
#[inline]
pub async fn alter_message(
&mut self,
@ -40,12 +40,15 @@ impl MessageIds {
Err(err) => return Err(err.into()),
};
let mut send = bot.send_message(self.0, mem::take(&mut edit.text));
let payload = send.payload_mut();
payload.parse_mode = edit.parse_mode;
payload.reply_markup = mem::take(&mut edit.reply_markup).map(Into::into);
let send = {
let mut send_request = bot.send_message(self.0, mem::take(&mut edit.text));
let payload = send_request.payload_mut();
payload.parse_mode = edit.parse_mode;
payload.reply_markup = edit.reply_markup.take().map(Into::into);
send_request.send().map_err(Into::into)
};
let msg = join!(self.delete(bot), send.send()).1?;
let msg = try_join!(self.delete(bot), send)?.1;
*self = Self::from(&msg);
Ok(())
}
@ -70,7 +73,7 @@ type DynHanlder<T> = Box<
+ Send,
>;
pub struct Handler<T: ?Sized> {
pub struct Handler<T> {
pub func: Option<DynHanlder<T>>,
pub previous: MessageIds,