From 856a1efa04abb222dbf30e2051303e6f772ffeb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Mon, 24 Oct 2022 12:24:15 +0200 Subject: [PATCH] Better error handling if pinning/unpinning the announcement message fails --- src/bot.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 4612a84..05c0eb6 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -163,6 +163,24 @@ pub async fn fetch_and_announce_appointment( ) .await?; + if let Some(pinned_message_id) = entry.pinned_message_id { + let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id)); + unpin_message.message_id = Some(MessageId(pinned_message_id)); + _ = unpin_message.await; + } + + let mut pin_message = bot.pin_chat_message(announcement.chat.id, announcement.id); + pin_message.disable_notification = Some(false); + let pin_sucessful = pin_message.await.is_ok(); + + chat_info.next_appointment = Some(appointment.clone()); + chat_info.last_reminder = Some(now); + chat_info.pinned_message_id = if pin_sucessful { + Some(announcement.id.0) + } else { + None + }; + db.lock().await.transaction(|db| { use schema::chat::dsl::*; diesel::update(chat.filter(telegram_id.eq(chat_info.id))) @@ -170,24 +188,10 @@ pub async fn fetch_and_announce_appointment( next_appointment_start.eq(appointment.start.timestamp()), next_appointment_end.eq(appointment.end.timestamp()), last_reminder.eq(now.timestamp()), - pinned_message_id.eq(announcement.id.0), + pinned_message_id.eq(chat_info.pinned_message_id), )) .execute(db) })?; - chat_info.next_appointment = Some(appointment); - chat_info.last_reminder = Some(now); - chat_info.pinned_message_id = Some(announcement.id.0); - - if let Some(pinned_message_id) = entry.pinned_message_id { - let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id)); - unpin_message.message_id = Some(MessageId(pinned_message_id)); - unpin_message.await?; - } - - let mut pin_message = bot.pin_chat_message(announcement.chat.id, announcement.id); - pin_message.disable_notification = Some(false); - pin_message.await?; - Ok(()) }