Skip to content

Commit a5c9468

Browse files
committed
Utility to extract the localpart from a MXID
1 parent fbbbf5b commit a5c9468

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ version = "0.12.12"
248248
default-features = false
249249
features = ["http2", "rustls-tls-manual-roots", "charset", "json", "socks"]
250250

251+
# Matrix-related types
252+
[workspace.dependencies.ruma-common]
253+
version = "0.15.0"
254+
251255
# TLS stack
252256
[workspace.dependencies.rustls]
253257
version = "0.23.21"

crates/data-model/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rand.workspace = true
2424
rand_chacha = "0.3.1"
2525
regex = "1.11.1"
2626
woothee = "0.13.0"
27-
ruma-common = "0.15.0"
27+
ruma-common.workspace = true
2828

2929
mas-iana.workspace = true
3030
mas-jose.workspace = true

crates/matrix/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ workspace = true
1515
anyhow.workspace = true
1616
async-trait.workspace = true
1717
tokio.workspace = true
18+
ruma-common.workspace = true

crates/matrix/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ mod mock;
88

99
use std::{collections::HashSet, sync::Arc};
1010

11+
use ruma_common::UserId;
12+
1113
pub use self::mock::HomeserverConnection as MockHomeserverConnection;
1214

1315
// TODO: this should probably be another error type by default
@@ -193,6 +195,22 @@ pub trait HomeserverConnection: Send + Sync {
193195
format!("@{}:{}", localpart, self.homeserver())
194196
}
195197

198+
/// Get the localpart of a Matrix ID if it has the right server name
199+
///
200+
/// Returns [`None`] if the input isn't a valid MXID, or if the server name
201+
/// doesn't match
202+
///
203+
/// # Parameters
204+
///
205+
/// * `mxid` - The MXID of the user
206+
fn localpart<'a>(&self, mxid: &'a str) -> Option<&'a str> {
207+
let mxid = <&UserId>::try_from(mxid).ok()?;
208+
if mxid.server_name() != self.homeserver() {
209+
return None;
210+
}
211+
Some(mxid.localpart())
212+
}
213+
196214
/// Query the state of a user on the homeserver.
197215
///
198216
/// # Parameters

0 commit comments

Comments
 (0)