Skip to content

Commit

Permalink
Implement basic message renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
haecker-felix committed Jun 20, 2021
1 parent 1d4a41d commit a06428f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
26 changes: 25 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio = { version="1.7", features = ["macros"] }
async-trait = "0.1"
log = "0.4"
pretty_env_logger = "0.4"
chrono = "0.4"
40 changes: 30 additions & 10 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
use std::sync::Mutex;

use crate::config::Config;
use crate::render;
use crate::store::{News, NewsStore};
use crate::utils;

Expand Down Expand Up @@ -86,8 +87,12 @@ impl Bot {
);
}

async fn send_message(&self, msg: &str, admin_room: bool) {
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(msg));
async fn send_message(&self, msg: &str, html: bool, admin_room: bool) {
let content = if html {
AnyMessageEventContent::RoomMessage(MessageEventContent::text_html(msg, msg))
} else {
AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(msg))
};
let txn_id = Uuid::new_v4();

let room = if admin_room {
Expand Down Expand Up @@ -164,7 +169,7 @@ impl EventCallback {
"Thanks for the report {}, I'll store your update!",
reporter_display_name
);
self.0.send_message(&msg, false).await;
self.0.send_message(&msg, false, false).await;

let news = News {
event_id,
Expand Down Expand Up @@ -200,7 +205,7 @@ impl EventCallback {
format!("Approved news with event id {}!", event_id)
}
};
self.0.send_message(&msg, true).await;
self.0.send_message(&msg, false, true).await;
}
}

Expand All @@ -213,7 +218,7 @@ impl EventCallback {
// Check if the sender is a editor (= has the permission to use commands)
if !self.is_editor(&member).await {
let msg = "You don't have the permission to use commands.";
self.0.send_message(msg, true).await;
self.0.send_message(msg, false, true).await;
return;
}

Expand All @@ -229,6 +234,7 @@ impl EventCallback {
info!("Received command: {} ({})", command, args);

match command {
"!render-message" => self.render_message_command(member).await,
"!status" => self.status_command().await,
"!clear" => self.clear_command().await,
"!help" => self.help_command().await,
Expand All @@ -245,7 +251,7 @@ impl EventCallback {
!clear \n\
!say <message>";

self.0.send_message(help, true).await;
self.0.send_message(help, false, true).await;
}

async fn status_command(&self) {
Expand All @@ -270,7 +276,21 @@ impl EventCallback {
)
};

self.0.send_message(&msg, true).await;
self.0.send_message(&msg, false, true).await;
}

async fn render_message_command(&self, editor: &RoomMember) {
let rendered = {
let news_store = self.0.news_store.lock().unwrap();
let news = news_store.get_news().clone();

let editor = utils::get_member_display_name(editor);
let r = render::render(news, editor);

format!("<pre><code>{}</code></pre>\n", r)
};

self.0.send_message(&rendered, true, true).await;
}

async fn clear_command(&self) {
Expand All @@ -283,16 +303,16 @@ impl EventCallback {
format!("Cleared {} news!", news.len())
};

self.0.send_message(&msg, true).await;
self.0.send_message(&msg, false, true).await;
}

async fn say_command(&self, msg: &str) {
self.0.send_message(&msg, false).await;
self.0.send_message(&msg, true, false).await;
}

async fn unrecognized_command(&self) {
let msg = "Unrecognized command. Use !help to list available commands.";
self.0.send_message(msg, true).await;
self.0.send_message(msg, false, true).await;
}

async fn is_editor(&self, member: &RoomMember) -> bool {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate log;
mod bot;
mod config;
mod error;
mod render;
mod store;
mod utils;

Expand Down
49 changes: 49 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::env;
use std::fs::File;
use std::io::Read;

use crate::store::News;

pub fn render(news: Vec<News>, editor: String) -> String {
let path = match env::var("TEMPLATE_PATH") {
Ok(val) => val,
Err(_) => "./template.md".to_string(),
};

let mut file = File::open(path).expect("Unable to open template file");
let mut template = String::new();
file.read_to_string(&mut template)
.expect("Unable to read template file");

let mut report = String::new();
for n in news {
// skip not approved news
if !n.approved {
continue;
}

let section = "Section header (not implemented yet)";
let user = format!(
"[{}](https://matrix.to/#/{})",
n.reporter_display_name, n.reporter_id
);

let section = format!(
"# {}\n\
{} reports that\n\
> {}\n\n",
section, user, n.message
);

report = (report + &section).to_string();
}

let now: chrono::DateTime<chrono::Utc> = chrono::Utc::now();
let today = now.format("%Y-%m-%d");

template = template.replace("{{today}}", &today.to_string());
template = template.replace("{{author}}", &editor);
template = template.replace("{{report}}", &report);

template
}

0 comments on commit a06428f

Please sign in to comment.