Add support for historical uppercase user IDs

This commit is contained in:
Marcel 2020-02-17 09:31:46 +01:00 committed by GitHub
parent 03555a3aed
commit 81fb260a46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View File

@ -1,5 +1,9 @@
# [unreleased]
Improvements:
* Add support for historical uppercase MXIDs
# 0.14.1
Breaking changes:

View File

@ -123,10 +123,9 @@ impl TryFrom<&str> for UserId {
/// server name.
fn try_from(user_id: &str) -> Result<Self, Error> {
let (localpart, host, port) = parse_id('@', user_id)?;
let downcased_localpart = localpart.to_lowercase();
// See https://matrix.org/docs/spec/appendices#user-identifiers
let is_fully_conforming = downcased_localpart.bytes().all(|b| match b {
let is_fully_conforming = localpart.bytes().all(|b| match b {
b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.' | b'=' | b'_' | b'/' => true,
_ => false,
});
@ -134,18 +133,14 @@ impl TryFrom<&str> for UserId {
// If it's not fully conforming, check if it contains characters that are also disallowed
// for historical user IDs. If there are, return an error.
// See https://matrix.org/docs/spec/appendices#historical-user-ids
if !is_fully_conforming
&& downcased_localpart
.bytes()
.any(|b| b < 0x21 || b == b':' || b > 0x7E)
{
if !is_fully_conforming && localpart.bytes().any(|b| b < 0x21 || b == b':' || b > 0x7E) {
return Err(Error::InvalidCharacters);
}
Ok(Self {
hostname: host,
port,
localpart: downcased_localpart,
localpart: localpart.to_owned(),
is_historical: !is_fully_conforming,
})
}
@ -176,12 +171,9 @@ mod tests {
#[test]
fn downcase_user_id() {
assert_eq!(
UserId::try_from("@CARL:example.com")
.expect("Failed to create UserId.")
.to_string(),
"@carl:example.com"
);
let user_id = UserId::try_from("@CARL:example.com").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@CARL:example.com");
assert!(user_id.is_historical());
}
#[test]