Updated delete_optional to get Option<impl Borrow<Message>>, update get_document to actually check that the document was sent

This commit is contained in:
StNicolay 2023-05-09 21:09:30 +03:00
parent 26ac79a2ed
commit 9365c75e6e
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
4 changed files with 28 additions and 18 deletions

View File

@ -79,16 +79,7 @@ async fn get_document(
dialogue: MainDialogue, dialogue: MainDialogue,
(): (), (): (),
) -> crate::Result<()> { ) -> crate::Result<()> {
let document = match msg.document() { let document = msg.document().unwrap();
Some(doc) => doc,
None => {
bot.send_message(msg.chat.id, "You didn't send a file")
.reply_markup(deletion_markup())
.await?;
dialogue.exit().await?;
return Ok(());
}
};
match document.file_name { match document.file_name {
Some(ref name) if name.trim_end().ends_with(".json") => (), Some(ref name) if name.trim_end().ends_with(".json") => (),
_ => { _ => {

View File

@ -25,16 +25,17 @@ where
) -> PinnedFuture<'a, crate::Result<bool>>, ) -> PinnedFuture<'a, crate::Result<bool>>,
{ {
let handler = next.lock().await.take(); let handler = next.lock().await.take();
let previous = handler
.as_ref()
.and_then(|handler| handler.previous.as_ref());
delete_optional(&bot, previous).await;
if text == "/cancel" { if text == "/cancel" {
let previous = handler.and_then(|handler| handler.previous);
delete_optional(&bot, &previous).await;
bot.send_message(msg.chat.id, "Successfully cancelled") bot.send_message(msg.chat.id, "Successfully cancelled")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())
.await?; .await?;
return Ok(()); return Ok(());
} }
let handler = handler.ok_or(HandlerUsed)?; let handler = handler.ok_or(HandlerUsed)?;
delete_optional(&bot, &handler.previous).await;
match check(&bot, &msg, &db, &text).await { match check(&bot, &msg, &db, &text).await {
Ok(true) => (), Ok(true) => (),
Ok(false) => { Ok(false) => {

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
errors::HandlerUsed, errors::HandlerUsed,
handlers::{utils::delete_optional, MainDialogue, PackagedHandler}, handlers::{markups::deletion_markup, utils::delete_optional, MainDialogue, PackagedHandler},
}; };
use sea_orm::prelude::*; use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*}; use teloxide::{adaptors::Throttle, prelude::*};
@ -13,7 +13,24 @@ pub async fn get_document(
dialogue: MainDialogue, dialogue: MainDialogue,
next: PackagedHandler<()>, next: PackagedHandler<()>,
) -> crate::Result<()> { ) -> crate::Result<()> {
let handler = next.lock().await.take().ok_or(HandlerUsed)?; let handler = next.lock().await.take();
delete_optional(&bot, &handler.previous).await; let previous = handler
.as_ref()
.and_then(|handler| handler.previous.as_ref());
delete_optional(&bot, previous).await;
if let Some("/cancel") = msg.text().map(str::trim_end) {
bot.send_message(msg.chat.id, "Successfully cancelled")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
let handler = handler.ok_or(HandlerUsed)?;
if msg.document().is_none() {
bot.send_message(msg.chat.id, "You didn't send a file")
.reply_markup(deletion_markup())
.await?;
dialogue.exit().await?;
return Ok(());
}
(handler.handler)(bot, msg, db, dialogue, ()).await (handler.handler)(bot, msg, db, dialogue, ()).await
} }

View File

@ -1,6 +1,6 @@
use crate::handlers::{Handler, MainDialogue, PackagedHandler}; use crate::handlers::{Handler, MainDialogue, PackagedHandler};
use sea_orm::prelude::*; use sea_orm::prelude::*;
use std::{future::Future, sync::Arc}; use std::{borrow::Borrow, future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*}; use teloxide::{adaptors::Throttle, prelude::*};
use tokio::sync::Mutex; use tokio::sync::Mutex;
@ -29,8 +29,9 @@ pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
/// Deletes the message if there is one ignoring the errors /// Deletes the message if there is one ignoring the errors
#[inline] #[inline]
pub async fn delete_optional(bot: &Throttle<Bot>, msg: &Option<Message>) { pub async fn delete_optional(bot: &Throttle<Bot>, msg: Option<impl Borrow<Message>>) {
if let Some(msg) = msg { if let Some(msg) = msg {
let msg: &Message = msg.borrow();
let _ = bot.delete_message(msg.chat.id, msg.id).await; let _ = bot.delete_message(msg.chat.id, msg.id).await;
} }
} }