Allow notifications to be sent days ahead of time
This commit is contained in:
64
src/main.rs
64
src/main.rs
@@ -10,7 +10,7 @@ use std::{env, fs::File, io::BufReader, sync::Arc};
|
||||
use anyhow::Result;
|
||||
use async_mutex::Mutex;
|
||||
use bot::fetch_and_announce_appointment;
|
||||
use chrono::{DateTime, NaiveTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, Days, NaiveTime, TimeZone, Utc};
|
||||
use chrono_tz::Europe;
|
||||
use db::ChatInfo;
|
||||
use diesel::result::Error::NotFound;
|
||||
@@ -41,6 +41,8 @@ pub struct Config {
|
||||
poll_interval: u64,
|
||||
#[serde(deserialize_with = "deserialize_time")]
|
||||
reminder_time: NaiveTime,
|
||||
#[serde(deserialize_with = "deserialize_time")]
|
||||
preceeding_day_reminder_time: NaiveTime,
|
||||
}
|
||||
|
||||
fn deserialize_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<NaiveTime, D::Error> {
|
||||
@@ -79,7 +81,7 @@ async fn main() {
|
||||
db.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
let db = Arc::new(Mutex::new(db));
|
||||
|
||||
let bot = Bot::new(config.token).throttle(Limits::default());
|
||||
let bot = Bot::new(&config.token).throttle(Limits::default());
|
||||
|
||||
{
|
||||
let db = db.clone();
|
||||
@@ -104,16 +106,18 @@ async fn main() {
|
||||
Err(e) => Err(e).unwrap(),
|
||||
};
|
||||
|
||||
let next_appointment =
|
||||
next_appointment.map(|timestamp| Utc.timestamp_opt(timestamp, 0).unwrap());
|
||||
|
||||
let sleep_duration = next_appointment
|
||||
.map(|timestamp| Utc.timestamp_opt(timestamp, 0).unwrap())
|
||||
.map(|date_time| date_time - now)
|
||||
.map(|next_appointment| next_appointment - now)
|
||||
.map(|duration| duration.to_std().unwrap())
|
||||
.filter(|duration| *duration < poll_duration)
|
||||
.unwrap_or(poll_duration);
|
||||
|
||||
sleep(sleep_duration).await;
|
||||
|
||||
let result = check_task(&bot, config.reminder_time, &db).await;
|
||||
let result = check_task(&bot, &config, &db).await;
|
||||
if let Err(e) = result {
|
||||
error!("{}\nBacktrace:\n{}", e, e.backtrace());
|
||||
}
|
||||
@@ -131,14 +135,15 @@ struct Reminder<Tz: TimeZone> {
|
||||
text: String,
|
||||
}
|
||||
|
||||
async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database) -> Result<()> {
|
||||
// Checks if the date of the next appointment has changed (and announces if so)
|
||||
// Additionally, checks if it is time for a reminder and sends that reminder if necessary
|
||||
async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Result<()> {
|
||||
let chats = db.lock().await.transaction(|db| {
|
||||
use schema::chat::dsl::*;
|
||||
chat.load::<DbChat>(db)
|
||||
})?;
|
||||
|
||||
let now = Utc::now().with_timezone(&Europe::Berlin);
|
||||
let today = now.date_naive();
|
||||
|
||||
for chat in chats {
|
||||
let mut chat_info = ChatInfo::from(chat);
|
||||
@@ -150,10 +155,6 @@ async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database
|
||||
};
|
||||
let appointment = appointment.with_timezone(&Europe::Berlin);
|
||||
|
||||
if appointment.start.date_naive() != today {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut reminder = None;
|
||||
if now >= appointment.start {
|
||||
reminder = Some(Reminder {
|
||||
@@ -161,24 +162,39 @@ async fn check_task(bot: &Throttle<Bot>, reminder_time: NaiveTime, db: &Database
|
||||
text: t!("messages.starting_now", locale = &chat_info.locale),
|
||||
});
|
||||
} else {
|
||||
let reminder_date_time = now
|
||||
.date_naive()
|
||||
.and_time(reminder_time)
|
||||
let reminder_day =
|
||||
appointment.start.date_naive() - Days::new(chat_info.remind_days_ahead);
|
||||
let reminder_date_time = if chat_info.remind_days_ahead == 0 {
|
||||
reminder_day.and_time(config.reminder_time)
|
||||
} else {
|
||||
reminder_day.and_time(config.preceeding_day_reminder_time)
|
||||
};
|
||||
let reminder_date_time = reminder_date_time
|
||||
.and_local_timezone(now.timezone())
|
||||
.unwrap();
|
||||
if now >= reminder_date_time {
|
||||
// TODO This can have weird effects if it's happenig around midnight, since it's not timezone aware (and may even mix multiple timezones)
|
||||
let remaining_time = appointment.start.date_naive() - now.date_naive();
|
||||
let remaining_days = remaining_time.num_days();
|
||||
let message_id = match remaining_days {
|
||||
0 => "messages.appointment_today",
|
||||
1 => "messages.appointment_tomorrow",
|
||||
_ => "messages.appointment_soon",
|
||||
};
|
||||
let reminder_text = t!(
|
||||
message_id,
|
||||
locale = &chat_info.locale,
|
||||
no_days = remaining_days,
|
||||
start_time = &appointment.start.format("%H:%M").to_string(),
|
||||
uk_time = &appointment
|
||||
.start
|
||||
.with_timezone(&Europe::London)
|
||||
.format("%H:%M")
|
||||
.to_string()
|
||||
);
|
||||
reminder = Some(Reminder {
|
||||
time: reminder_date_time,
|
||||
text: t!(
|
||||
"messages.appointment_today",
|
||||
locale = &chat_info.locale,
|
||||
start_time = &appointment.start.format("%H:%M").to_string(),
|
||||
uk_time = &appointment
|
||||
.start
|
||||
.with_timezone(&Europe::London)
|
||||
.format("%H:%M")
|
||||
.to_string()
|
||||
),
|
||||
text: reminder_text,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user