diff --git a/Cargo.lock b/Cargo.lock index bbe35a3ac..f78d30442 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3527,6 +3527,7 @@ version = "0.13.0-rc.1" dependencies = [ "anyhow", "async-trait", + "ruma-common", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 11a431119..8253b9774 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -248,6 +248,10 @@ version = "0.12.12" default-features = false features = ["http2", "rustls-tls-manual-roots", "charset", "json", "socks"] +# Matrix-related types +[workspace.dependencies.ruma-common] +version = "0.15.0" + # TLS stack [workspace.dependencies.rustls] version = "0.23.21" diff --git a/crates/data-model/Cargo.toml b/crates/data-model/Cargo.toml index ee49a6c05..845972c19 100644 --- a/crates/data-model/Cargo.toml +++ b/crates/data-model/Cargo.toml @@ -24,7 +24,7 @@ rand.workspace = true rand_chacha = "0.3.1" regex = "1.11.1" woothee = "0.13.0" -ruma-common = "0.15.0" +ruma-common.workspace = true mas-iana.workspace = true mas-jose.workspace = true diff --git a/crates/matrix/Cargo.toml b/crates/matrix/Cargo.toml index 8f7e77579..4f194bd22 100644 --- a/crates/matrix/Cargo.toml +++ b/crates/matrix/Cargo.toml @@ -15,3 +15,4 @@ workspace = true anyhow.workspace = true async-trait.workspace = true tokio.workspace = true +ruma-common.workspace = true diff --git a/crates/matrix/src/lib.rs b/crates/matrix/src/lib.rs index 700bb15d8..76f32e09f 100644 --- a/crates/matrix/src/lib.rs +++ b/crates/matrix/src/lib.rs @@ -8,6 +8,8 @@ mod mock; use std::{collections::HashSet, sync::Arc}; +use ruma_common::UserId; + pub use self::mock::HomeserverConnection as MockHomeserverConnection; // TODO: this should probably be another error type by default @@ -193,6 +195,22 @@ pub trait HomeserverConnection: Send + Sync { format!("@{}:{}", localpart, self.homeserver()) } + /// Get the localpart of a Matrix ID if it has the right server name + /// + /// Returns [`None`] if the input isn't a valid MXID, or if the server name + /// doesn't match + /// + /// # Parameters + /// + /// * `mxid` - The MXID of the user + fn localpart<'a>(&self, mxid: &'a str) -> Option<&'a str> { + let mxid = <&UserId>::try_from(mxid).ok()?; + if mxid.server_name() != self.homeserver() { + return None; + } + Some(mxid.localpart()) + } + /// Query the state of a user on the homeserver. /// /// # Parameters