From 22ec6a3c3be8fac39c13962286ba29d496da68d4 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 11 Jan 2024 01:46:01 +0100 Subject: [PATCH] Move webhook deletion in `Polling` --- crates/teloxide/src/dispatching/dispatcher.rs | 2 +- crates/teloxide/src/repls/commands_repl.rs | 22 +++------ crates/teloxide/src/repls/repl.rs | 2 +- .../teloxide/src/update_listeners/polling.rs | 49 ++++++++----------- 4 files changed, 29 insertions(+), 46 deletions(-) diff --git a/crates/teloxide/src/dispatching/dispatcher.rs b/crates/teloxide/src/dispatching/dispatcher.rs index f3b5a4d7..e0bedaca 100644 --- a/crates/teloxide/src/dispatching/dispatcher.rs +++ b/crates/teloxide/src/dispatching/dispatcher.rs @@ -293,7 +293,7 @@ where R: Requester + Clone, ::GetUpdates: Send, { - let listener = update_listeners::polling_default(self.bot.clone()).await; + let listener = update_listeners::polling_default(self.bot.clone()); let error_handler = LoggingErrorHandler::with_custom_text("An error from the update listener"); diff --git a/crates/teloxide/src/repls/commands_repl.rs b/crates/teloxide/src/repls/commands_repl.rs index 1f9cbebb..a58410c6 100644 --- a/crates/teloxide/src/repls/commands_repl.rs +++ b/crates/teloxide/src/repls/commands_repl.rs @@ -110,14 +110,11 @@ where { let cloned_bot = bot.clone(); - Box::pin(async move { - Self::repl_with_listener( - bot, - handler, - update_listeners::polling_default(cloned_bot).await, - ) - .await - }) + Box::pin(Self::repl_with_listener( + bot, + handler, + update_listeners::polling_default(cloned_bot), + )) } fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) -> BoxFuture<'a, ()> @@ -212,13 +209,8 @@ where let cloned_bot = bot.clone(); #[allow(deprecated)] - commands_repl_with_listener( - bot, - handler, - update_listeners::polling_default(cloned_bot).await, - cmd, - ) - .await; + commands_repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot), cmd) + .await; } /// A [REPL] for commands, with a custom [`UpdateListener`]. diff --git a/crates/teloxide/src/repls/repl.rs b/crates/teloxide/src/repls/repl.rs index 728593ae..bb5216eb 100644 --- a/crates/teloxide/src/repls/repl.rs +++ b/crates/teloxide/src/repls/repl.rs @@ -56,7 +56,7 @@ where H: Injectable, Args> + Send + Sync + 'static, { let cloned_bot = bot.clone(); - repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot).await).await; + repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot)).await; } /// A [REPL] for messages, with a custom [`UpdateListener`]. diff --git a/crates/teloxide/src/update_listeners/polling.rs b/crates/teloxide/src/update_listeners/polling.rs index 18a62b49..2cea9bd1 100644 --- a/crates/teloxide/src/update_listeners/polling.rs +++ b/crates/teloxide/src/update_listeners/polling.rs @@ -31,6 +31,7 @@ pub struct PollingBuilder { pub limit: Option, pub allowed_updates: Option>, pub drop_pending_updates: bool, + pub delete_webhook: bool, } impl PollingBuilder @@ -85,10 +86,8 @@ where } /// Deletes webhook if it was set up. - pub async fn delete_webhook(self) -> Self { - delete_webhook_if_setup(&self.bot).await; - - self + pub fn delete_webhook(self) -> Self { + Self { delete_webhook: true, ..self } } /// Returns a long polling update listener with configuration from the @@ -96,7 +95,8 @@ where /// /// See also: [`polling_default`], [`Polling`]. pub fn build(self) -> Polling { - let Self { bot, timeout, limit, allowed_updates, drop_pending_updates } = self; + let Self { bot, timeout, limit, allowed_updates, drop_pending_updates, delete_webhook } = + self; let (token, flag) = mk_stop_token(); let polling = Polling { bot, @@ -104,6 +104,7 @@ where limit, allowed_updates, drop_pending_updates, + delete_webhook, flag: Some(flag), token, }; @@ -119,13 +120,12 @@ where /// ## Notes /// /// This function will automatically delete a webhook if it was set up. -pub async fn polling_default(bot: R) -> Polling +pub fn polling_default(bot: R) -> Polling where R: Requester + Send + 'static, ::GetUpdates: Send, { - let polling = - Polling::builder(bot).timeout(Duration::from_secs(10)).delete_webhook().await.build(); + let polling = Polling::builder(bot).timeout(Duration::from_secs(10)).delete_webhook().build(); assert_update_listener(polling) } @@ -149,27 +149,6 @@ where assert_update_listener(builder.build()) } -async fn delete_webhook_if_setup(requester: &R) -where - R: Requester, -{ - let webhook_info = match requester.get_webhook_info().send().await { - Ok(ok) => ok, - Err(e) => { - log::error!("Failed to get webhook info: {:?}", e); - return; - } - }; - - let is_webhook_setup = webhook_info.url.is_some(); - - if is_webhook_setup { - if let Err(e) = requester.delete_webhook().send().await { - log::error!("Failed to delete a webhook: {:?}", e); - } - } -} - #[cfg_attr(doc, aquamarine::aquamarine)] /// A polling update listener. /// @@ -248,6 +227,7 @@ pub struct Polling { limit: Option, allowed_updates: Option>, drop_pending_updates: bool, + delete_webhook: bool, flag: Option, token: StopToken, } @@ -268,6 +248,7 @@ where limit: None, allowed_updates: None, drop_pending_updates: false, + delete_webhook: false, } } @@ -327,6 +308,16 @@ impl UpdateListener for Polling { // and you need to call `stop_token` *again* after it self.reinit_stop_flag_if_needed(); + if self.delete_webhook { + let webhook_info = self.bot.get_webhook_info().send().await?; + + let is_webhook_setup = webhook_info.url.is_some(); + + if is_webhook_setup { + self.bot.delete_webhook().send().await?; + } + } + if self.drop_pending_updates { self.bot.get_updates().offset(-1).limit(1).timeout(0).await?; }