From 57f211036ee5119feffe73936f88ef8ed36a2133 Mon Sep 17 00:00:00 2001 From: murmeldin Date: Fri, 20 Dec 2024 09:38:32 +0100 Subject: [PATCH] some matrix improvements with added html messages --- src/matrix.rs | 70 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 8a025ab..9479c7e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,10 +1,7 @@ -use std::cell::OnceCell; use std::error::Error; +use std::io::Read; -use clap::builder::Str; use colored::Colorize; - -use nom::Err; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::time::{SystemTime, UNIX_EPOCH}; @@ -104,6 +101,7 @@ impl MatrixClient { ) -> Result> { let client = reqwest::blocking::Client::new(); let url = format!("{}/_matrix/client{}", self.homeserver_url, endpoint); + print!("url: {}", url.yellow()); // Construct URL with query parameters let mut request = client.request(method, &url); @@ -144,17 +142,17 @@ impl MatrixClient { self.request(reqwest::Method::POST, endpoint, query_data, json_data, unauth) } - fn get( - &self, endpoint: &str, query_data: Option<&HashMap>, unauth: bool, - ) -> Result> { - self.request::>( - reqwest::Method::GET, - endpoint, - query_data, - None, - unauth, - ) - } + // fn get( + // &self, endpoint: &str, query_data: Option<&HashMap>, unauth: bool, + // ) -> Result> { + // self.request::>( + // reqwest::Method::GET, + // endpoint, + // query_data, + // None, + // unauth, + // ) + // } pub fn login(&mut self, username: &str, password: &str) -> Result<(), Box> { let login_request = LoginRequest { @@ -176,10 +174,44 @@ impl MatrixClient { current } + pub fn pandoc_convert_md_to_html(markdown: String) -> Result> { + let (output, errors, status) = crate::pipe( + "pandoc", + &mut ["--from", "markdown-auto_identifiers", "--to", "html5"], + markdown, + )?; + if status.success() { + println!("Resultat von Pandoc: {}", output); + Ok(output) + } else { + Err(format!("Pandoc error, exit code {:?}\n{}", status, errors).into()) + } + } + + pub fn pandoc_convert_text_to_md(markdown: String) -> Result> { + let (output, errors, status) = crate::pipe( + "pandoc", + &mut ["--from", "markdown-auto_identifiers", "--to", "html5"], + markdown, + )?; + if status.success() { + println!("Resultat von Pandoc: {}", output); + Ok(output) + } else { + Err(format!("Pandoc error, exit code {:?}\n{}", status, errors).into()) + } + } + pub fn send_room_message( &mut self, room_id: &str, text: &str, ) -> Result> { - let content = HashMap::from([("msgtype", "m.text"), ("body", text)]); + let formatted_text = Self::pandoc_convert_md_to_html(text.to_string())?; + let content = HashMap::from([ + ("msgtype", "m.text"), + ("body", text), + ("format", "org.matrix.custom.html"), + ("formatted_body", &formatted_text), + ]); self.send_room_event(&room_id, "m.room.message", &content) } @@ -187,13 +219,15 @@ impl MatrixClient { &mut self, room: &str, event_type: &str, content: &impl Serialize, ) -> Result> { let endpoint = format!("/r0/rooms/{}/send/{}/{}", room, event_type, self.txn_id()); + println!("room event:{}", &endpoint.red()); self.put(&endpoint, None, Some(content), false) } pub fn send_short_and_long_messages_to_two_rooms( &mut self, short_message: &str, long_message: &str, ) -> Result<(), Box> { - self.send_room_message(&long_message, &self.room_id_for_long_messages.clone())?; - self.send_room_message(&short_message, &self.room_id_for_short_messages.clone())?; + self.send_room_message( &self.room_id_for_long_messages.clone(), &long_message,)?; + self.send_room_message( &self.room_id_for_short_messages.clone(), &short_message,)?; Ok(()) } + }