From 4c2d78c6bb4268fee522941a369ae4d833b02c2d Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu@web.de> Date: Sat, 23 May 2020 17:04:00 +0200 Subject: [PATCH] Send json data about events to Trilium --- src/ical_parsing.rs | 14 +++++++++++++- src/main.rs | 27 ++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/ical_parsing.rs b/src/ical_parsing.rs index 8e51a1f..c7e3995 100644 --- a/src/ical_parsing.rs +++ b/src/ical_parsing.rs @@ -14,6 +14,8 @@ pub struct Event { pub uid: String, pub summary: String, pub description: String, + /// X-ALT-DESC;FMTTYPE=text/html + pub description_html: Option, pub start: NaiveDateTime, pub end: NaiveDateTime, pub duration: Option, @@ -43,6 +45,7 @@ fn process_event(event: IcalEvent) -> Result { let mut uid = None; let mut summary = None; let mut description = None; + let mut description_html = None; let mut start = None; let mut end = None; let mut duration = None; @@ -59,6 +62,14 @@ fn process_event(event: IcalEvent) -> Result { "DTEND" => end = Some(process_dt(&value)?), "DURATION" => duration = Some(process_duration(&value)?), "RRULE" => { /* TODO: periodic */ }, + "X-ALT-DESC" => { + if prop.params.as_ref() + .map(|x| x.iter() + .any(|(key, values)| key == "FMTTYPE" && values.first().map(|x| &**x) == Some("text/html")) + ).unwrap_or(false) { + description_html = Some(value); + } + } _ => (), }; } @@ -68,7 +79,8 @@ fn process_event(event: IcalEvent) -> Result { Ok(Event { uid: uid.unwrap_or_default(), summary: summary.unwrap_or_default(), - description: description.unwrap_or_default(), + description: description.unwrap_or_default(), + description_html, start: start, end: end, duration, diff --git a/src/main.rs b/src/main.rs index 729817f..3ebf544 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,7 +130,6 @@ async fn process_one(update: Update, reminder_msg: &mut MessageId, reminder_text } } else if let MessageKind::Document { ref data, ref caption, .. } = message.kind { let document = data; - send_message(format!("Document {:?} {:?} {:?} {:?}", caption, document.file_id, document.file_name, document.mime_type)).await?; let get_file = GetFile::new(&document); let file = API.send(get_file).await?; let url = file.get_url(&TELEGRAM_BOT_TOKEN).ok_or_else(|| error("url is none"))?; @@ -140,11 +139,29 @@ async fn process_one(update: Update, reminder_msg: &mut MessageId, reminder_text (mime::TEXT, x) if x == "calendar" => { let text = String::from_utf8_lossy(&data); let text = text.replace("\n<", "<"); // newlines in HTML values - send_message(&text).await?; + //send_message(&text).await?; let calendar = ical_parsing::parse_calendar(&text)?; - send_message(format!("{:?}", calendar)).await?; + //send_message(format!("{:?}", calendar)).await?; + if calendar.events.len() != 1 { + return Ok(()); + } + if CLIENT.get("http://localhost:9001/custom/new_event").form(&json!({ + "name": calendar.events[0].summary, + "summary": calendar.events[0].description_html.as_deref().unwrap_or(&calendar.events[0].description), + "fileName": document.file_name, + "fileData": text, + "location": calendar.events[0].location, + "startTime": calendar.events[0].start.format("%Y-%m-%dT%H:%M:%S").to_string(), + "endTime": calendar.events[0].end.format("%Y-%m-%dT%H:%M:%S").to_string(), + })).send().await?.status().is_success() { + send_message("Event saved :-)").await?; + } else { + send_message("error saving event").await?; + } }, - _ => {} + _ => { + send_message(format!("Document {:?} {:?} {:?} {:?}", caption, document.file_id, document.file_name, document.mime_type)).await?; + } } } } else if let UpdateKind::CallbackQuery(cb) = update.kind { @@ -178,7 +195,7 @@ async fn process_one(update: Update, reminder_msg: &mut MessageId, reminder_text CLIENT.get("http://localhost:9001/custom/new_reminder").form(&json!({ "time": remind_time.to_rfc3339(), "task": *reminder_text - })).send().await?.text().await?; + })).send().await?; API.send(SendMessage::new(*OWNER, "Reminder saved :-)")).await?; *reminder_text = String::new(); },