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,
(): (),
) -> crate::Result<()> {
let document = match msg.document() {
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(());
}
};
let document = msg.document().unwrap();
match document.file_name {
Some(ref name) if name.trim_end().ends_with(".json") => (),
_ => {

View File

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

View File

@ -1,6 +1,6 @@
use crate::{
errors::HandlerUsed,
handlers::{utils::delete_optional, MainDialogue, PackagedHandler},
handlers::{markups::deletion_markup, utils::delete_optional, MainDialogue, PackagedHandler},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -13,7 +13,24 @@ pub async fn get_document(
dialogue: MainDialogue,
next: PackagedHandler<()>,
) -> crate::Result<()> {
let handler = next.lock().await.take().ok_or(HandlerUsed)?;
delete_optional(&bot, &handler.previous).await;
let handler = next.lock().await.take();
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
}

View File

@ -1,6 +1,6 @@
use crate::handlers::{Handler, MainDialogue, PackagedHandler};
use sea_orm::prelude::*;
use std::{future::Future, sync::Arc};
use std::{borrow::Borrow, future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*};
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
#[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 {
let msg: &Message = msg.borrow();
let _ = bot.delete_message(msg.chat.id, msg.id).await;
}
}