Utility to extract the localpart from a MXID

This commit is contained in:
Quentin Gliech
2025-01-28 17:25:36 +01:00
parent 7f72ba51f9
commit cba1564f4e
5 changed files with 25 additions and 1 deletions

1
Cargo.lock generated
View File

@@ -3527,6 +3527,7 @@ version = "0.13.0-rc.1"
dependencies = [
"anyhow",
"async-trait",
"ruma-common",
"tokio",
]

View File

@@ -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"

View File

@@ -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

View File

@@ -15,3 +15,4 @@ workspace = true
anyhow.workspace = true
async-trait.workspace = true
tokio.workspace = true
ruma-common.workspace = true

View File

@@ -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