diff options
author | Daniel Schadt <kingdread@gmx.de> | 2024-08-28 22:18:09 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2024-08-28 22:18:09 +0200 |
commit | 66719483b12d39f55dfac48121d0b8aafd544631 (patch) | |
tree | 25eb4f9ed135d2e98bdfb40f89e1749afdf853bd /src | |
parent | 0cb54858732ec6db0ef1de3f937ea41bec5ec7ae (diff) | |
download | ezau-66719483b12d39f55dfac48121d0b8aafd544631.tar.gz ezau-66719483b12d39f55dfac48121d0b8aafd544631.tar.bz2 ezau-66719483b12d39f55dfac48121d0b8aafd544631.zip |
add setting for log sorting
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 3 | ||||
-rw-r--r-- | src/discord.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 16 | ||||
-rw-r--r-- | src/matrix.rs | 9 |
4 files changed, 27 insertions, 15 deletions
diff --git a/src/config.rs b/src/config.rs index 8b9211c..6c2a035 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,9 @@ use serde_with::{serde_as, OneOrMany}; pub struct Config { /// Flag indicating whether logs should be uploaded or not. pub upload: bool, + /// Whether to sort the logs when posting them. + #[serde(default)] + pub sort_logs: bool, /// Where to upload the logs. #[serde(default = "default_dps_report_upload_url")] pub dps_report_upload_url: String, diff --git a/src/discord.rs b/src/discord.rs index 3ab849c..eeb4210 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -10,6 +10,7 @@ use tokio::runtime::Runtime; use log::info; +use super::config::Config; use super::categories::Categorizable; use super::logbag::{state_emoji, LogBag}; @@ -33,6 +34,7 @@ struct Handler { channel_id: u64, log: Log, link: String, + sort_logs: bool, } impl Handler { @@ -59,14 +61,14 @@ impl Handler { } if let Some(mut m) = messages.pop() { - let new_text = insert_link(&m.content, &self.log, &self.link); + let new_text = insert_link(&m.content, &self.log, &self.link, self.sort_logs); if new_text.len() <= MAX_MESSAGE_LENGTH { m.edit(ctx, |m| m.content(new_text)).await?; return Ok(()); } } - let new_text = insert_link("", &self.log, &self.link); + let new_text = insert_link("", &self.log, &self.link, false); ChannelId(self.channel_id).say(ctx, new_text).await?; Ok(()) @@ -88,7 +90,7 @@ impl EventHandler for Handler { } } -pub fn post_link(discord_token: &str, channel_id: u64, log: &Log, link: &str) -> Result<()> { +pub fn post_link(config: &Config, discord_token: &str, channel_id: u64, log: &Log, link: &str) -> Result<()> { let link = link.to_owned(); let log = log.clone(); let rt = Runtime::new()?; @@ -99,6 +101,7 @@ pub fn post_link(discord_token: &str, channel_id: u64, log: &Log, link: &str) -> channel_id, log, link, + sort_logs: config.sort_logs, }) .await?; { @@ -112,9 +115,12 @@ pub fn post_link(discord_token: &str, channel_id: u64, log: &Log, link: &str) -> }) } -fn insert_link(text: &str, log: &Log, link: &str) -> String { +fn insert_link(text: &str, log: &Log, link: &str, sort_logs: bool) -> String { let mut logbag = LogBag::parse_markdown(text).unwrap(); let line = format!("{} {}", state_emoji(log), link); logbag.insert(log.category(), line); + if sort_logs { + logbag.sort(); + } logbag.render_markdown() } diff --git a/src/main.rs b/src/main.rs index b0ddd44..5a5377a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,13 +84,13 @@ fn inner_main(opts: &Opts) -> Result<()> { let log = load_log(&u.path)?; if let Some(d) = &config.discord { - discord::post_link(&d.auth_token, d.channel_id, &log, &permalink) + discord::post_link(&config, &d.auth_token, d.channel_id, &log, &permalink) .context("Could not post link to Discord")?; } if let Some(m) = &config.matrix { for room_id in &m.room_id { - matrix::post_link(m.clone().into(), room_id, &log, &permalink).context( + matrix::post_link(&config, m.clone().into(), room_id, &log, &permalink).context( format!("Could not post link to Matrix (room_id: {})", &room_id), )?; } @@ -154,14 +154,14 @@ fn handle_file(config: &Config, filename: &Path) -> Result<()> { info!("Uploaded log, available at {}", permalink); if let Some(d) = &config.discord { - discord::post_link(&d.auth_token, d.channel_id, &log, &permalink) + discord::post_link(config, &d.auth_token, d.channel_id, &log, &permalink) .context("Could not post link to Discord")?; info!("Posted link to Discord"); } if let Some(m) = &config.matrix { for room_id in &m.room_id { - matrix::post_link(m.clone().into(), room_id, &log, &permalink).context(format!( + matrix::post_link(config, m.clone().into(), room_id, &log, &permalink).context(format!( "Could not post link to Matrix (room_id: {})", &room_id ))?; @@ -253,18 +253,18 @@ fn sanity_check(config: &Config, opts: &Opts) -> Result<()> { // Dummy modules for when the features are disabled #[cfg(not(feature = "im-discord"))] mod discord { - use super::{Log, Result}; + use super::{config::Config, Log, Result}; use anyhow::bail; /// Stub, enable the `im-discord` feature to use this function. - pub fn post_link(_: &str, _: u64, _: &Log, _: &str) -> Result<()> { + pub fn post_link(_: &Config, _: &str, _: u64, _: &Log, _: &str) -> Result<()> { bail!("Discord feature is disabled in this build!") } } #[cfg(not(feature = "im-matrix"))] mod matrix { - use super::{Log, Result}; + use super::{config::Config, Log, Result}; use anyhow::bail; pub struct MatrixUser; impl From<super::config::Matrix> for MatrixUser { @@ -273,7 +273,7 @@ mod matrix { } } /// Stub, enable the `im-matrix` feature to use this function. - pub fn post_link(_: MatrixUser, _: &str, _: &Log, _: &str) -> Result<()> { + pub fn post_link(_: &Config, _: MatrixUser, _: &str, _: &Log, _: &str) -> Result<()> { bail!("Matrix feature is disabled in this build!") } } diff --git a/src/matrix.rs b/src/matrix.rs index 7835ddd..d75381d 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -95,7 +95,7 @@ const MESSAGE_CHUNK_COUNT: u16 = 3; /// /// This function blocks until all API calls have been made, that is until the message has reached /// the homeserver. -pub fn post_link(user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Result<()> { +pub fn post_link(config: &config::Config, user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Result<()> { let rt = Runtime::new()?; let room_id = RoomId::parse(room_id)?; @@ -112,7 +112,7 @@ pub fn post_link(user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Resu }; sync(&client, sync_token, &session_file).await?; - + info!("Matrix connected as {:?}", client.user_id()); let old_msg = find_message(&client, &client.user_id().unwrap(), &room_id).await?; @@ -125,7 +125,10 @@ pub fn post_link(user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Resu Some((old_id, old_text)) => { debug!("Updating message {:?}", old_id); debug!("Updating message body {:?}", old_text); - let logbag = insert_log(&old_text, log, link); + let mut logbag = insert_log(&old_text, log, link); + if config.sort_logs { + logbag.sort(); + } let new_text = logbag.render_plain(); debug!("New message body {:?}", new_text); let new_html = logbag.render_html(); |