diff --git a/Cargo.toml b/Cargo.toml index ee70c7a..61ff008 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ serde = "1.0.144" serde_yaml = "0.9.10" postbus = "0.2.0" -matrix-sdk= "0.5.0" +ruma = "0.6.4" +matrix-sdk = "0.5.0" [target.x86_64-unknown-linux-musl.dependencies] openssl = { version = "0.10", features = ["vendored"] } \ No newline at end of file diff --git a/advanced_sample_config.yaml b/advanced_sample_config.yaml index 448a812..8546387 100644 --- a/advanced_sample_config.yaml +++ b/advanced_sample_config.yaml @@ -23,4 +23,4 @@ mappings: room_id: "!idofWARNINGroomyoucreatedbefore:example.com" - to: "critical@mydomain.com" sender_mxid: "@firstbot:example.com" - room'id: "!idofIMPORTANTroomyoucreatedbefore:example.com" \ No newline at end of file + room_id: "#IMPORTANTroomyoucreatedbefore:example.com" \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index f7dbe16..329a276 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,7 @@ pub struct Mapping { pub to: Option, pub mxid_sender: Option, pub room_id: String, + pub room_alias: Option, } pub fn load_config(file_path: &str) -> Config { diff --git a/src/matrix.rs b/src/matrix.rs index 1c05a96..e43121c 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,10 +1,12 @@ use matrix_sdk::{Client, ruma::{UserId, RoomId, events::{room::message::RoomMessageEventContent}}, Session, room::Room, config::SyncSettings}; +use ruma::OwnedRoomAliasId; use postbus::command::Mailbox; use crate::config::{Mapping, Config}; pub async fn get_clients(config: &mut Config) -> Vec { let mut clients: Vec = vec![]; + let mut room_aliases_resolved = false; // Try to build a matrix_sdk::Client object for each configured client for conf in &mut config.clients { let user_id = match UserId::parse(&conf.mxid) { @@ -44,6 +46,28 @@ pub async fn get_clients(config: &mut Config) -> Vec { conf.access_token = Some(session.access_token); conf.device_id = Some(session.device_id.to_string()); } + // If room aliases are supplied as room_id in the config file, resolve them and store the pairs properly + if !room_aliases_resolved && client.logged_in().await { + for mapping in &mut config.mappings { + if mapping.room_id.as_str().chars().nth(0).unwrap() == '#' || mapping.room_id.len() == 0 { + // TODO: Clean this up once matrix-rust-sdk 0.6.0 is available + let alias: OwnedRoomAliasId = match OwnedRoomAliasId::try_from(mapping.room_id.as_str()) { + Ok(a) => a, + Err(_e) => continue, + }; + let resolve_request = matrix_sdk::ruma::api::client::alias::get_alias::v3::Request::new(&alias); + match client.send(resolve_request, None).await { + Ok(result) => { + let matrix_sdk::ruma::api::client::alias::get_alias::v3::Response{room_id, ..} = result; + mapping.room_alias = Some(mapping.room_id.clone()); + mapping.room_id = room_id.to_string(); + }, + Err(_e) => (), + }; + }let a = ruma::RoomAliasId::parse(&String::from("abc")); + } + room_aliases_resolved = true; + } clients.push(client.clone()); }