Consistently use localpart instead of opaque_id and use homeserver_host instead of server_name.

This commit is contained in:
Jimmy Cuadra 2019-06-03 12:11:57 -07:00
parent fb8039ac6d
commit 9011423eea
4 changed files with 27 additions and 23 deletions

View File

@ -83,9 +83,9 @@ impl EventId {
/// of 18 random ASCII characters. This should only be used for events in the original format /// of 18 random ASCII characters. This should only be used for events in the original format
/// as used by Matrix room versions 1 and 2. /// as used by Matrix room versions 1 and 2.
/// ///
/// Fails if the given origin server name cannot be parsed as a valid host. /// Fails if the homeserver cannot be parsed as a valid host.
pub fn new(server_name: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let event_id = format!("${}:{}", generate_localpart(18), server_name); let event_id = format!("${}:{}", generate_localpart(18), homeserver_host);
let (localpart, host, port) = parse_id('$', &event_id)?; let (localpart, host, port) = parse_id('$', &event_id)?;
Ok(Self(Format::Original(Original { Ok(Self(Format::Original(Original {

View File

@ -35,7 +35,7 @@ pub struct RoomId {
/// The hostname of the homeserver. /// The hostname of the homeserver.
hostname: Host, hostname: Host,
/// The room's unique ID. /// The room's unique ID.
opaque_id: String, localpart: String,
/// The network port of the homeserver. /// The network port of the homeserver.
port: u16, port: u16,
} }
@ -47,14 +47,14 @@ impl RoomId {
/// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of /// Attempts to generate a `RoomId` for the given origin server with a localpart consisting of
/// 18 random ASCII characters. /// 18 random ASCII characters.
/// ///
/// Fails if the given origin server name cannot be parsed as a valid host. /// Fails if the given homeserver cannot be parsed as a valid host.
pub fn new(server_name: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let room_id = format!("!{}:{}", generate_localpart(18), server_name); let room_id = format!("!{}:{}", generate_localpart(18), homeserver_host);
let (opaque_id, host, port) = parse_id('!', &room_id)?; let (localpart, host, port) = parse_id('!', &room_id)?;
Ok(Self { Ok(Self {
hostname: host, hostname: host,
opaque_id: opaque_id.to_string(), localpart: localpart.to_string(),
port, port,
}) })
} }
@ -67,9 +67,9 @@ impl RoomId {
&self.hostname &self.hostname
} }
/// Returns the event's opaque ID. /// Returns the rooms's unique ID.
pub fn opaque_id(&self) -> &str { pub fn localpart(&self) -> &str {
&self.opaque_id &self.localpart
} }
/// Returns the port the originating homeserver can be accessed on. /// Returns the port the originating homeserver can be accessed on.
@ -80,7 +80,7 @@ impl RoomId {
impl Display for RoomId { impl Display for RoomId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
display(f, '!', &self.opaque_id, &self.hostname, self.port) display(f, '!', &self.localpart, &self.hostname, self.port)
} }
} }
@ -107,14 +107,14 @@ impl<'a> TryFrom<&'a str> for RoomId {
/// Attempts to create a new Matrix room ID from a string representation. /// Attempts to create a new Matrix room ID from a string representation.
/// ///
/// The string must include the leading ! sigil, the opaque ID, a literal colon, and a valid /// The string must include the leading ! sigil, the localpart, a literal colon, and a valid
/// server name. /// server name.
fn try_from(room_id: &'a str) -> Result<Self, Error> { fn try_from(room_id: &'a str) -> Result<Self, Error> {
let (opaque_id, host, port) = parse_id('!', room_id)?; let (localpart, host, port) = parse_id('!', room_id)?;
Ok(Self { Ok(Self {
hostname: host, hostname: host,
opaque_id: opaque_id.to_owned(), localpart: localpart.to_owned(),
port, port,
}) })
} }

View File

@ -59,7 +59,7 @@ impl Display for RoomIdOrAliasId {
RoomIdOrAliasId::RoomId(ref room_id) => display( RoomIdOrAliasId::RoomId(ref room_id) => display(
f, f,
'!', '!',
room_id.opaque_id(), room_id.localpart(),
room_id.hostname(), room_id.hostname(),
room_id.port(), room_id.port(),
), ),
@ -95,9 +95,9 @@ impl<'a> TryFrom<&'a str> for RoomIdOrAliasId {
/// Attempts to create a new Matrix room ID or a room alias ID from a string representation. /// Attempts to create a new Matrix room ID or a room alias ID from a string representation.
/// ///
/// The string must either /// The string must either include the leading ! sigil, the localpart, a literal colon, and a
/// include the leading ! sigil, the opaque ID, a literal colon, and a valid server name or /// valid homeserver host or include the leading # sigil, the alias, a literal colon, and a
/// include the leading # sigil, the alias, a literal colon, and a valid server name. /// valid homeserver host.
fn try_from(room_id_or_alias_id: &'a str) -> Result<Self, Error> { fn try_from(room_id_or_alias_id: &'a str) -> Result<Self, Error> {
validate_id(room_id_or_alias_id)?; validate_id(room_id_or_alias_id)?;

View File

@ -54,9 +54,13 @@ impl UserId {
/// Attempts to generate a `UserId` for the given origin server with a localpart consisting of /// Attempts to generate a `UserId` for the given origin server with a localpart consisting of
/// 12 random ASCII characters. /// 12 random ASCII characters.
/// ///
/// Fails if the given origin server name cannot be parsed as a valid host. /// Fails if the given homeserver cannot be parsed as a valid host.
pub fn new(server_name: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let user_id = format!("@{}:{}", generate_localpart(12).to_lowercase(), server_name); let user_id = format!(
"@{}:{}",
generate_localpart(12).to_lowercase(),
homeserver_host
);
let (localpart, host, port) = parse_id('@', &user_id)?; let (localpart, host, port) = parse_id('@', &user_id)?;
Ok(Self { Ok(Self {