Skip to content

Commit

Permalink
allow toggling between diff update and full updates; bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
fnschmidt committed Nov 8, 2024
1 parent c4bb0e3 commit 63c1a8a
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 105 deletions.
453 changes: 361 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ regex-lite = "0.1.5"
reqwest = { version = "0.12.2", features = ["cookies", "json"] }
reqwest-websocket = "0.4.2"
rusqlite = { version = "0.32.1" }
scraper = "0.20.0"
scraper = "0.21.0"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
static_init = "1.0.3"
teloxide = { version = "0.13.0", features = ["macros"] }
teloxide-core = "0.10.1"
thiserror = "1.0.56"
thiserror = "2.0.0"
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros"] }
tokio-cron-scheduler = "0.13.0"
uuid = "1.7.0"
Expand Down
5 changes: 3 additions & 2 deletions src/bin/mensi-telegram-bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use stuwe_telegram_rs::data_backend::mm_parser::get_mensen;
use stuwe_telegram_rs::data_types::CampusDualData;

use stuwe_telegram_rs::bot_command_handlers::{
allergene, change_mensa, day_cmd, invalid_cmd, reply_time_dialogue, show_different_mensa,
start, start_time_dialogue, subscribe, unsubscribe,
allergene, change_mensa, day_cmd, invalid_cmd, reply_time_dialogue, senddiff,
show_different_mensa, start, start_time_dialogue, subscribe, unsubscribe,
};
use stuwe_telegram_rs::constants::{
BACKEND, CD_DATA, DB_FILENAME, MENSI_DB, OLLAMA_HOST, OLLAMA_MODEL, USER_REGISTRATIONS,
Expand Down Expand Up @@ -138,6 +138,7 @@ fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>>
.branch(dptree::case![Command::Unsubscribe].endpoint(unsubscribe))
.branch(dptree::case![Command::Mensa].endpoint(change_mensa))
.branch(dptree::case![Command::Allergene].endpoint(allergene))
.branch(dptree::case![Command::Diff].endpoint(senddiff))
.branch(dptree::case![Command::Uhrzeit].endpoint(start_time_dialogue));

let message_handler = Update::filter_message()
Expand Down
5 changes: 3 additions & 2 deletions src/bin/stuwe-telegram-bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use stuwe_telegram_rs::data_types::CampusDualData;

use stuwe_telegram_rs::bot_command_handlers::{
allergene, change_mensa, day_cmd, invalid_cmd, reply_time_dialogue, show_different_mensa,
start, start_time_dialogue, subscribe, unsubscribe,
allergene, change_mensa, day_cmd, invalid_cmd, reply_time_dialogue, senddiff,
show_different_mensa, start, start_time_dialogue, subscribe, unsubscribe,
};
use stuwe_telegram_rs::constants::{
API_URL, BACKEND, CD_DATA, DB_FILENAME, OLLAMA_HOST, OLLAMA_MODEL, STUWE_DB, USER_REGISTRATIONS,
Expand Down Expand Up @@ -146,6 +146,7 @@ fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>>
.branch(dptree::case![Command::Unsubscribe].endpoint(unsubscribe))
.branch(dptree::case![Command::Mensa].endpoint(change_mensa))
.branch(dptree::case![Command::Allergene].endpoint(allergene))
.branch(dptree::case![Command::Diff].endpoint(senddiff))
.branch(dptree::case![Command::Uhrzeit].endpoint(start_time_dialogue));

let message_handler = Update::filter_message()
Expand Down
30 changes: 30 additions & 0 deletions src/bot_command_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,36 @@ pub async fn allergene(bot: Bot, msg: Message) -> HandlerResult {
Ok(())
}

pub async fn senddiff(bot: Bot, msg: Message) -> HandlerResult {
if let Some(mut registration) = get_user_registration(msg.chat.id.0) {
registration.senddiff = !registration.senddiff;

set_user_allergen_state(msg.chat.id.0, registration.senddiff)?;
insert_user_registration(msg.chat.id.0, registration);

match registration.senddiff {
true => {
bot.send_message(
msg.chat.id,
"✅ Nur Änderungen werden bei Planänderung gesendet.",
)
.await?;
}
false => {
bot.send_message(
msg.chat.id,
"❌ Gesamter Plan wird bei Änderungen gesendet.",
)
.await?;
}
}
} else {
bot.send_message(msg.chat.id, NO_DB_MSG).await?;
}

Ok(())
}

pub async fn start_time_dialogue(
bot: Bot,
msg: Message,
Expand Down
3 changes: 3 additions & 0 deletions src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum Command {
Uhrzeit,
#[command(hide)]
Allergene,
#[command(description = "Bei Planänderungen nur Unterschied schicken")]
Diff,
#[command(hide)]
Start,
}
Expand Down Expand Up @@ -160,6 +162,7 @@ pub struct RegistrationEntry {
pub hour: Option<u32>,
pub minute: Option<u32>,
pub allergens: bool,
pub senddiff: bool,
}

#[derive(Error, Debug, Clone)]
Expand Down
27 changes: 26 additions & 1 deletion src/db_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub fn check_or_create_db_tables() -> rusqlite::Result<()> {
mensa_id integer not null,
hour integer,
minute integer,
allergens BOOLEAN
allergens BOOLEAN DEFAULT 1,
senddiff BOOLEAN DEFAULT 1
)",
)?
.execute([])?;
Expand Down Expand Up @@ -142,3 +143,27 @@ pub fn set_user_allergen_state(chat_id: i64, state: bool) -> rusqlite::Result<()

Ok(())
}

pub fn get_user_senddiff_state(chat_id: i64) -> rusqlite::Result<bool> {
let conn = Connection::open(DB_FILENAME.get().unwrap()).unwrap();

let mut stmt = conn.prepare_cached(
"select senddiff from registrations
where chat_id = ?1",
)?;

let diff: bool = stmt.query_row(params![chat_id], |row| row.get(0))?;

Ok(diff)
}

pub fn set_user_senddiff_state(chat_id: i64, state: bool) -> rusqlite::Result<()> {
let conn = Connection::open(DB_FILENAME.get().unwrap()).unwrap();

let mut stmt =
conn.prepare_cached("update registrations set senddiff = ?2 where chat_id = ?1")?;

stmt.execute(params![chat_id, state])?;

Ok(())
}
5 changes: 3 additions & 2 deletions src/shared_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ pub fn make_commands_keyrow() -> KeyboardMarkup {
vec![
KeyboardButton::new("/heute"),
KeyboardButton::new("/morgen"),
KeyboardButton::new("/übermorgen"),
KeyboardButton::new("/andere"),
// KeyboardButton::new("/übermorgen"),
],
vec![
KeyboardButton::new("/andere"),
KeyboardButton::new("/mensa"),
KeyboardButton::new("/allergene"),
KeyboardButton::new("/diff"),
],
];
KeyboardMarkup::new(keyboard).resize_keyboard()
Expand Down
17 changes: 13 additions & 4 deletions src/task_scheduler_funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use crate::{
save_campusdual_grades, save_campusdual_signup_options,
},
constants::{API_URL, BACKEND, CD_DATA, NO_DB_MSG, USER_REGISTRATIONS},
data_backend::stuwe_parser::stuwe_build_diff_msg,
data_backend::stuwe_parser::{stuwe_build_diff_msg, stuwe_build_meal_msg},
data_types::{
Backend, BroadcastUpdateTask, JobHandlerTask, RegistrationEntry, UpdateRegistrationTask,
},
db_operations::{
get_all_user_registrations_db, get_user_allergen_state, init_db_record, task_db_kill_auto,
update_db_row,
get_all_user_registrations_db, get_user_allergen_state, get_user_senddiff_state,
init_db_record, task_db_kill_auto, update_db_row,
},
shared_main::{get_user_registration, insert_user_registration, load_job},
};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub async fn handle_add_registration_task(
hour: job_handler_task.hour,
minute: job_handler_task.minute,
allergens: registration.map(|reg| reg.allergens).unwrap_or(true),
senddiff: registration.map(|reg| reg.senddiff).unwrap_or(true),
},
);
}
Expand Down Expand Up @@ -119,6 +120,7 @@ pub async fn handle_update_registration_task(
hour,
minute,
allergens: registration.allergens,
senddiff: registration.senddiff,
},
);

Expand Down Expand Up @@ -157,6 +159,7 @@ pub async fn handle_delete_registration_task(
hour: None,
minute: None,
allergens: registration.allergens,
senddiff: registration.senddiff,
},
);

Expand Down Expand Up @@ -213,7 +216,12 @@ pub async fn handle_broadcast_update_task(bot: &Bot, job_handler_task: JobHandle

log::info!("Sent update to {}", chat_id);

let text = stuwe_build_diff_msg(&diff, registration_data.allergens).await;
let text = match registration_data.senddiff {
true => stuwe_build_diff_msg(&diff, registration_data.allergens).await,
false => {
stuwe_build_meal_msg(0, diff.canteen_id, registration_data.allergens).await
}
};

bot.send_message(ChatId(chat_id), text)
.parse_mode(ParseMode::MarkdownV2)
Expand Down Expand Up @@ -345,6 +353,7 @@ pub async fn load_jobs_from_db(
minute: task.minute,
// hack job
allergens: get_user_allergen_state(task.chat_id.unwrap()).unwrap(),
senddiff: get_user_senddiff_state(task.chat_id.unwrap()).unwrap(),
},
);
}
Expand Down

0 comments on commit 63c1a8a

Please sign in to comment.