Allow granular per-chat reminder configuration
This commit is contained in:
80
src/main.rs
80
src/main.rs
@@ -5,13 +5,12 @@ mod error;
|
||||
mod schema;
|
||||
|
||||
use std::env::args;
|
||||
use std::time::Duration;
|
||||
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, Days, NaiveTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, Duration, TimeZone, Utc};
|
||||
use chrono_tz::Europe;
|
||||
use db::ChatInfo;
|
||||
use diesel::result::Error::{self, NotFound};
|
||||
@@ -22,7 +21,7 @@ use diesel::{ExpressionMethods, QueryDsl};
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use error::ConfigLoadError;
|
||||
use log::*;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::Deserialize;
|
||||
use teloxide::adaptors::Throttle;
|
||||
use teloxide::prelude::RequesterExt;
|
||||
use teloxide::requests::Requester;
|
||||
@@ -42,16 +41,7 @@ i18n!("locales");
|
||||
pub struct Config {
|
||||
token: String,
|
||||
data_path: String,
|
||||
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> {
|
||||
let s: String = Deserialize::deserialize(deserializer)?;
|
||||
NaiveTime::parse_from_str(&s, "%H:%M").map_err(serde::de::Error::custom)
|
||||
poll_interval: i64,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -96,7 +86,7 @@ async fn main() {
|
||||
{
|
||||
let db = db.clone();
|
||||
let bot = bot.clone();
|
||||
let poll_duration = Duration::from_secs(config.poll_interval);
|
||||
let poll_duration = Duration::seconds(config.poll_interval);
|
||||
tokio::task::spawn(async move {
|
||||
loop {
|
||||
let now = Utc::now();
|
||||
@@ -122,13 +112,13 @@ async fn main() {
|
||||
|
||||
let sleep_duration = next_appointment
|
||||
.map(|next_appointment| next_appointment - now)
|
||||
.map(|duration| duration.to_std().unwrap())
|
||||
.map(|duration| duration)
|
||||
.filter(|duration| *duration < poll_duration)
|
||||
.unwrap_or(poll_duration);
|
||||
|
||||
sleep(sleep_duration).await;
|
||||
sleep(sleep_duration.to_std().unwrap()).await;
|
||||
|
||||
let result = check_task(&bot, &config, &db).await;
|
||||
let result = check_task(&bot, &db).await;
|
||||
if let Err(e) = result {
|
||||
error!("{}\nBacktrace:\n{}", e, e.backtrace());
|
||||
}
|
||||
@@ -136,27 +126,25 @@ async fn main() {
|
||||
});
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||
|
||||
bot::spawn(bot, db).await;
|
||||
}
|
||||
|
||||
struct Reminder<Tz: TimeZone> {
|
||||
struct ReminderMessage<Tz: TimeZone> {
|
||||
time: DateTime<Tz>,
|
||||
text: String,
|
||||
}
|
||||
|
||||
// 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<()> {
|
||||
async fn check_task(bot: &Throttle<Bot>, db: &Database) -> Result<()> {
|
||||
let chats = db.lock().await.transaction::<_, Error, _>(|db| {
|
||||
use schema::chats::dsl::*;
|
||||
use schema::reminders::dsl::*;
|
||||
let db_chats = chats.load::<DbChat>(db)?;
|
||||
|
||||
let db_reminders: Vec<DbReminder> = DbReminder::belonging_to(&db_chats)
|
||||
.select(DbReminder::as_select())
|
||||
.order(days_ahead.asc())
|
||||
.load(db)?;
|
||||
|
||||
let reminders_per_chat = db_reminders
|
||||
@@ -171,7 +159,7 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
|
||||
let now = Utc::now().with_timezone(&Europe::Berlin);
|
||||
|
||||
for (reminders, chat) in chats {
|
||||
let mut chat_info = ChatInfo::from_db(chat, reminders);
|
||||
let mut chat_info = ChatInfo::from_db(chat, reminders)?;
|
||||
fetch_and_announce_appointment(bot, &mut chat_info, db).await?;
|
||||
|
||||
let appointment = match chat_info.next_appointment {
|
||||
@@ -182,34 +170,45 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
|
||||
|
||||
let mut reminder = None;
|
||||
if now >= appointment.start {
|
||||
reminder = Some(Reminder {
|
||||
reminder = Some(ReminderMessage {
|
||||
time: appointment.start,
|
||||
text: t!("messages.starting_now", locale = &chat_info.locale),
|
||||
});
|
||||
} else {
|
||||
// This assumes that remind_days_ahead is sorted in ascending order
|
||||
let most_recent_active_reminder = chat_info
|
||||
.remind_days_ahead
|
||||
.reminders
|
||||
.iter()
|
||||
.map(|days_ahead| {
|
||||
let reminder_day = appointment.start.date_naive() - Days::new(*days_ahead);
|
||||
let reminder_date_time = if *days_ahead == 0 {
|
||||
reminder_day.and_time(config.reminder_time)
|
||||
.map(|reminder| {
|
||||
if let Some(reminder_time) = reminder.time {
|
||||
let reminder_day = appointment.start.date_naive() - reminder.delta;
|
||||
let reminder_date_time = reminder_day.and_time(reminder_time);
|
||||
reminder_date_time
|
||||
.and_local_timezone(now.timezone())
|
||||
.unwrap()
|
||||
} else {
|
||||
reminder_day.and_time(config.preceeding_day_reminder_time)
|
||||
};
|
||||
reminder_date_time
|
||||
.and_local_timezone(now.timezone())
|
||||
.unwrap()
|
||||
appointment.start - reminder.delta
|
||||
}
|
||||
})
|
||||
.find(|reminder_datetime| now >= *reminder_datetime);
|
||||
.filter(|reminder_datetime| now >= *reminder_datetime)
|
||||
.max();
|
||||
|
||||
if let Some(reminder_date_time) = most_recent_active_reminder {
|
||||
// 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 remaining_days = appointment.start.date_naive() - now.date_naive();
|
||||
let remaining_days = remaining_days.num_days();
|
||||
let remaining_hours = appointment.start - now;
|
||||
// Add 15 Minutes to ensure we aren't reporting too few hours because we're missing a few minutes (or even seconds) due to a delay by the bot
|
||||
let remaining_hours = (remaining_hours + Duration::minutes(15)).num_hours();
|
||||
let message_id = match remaining_days {
|
||||
0 => "messages.appointment_today",
|
||||
0 => {
|
||||
if remaining_hours > 6 {
|
||||
"messages.appointment_today"
|
||||
} else if remaining_hours == 1 {
|
||||
"messages.appointment_hours.one"
|
||||
} else {
|
||||
"messages.appointment_hours.other"
|
||||
}
|
||||
}
|
||||
1 => "messages.appointment_tomorrow",
|
||||
_ => "messages.appointment_soon",
|
||||
};
|
||||
@@ -217,6 +216,7 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
|
||||
message_id,
|
||||
locale = &chat_info.locale,
|
||||
no_days = remaining_days,
|
||||
no_hours = remaining_hours,
|
||||
start_time = &appointment.start.format("%H:%M").to_string(),
|
||||
uk_time = &appointment
|
||||
.start
|
||||
@@ -224,7 +224,7 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
|
||||
.format("%H:%M")
|
||||
.to_string()
|
||||
);
|
||||
reminder = Some(Reminder {
|
||||
reminder = Some(ReminderMessage {
|
||||
time: reminder_date_time,
|
||||
text: reminder_text,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user