Browse Source

feat: keep track of both days

master
Timo Kösters 8 months ago
parent
commit
c1a7d98a43
3 changed files with 53 additions and 25 deletions
  1. +1
    -1
      Cargo.lock
  2. +15
    -6
      src/bot.rs
  3. +37
    -18
      src/handler.rs

+ 1
- 1
Cargo.lock View File

@@ -1628,7 +1628,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vertretungsplan"
version = "0.1.0"
source = "git+https://git.koesters.xyz/timo/vertretungsplan.git#558c92a1955aa1c87f2ea298ebde4f69b704d5b3"
source = "git+https://git.koesters.xyz/timo/vertretungsplan.git#342dcf8d28b140338dcb49a14c014ca8e25e6e90"
dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",


+ 15
- 6
src/bot.rs View File

@@ -10,7 +10,7 @@ use ruma_events::{
};
use std::time::{Duration, Instant};
use url::Url;
use vertretungsplan::Vertretungsplan;
use vertretungsplan::{Tag, Vertretungsplan};

pub async fn start_bot(homeserver_url: Url, save: &mut Save) {
bot(homeserver_url, save).await.unwrap();
@@ -19,7 +19,8 @@ pub async fn start_bot(homeserver_url: Url, save: &mut Save) {
pub async fn bot(homeserver_url: Url, save: &mut Save) -> Result<()> {
// Connect
let client = Client::https(homeserver_url, save.session.clone());
let mut vertretungsplan = Vertretungsplan::download().await?;
let mut vertretungsplan1 = Vertretungsplan::download(Tag::Aktuell).await?;
let mut vertretungsplan2 = Vertretungsplan::download(Tag::Naechster).await?;

// Log in if necessary
if save.session.is_none() {
@@ -61,7 +62,8 @@ pub async fn bot(homeserver_url: Url, save: &mut Save) -> Result<()> {
room_id,
&client,
save,
&mut vertretungsplan,
&mut vertretungsplan1,
&mut vertretungsplan2,
)
.await?;
save.active_room = Some(room_id.clone());
@@ -79,11 +81,18 @@ pub async fn bot(homeserver_url: Url, save: &mut Save) -> Result<()> {
}

// Maintain vertretungsplan (update if outdated)
let changed = vertretungsplan
let changed1 = vertretungsplan1
.maintain(Duration::from_secs(60 * 5))
.await?;
if changed {
handler::handle_new_vertretungsplan(&client, save, &mut vertretungsplan).await?;
if changed1 {
handler::handle_new_vertretungsplan(&client, save, &mut vertretungsplan1).await?;
}

let changed2 = vertretungsplan2
.maintain(Duration::from_secs(60 * 5))
.await?;
if changed2 {
handler::handle_new_vertretungsplan(&client, save, &mut vertretungsplan2).await?;
}
}
}

+ 37
- 18
src/handler.rs View File

@@ -15,28 +15,48 @@ pub async fn handle_text_message(
room_id: &RoomId,
client: &HttpsClient,
save: &mut Save,
vertretungsplan: &mut Vertretungsplan,
vertretungsplan1: &mut Vertretungsplan,
vertretungsplan2: &mut Vertretungsplan,
) -> Result<()> {
println!("{} in {}: {}", sender, room_id, body);
if body.starts_with("!v ") {
let klasse = body.trim_start_matches("!v ");

let body = formatter::raw_vertretungsplan(&vertretungsplan, klasse);
let formatted_body = Some(formatter::html_vertretungsplan(&vertretungsplan, klasse));
let body = formatter::raw_vertretungsplan(&vertretungsplan1, klasse);
let formatted_body = Some(formatter::html_vertretungsplan(&vertretungsplan1, klasse));

client
.request(create_message_event::Request {
room_id: room_id.clone(),
event_type: EventType::RoomMessage,
txn_id: save.next_txn_id(),
data: MessageEventContent::Text(TextMessageEventContent {
body,
format: Some("org.matrix.custom.html".to_owned()),
formatted_body,
relates_to: None,
}),
})
.await?;
} else if body.starts_with("!v2 ") {
let klasse = body.trim_start_matches("!v2 ");

client
.request(create_message_event::Request {
room_id: room_id.clone(),
event_type: EventType::RoomMessage,
txn_id: save.next_txn_id(),
data: MessageEventContent::Text(TextMessageEventContent {
body,
format: Some("org.matrix.custom.html".to_owned()),
formatted_body,
relates_to: None,
}),
})
.await?;
let body = formatter::raw_vertretungsplan(&vertretungsplan2, klasse);
let formatted_body = Some(formatter::html_vertretungsplan(&vertretungsplan2, klasse));

client
.request(create_message_event::Request {
room_id: room_id.clone(),
event_type: EventType::RoomMessage,
txn_id: save.next_txn_id(),
data: MessageEventContent::Text(TextMessageEventContent {
body,
format: Some("org.matrix.custom.html".to_owned()),
formatted_body,
relates_to: None,
}),
})
.await?;
}
Ok(())
}
@@ -46,8 +66,7 @@ pub async fn handle_new_vertretungsplan(
save: &mut Save,
vertretungsplan: &mut Vertretungsplan,
) -> Result<()> {
if let Some(active_room) = save.active_room.clone()
{
if let Some(active_room) = save.active_room.clone() {
client
.request(create_message_event::Request {
room_id: active_room,


Loading…
Cancel
Save