If a group migrates to a supergroup, update to the new id in the db and re-send the announcement

This commit is contained in:
Manuel Vögele
2024-10-27 16:55:50 +01:00
parent 2041853a83
commit b4dc2ce561

View File

@@ -10,6 +10,7 @@ use diesel::ExpressionMethods;
use diesel::QueryDsl; use diesel::QueryDsl;
use diesel::RunQueryDsl; use diesel::RunQueryDsl;
use itertools::Itertools; use itertools::Itertools;
use log::info;
use strum::Display; use strum::Display;
use strum::EnumIter; use strum::EnumIter;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
@@ -28,7 +29,7 @@ use teloxide::types::MessageId;
use teloxide::types::{ChatId, ReplyParameters}; use teloxide::types::{ChatId, ReplyParameters};
use teloxide::types::{Message, Update}; use teloxide::types::{Message, Update};
use teloxide::utils::command::BotCommands; use teloxide::utils::command::BotCommands;
use teloxide::Bot; use teloxide::{Bot, RequestError};
use crate::appointment::fetch_next_appointment; use crate::appointment::fetch_next_appointment;
use crate::db::{ChatInfo, Reminder}; use crate::db::{ChatInfo, Reminder};
@@ -332,9 +333,9 @@ pub async fn fetch_and_announce_appointment(
.to_string(), .to_string(),
}; };
let announcement = bot let send_announcement = |chat_id| {
.send_message( bot.send_message(
ChatId(chat_info.id), ChatId(chat_id),
t!( t!(
"messages.next_appointment", "messages.next_appointment",
locale = &chat_info.locale, locale = &chat_info.locale,
@@ -343,7 +344,25 @@ pub async fn fetch_and_announce_appointment(
uk_time = &uk_time_str.to_string() uk_time = &uk_time_str.to_string()
), ),
) )
.await?; };
let mut announcement = send_announcement(chat_info.id).await;
if let Err(e) = &announcement {
if let RequestError::MigrateToChatId(new_id) = e {
db.lock().await.exclusive_transaction(|db| {
use schema::chats::dsl::*;
info!("Migrating chat {} to {}", chat_info.id, new_id.0);
diesel::update(chats)
.filter(telegram_id.eq(chat_info.id))
.set(telegram_id.eq(new_id.0))
.execute(db)
})?;
announcement = send_announcement(new_id.0).await;
}
}
let announcement = announcement?;
if let Some(pinned_message_id) = entry.pinned_message_id { if let Some(pinned_message_id) = entry.pinned_message_id {
let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id)); let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id));