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] # [unreleased]
Improvements:
* Add support for historical uppercase MXIDs
# 0.14.1 # 0.14.1
Breaking changes: Breaking changes:

View File

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