Utility to extract the localpart from a MXID
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -3527,6 +3527,7 @@ version = "0.13.0-rc.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"ruma-common",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -15,3 +15,4 @@ workspace = true
|
||||
anyhow.workspace = true
|
||||
async-trait.workspace = true
|
||||
tokio.workspace = true
|
||||
ruma-common.workspace = true
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user