diff --git a/migrations/2023-01-25-220630_localization/down.sql b/migrations/2023-01-25-220630_localization/down.sql new file mode 100644 index 0000000..3cd7835 --- /dev/null +++ b/migrations/2023-01-25-220630_localization/down.sql @@ -0,0 +1 @@ +ALTER TABLE chat DROP locale; \ No newline at end of file diff --git a/migrations/2023-01-25-220630_localization/up.sql b/migrations/2023-01-25-220630_localization/up.sql new file mode 100644 index 0000000..fa8f873 --- /dev/null +++ b/migrations/2023-01-25-220630_localization/up.sql @@ -0,0 +1 @@ +ALTER TABLE chat ADD locale TEXT; \ No newline at end of file diff --git a/src/bot.rs b/src/bot.rs index 7f72747..ef62a28 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -31,6 +31,7 @@ use crate::{schema, Database}; pub enum Command { #[command()] SetCalendar, + SetLocale, } pub async fn spawn(bot: Throttle, db: Database) { @@ -43,7 +44,8 @@ pub async fn spawn(bot: Throttle, db: Database) { fn build_handler_chain() -> UpdateHandler { let command_handler = teloxide::filter_command::() - .branch(case![Command::SetCalendar].endpoint(set_calendar)); + .branch(case![Command::SetCalendar].endpoint(set_calendar)) + .branch(case![Command::SetLocale].endpoint(set_locale)); let my_chat_member_handler = Update::filter_my_chat_member().endpoint(handle_my_chat_member); @@ -93,6 +95,7 @@ async fn set_calendar(bot: Throttle, msg: Message, db: Database) -> Result< next_appointment: None, last_reminder: None, pinned_message_id: None, + locale: "de".into(), }; fetch_and_announce_appointment(&bot, &mut chat_info, &db).await?; @@ -100,6 +103,26 @@ async fn set_calendar(bot: Throttle, msg: Message, db: Database) -> Result< Ok(()) } +async fn set_locale(_bot: Throttle, msg: Message, db: Database) -> Result<(), Error> { + let new_locale = msg.text().map(|text| text.splitn(2, " ").nth(1)).flatten(); + + if new_locale.is_none() { + return Ok(()); + } + let new_locale = new_locale.unwrap(); + + db.lock().await.transaction::<_, Error, _>(|db| { + use schema::chat::dsl::*; + diesel::update(chat) + .filter(telegram_id.eq(msg.chat.id.0)) + .set(locale.eq(new_locale)) + .execute(db)?; + Ok(()) + })?; + + Ok(()) +} + pub async fn fetch_and_announce_appointment( bot: &Throttle, chat_info: &mut ChatInfo, @@ -146,14 +169,22 @@ pub async fn fetch_and_announce_appointment( .with_timezone(&Europe::Berlin) .format("%d.%m.%Y %H:%M"); - let weekday = match appointment.start.weekday() { - chrono::Weekday::Mon => "Montag", - chrono::Weekday::Tue => "Dienstag", - chrono::Weekday::Wed => "Mittwoch", - chrono::Weekday::Thu => "Donnerstag", - chrono::Weekday::Fri => "Freitag", - chrono::Weekday::Sat => "Samstag", - chrono::Weekday::Sun => "Sonntag", + let weekday = match chat_info.locale.as_str() { + "de" => match appointment.start.weekday() { + chrono::Weekday::Mon => "Montag", + chrono::Weekday::Tue => "Dienstag", + chrono::Weekday::Wed => "Mittwoch", + chrono::Weekday::Thu => "Donnerstag", + chrono::Weekday::Fri => "Freitag", + chrono::Weekday::Sat => "Samstag", + chrono::Weekday::Sun => "Sonntag", + } + .to_owned(), + _ => appointment + .start + .with_timezone(&Europe::Berlin) + .format("%A") + .to_string(), }; let announcement = bot @@ -161,7 +192,8 @@ pub async fn fetch_and_announce_appointment( ChatId(chat_info.id), t!( "messages.next_appointment", - weekday = weekday, + locale = &chat_info.locale, + weekday = &weekday, date = &date_str.to_string() ), ) diff --git a/src/db.rs b/src/db.rs index 9c225fb..f35d10c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -12,6 +12,7 @@ pub struct DbChat { next_appointment_end: Option, last_reminder: Option, pinned_message: Option, + locale: Option, } pub struct ChatInfo { @@ -20,6 +21,7 @@ pub struct ChatInfo { pub next_appointment: Option>, pub last_reminder: Option>, pub pinned_message_id: Option, + pub locale: String, } impl From for ChatInfo { @@ -42,12 +44,15 @@ impl From for ChatInfo { .map(|timestamp| NaiveDateTime::from_timestamp(timestamp, 0)) .map(|date_time| DateTime::::from_utc(date_time, Utc)); + let locale = db_chat.locale.unwrap_or("de".into()); + ChatInfo { id: db_chat.telegram_id, calendar: db_chat.calendar, next_appointment: next_appointment, last_reminder, pinned_message_id: db_chat.pinned_message, + locale, } } } diff --git a/src/main.rs b/src/main.rs index 8b51cc0..51b7a3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,7 +157,7 @@ async fn check_task(bot: &Throttle, reminder_time: NaiveTime, db: &Database if now >= appointment.start { reminder = Some(Reminder { time: appointment.start, - text: t!("messages.starting_now"), + text: t!("messages.starting_now", locale = &chat_info.locale), }); } else { let reminder_date_time = now.date().and_time(reminder_time).unwrap(); @@ -166,6 +166,7 @@ async fn check_task(bot: &Throttle, reminder_time: NaiveTime, db: &Database time: reminder_date_time, text: t!( "messages.appointment_today", + locale = &chat_info.locale, start_time = &appointment.start.format("%H:%M").to_string() ), }) diff --git a/src/schema.rs b/src/schema.rs index 5d288a6..ee13523 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -9,5 +9,6 @@ diesel::table! { next_appointment_end -> Nullable, last_reminder -> Nullable, pinned_message_id -> Nullable, + locale -> Nullable, } }