From cfc7dc8b3ef149f384bd1f2ef3989701a93c7df2 Mon Sep 17 00:00:00 2001 From: Xyoz Netsphere Date: Mon, 24 Jan 2022 12:02:56 +0100 Subject: feat(im-matrix): added multi-room support --- Cargo.lock | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ README.md | 4 ++-- ezau-sample.toml | 4 ++-- src/config.rs | 7 ++++-- src/main.rs | 14 +++++++---- 6 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4baf4a1..6999e74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -482,6 +482,41 @@ dependencies = [ "zeroize", ] +[[package]] +name = "darling" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d720b8683f8dd83c65155f0530560cba68cd2bf395f6513a483caee57ff7f4" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a340f241d2ceed1deb47ae36c4144b2707ec7dd0b649f894cb39bb595986324" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2 1.0.32", + "quote 1.0.10", + "strsim", + "syn 1.0.82", +] + +[[package]] +name = "darling_macro" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c41b3b7352feb3211a0d743dc5700a4e3b60f51bd2b368892d1e0f9a95f44b" +dependencies = [ + "darling_core", + "quote 1.0.10", + "syn 1.0.82", +] + [[package]] name = "dashmap" version = "4.0.2" @@ -608,6 +643,7 @@ dependencies = [ "reqwest", "ruma", "serde", + "serde_with", "serenity", "tokio", "toml", @@ -1090,6 +1126,12 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.3" @@ -2426,6 +2468,12 @@ dependencies = [ "webpki", ] +[[package]] +name = "rustversion" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f" + [[package]] name = "ryu" version = "1.0.5" @@ -2545,6 +2593,29 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad6056b4cb69b6e43e3a0f055def223380baecc99da683884f205bf347f7c4b3" +dependencies = [ + "rustversion", + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12e47be9471c72889ebafb5e14d5ff930d89ae7a67bbdb5f8abb564f845a927e" +dependencies = [ + "darling", + "proc-macro2 1.0.32", + "quote 1.0.10", + "syn 1.0.82", +] + [[package]] name = "serenity" version = "0.10.9" diff --git a/Cargo.toml b/Cargo.toml index 304ccab..8f41382 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,3 +45,7 @@ features = [ ] version = "0.10" optional = true + +[dependencies.serde_with] +version = "1.11.0" +features = [] diff --git a/README.md b/README.md index b89a6ec..e1fe4d7 100644 --- a/README.md +++ b/README.md @@ -113,9 +113,9 @@ password = "secret-foo" # after the first connection. # (optional) device_id = "ASDFGH" -# Room ID to which the message should be posted +# Room ID(s) to which the message should be posted # (mandatory) -room_id = "!room123456:homeserver.org" +room_id = ["!room123456:homeserver.org"] ``` Usage diff --git a/ezau-sample.toml b/ezau-sample.toml index 302f9dd..9076233 100644 --- a/ezau-sample.toml +++ b/ezau-sample.toml @@ -55,6 +55,6 @@ password = "secret-foo" # after the first connection. # (optional) device_id = "ASDFGH" -# Room ID to which the message should be posted +# Room ID(s) to which the message should be posted # (mandatory) -room_id = "!room123456:homeserver.org" +room_id = ["!room123456:homeserver.org"] diff --git a/src/config.rs b/src/config.rs index a4bba44..bf9af1e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use std::{fs, path::Path}; use anyhow::Result; use serde::Deserialize; +use serde_with::{serde_as, OneOrMany}; /// The main configuration. #[derive(Debug, Clone, Deserialize, Default)] @@ -36,6 +37,7 @@ pub struct Discord { } /// Configuration pertaining to the Matrix posting. +#[serde_as] #[derive(Debug, Clone, Deserialize)] pub struct Matrix { /// Matrix homeserver. @@ -46,8 +48,9 @@ pub struct Matrix { pub password: String, /// Device ID, or None if a new one should be generated. pub device_id: Option, - /// Room ID where the message should be posted to. - pub room_id: String, + /// Room ID(s) where the message should be posted to. + #[serde_as(as = "OneOrMany<_>")] + pub room_id: Vec, } /// Attempt to load the configuration from the given file. diff --git a/src/main.rs b/src/main.rs index bdaf13a..ada456c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,8 +93,10 @@ fn inner_main(opts: &Opts) -> Result<()> { } if let Some(m) = &config.matrix { - matrix::post_link(m.clone().into(), &m.room_id, &log, &permalink) - .context("Could not post link to Matrix")?; + for room_id in &m.room_id { + matrix::post_link(m.clone().into(), &room_id, &log, &permalink) + .context(format!("Could not post link to Matrix (room_id: {})", &room_id))?; + } } } } @@ -203,9 +205,11 @@ fn handle_file(config: &Config, filename: &Path) -> Result<()> { } if let Some(m) = &config.matrix { - matrix::post_link(m.clone().into(), &m.room_id, &log, &permalink) - .context("Could not post link to Matrix")?; - info!("Posted link to Matrix"); + for room_id in &m.room_id { + matrix::post_link(m.clone().into(), &room_id, &log, &permalink) + .context(format!("Could not post link to Matrix (room_id: {})", &room_id))?; + info!("Posted link to Matrix ({})", &room_id); + } } Ok(()) -- cgit v1.2.3