don't unwrap UserId from state key

master
Devin R 2020-04-05 08:23:59 -04:00
parent eff322c0c5
commit 25a3533792
3 changed files with 177 additions and 15 deletions

View File

@ -50,17 +50,6 @@ use ruma_identifiers::DeviceId;
pub type Token = String;
#[derive(Debug, Default)]
/// `RoomName` allows the calculation of a text room name.
pub struct RoomName {
/// The displayed name of the room.
name: Option<String>,
/// The canonical alias of the room ex. `#room-name:example.com` and port number.
canonical_alias: Option<RoomAliasId>,
/// List of `RoomAliasId`s the room has been given.
aliases: Vec<RoomAliasId>,
}
/// A no IO Client implementation.
///
/// This Client is a state machine that receives responses and events and

View File

@ -156,7 +156,7 @@ impl Room {
}
let member = RoomMember::new(event);
self.members
.insert(UserId::try_from(event.state_key.as_str()).unwrap(), member);
@ -187,9 +187,14 @@ impl Room {
match event.membership_change() {
MembershipChange::Invited | MembershipChange::Joined => self.add_member(event),
_ => {
let user = if let Ok(id) = UserId::try_from(event.state_key.as_str()) {
id
} else {
return false;
};
if let Some(member) = self
.members
.get_mut(&UserId::try_from(event.state_key.as_str()).unwrap())
.get_mut(&user)
{
member.update_member(event)
} else {
@ -234,9 +239,17 @@ impl Room {
///
/// Returns true if the room name changed, false otherwise.
pub fn handle_power_level(&mut self, event: &PowerLevelsEvent) -> bool {
// when getting a response from the actual matrix server `state_key`
// was empty, the spec says "state_key: A zero-length string."
// for `m.room.power_levels` events
let user = if let Ok(id) = UserId::try_from(event.state_key.as_str()) {
id
} else {
return false;
};
if let Some(member) = self
.members
.get_mut(&UserId::try_from(event.state_key.as_str()).unwrap())
.get_mut(&user)
{
member.update_power(event)
} else {
@ -260,7 +273,7 @@ impl Room {
match event {
// update to the current members of the room
RoomEvent::RoomMember(m) => self.handle_membership(m),
// finds all events related to the name of the room for later calculation
// finds all events related to the name of the room for later use
RoomEvent::RoomName(n) => self.handle_room_name(n),
RoomEvent::RoomCanonicalAlias(ca) => self.handle_canonical(ca),
RoomEvent::RoomAliases(a) => self.handle_room_aliases(a),

160
tests/data/sync2.json Normal file
View File

@ -0,0 +1,160 @@
{
"next_batch": "s72595_4483_1934",
"presence": {
"events": [
{
"content": {
"avatar_url": "mxc://localhost:wefuiwegh8742w",
"last_active_ago": 2478593,
"presence": "online",
"currently_active": false,
"status_msg": "Making cupcakes"
},
"type": "m.presence",
"sender": "@example:localhost"
}
]
},
"account_data": {
"events": [
{
"type": "org.example.custom.config",
"content": {
"custom_config_key": "custom_config_value"
}
}
]
},
"rooms": {
"join": {
"!726s6s6q:example.com": {
"summary": {
"m.heroes": [
"@alice:example.com",
"@bob:example.com"
],
"m.joined_member_count": 2,
"m.invited_member_count": 0
},
"state": {
"events": [
{
"content": {
"membership": "join",
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice Margatroid"
},
"type": "m.room.member",
"event_id": "$143273582443PhrSn:example.org",
"room_id": "!726s6s6q:example.com",
"sender": "@example:example.org",
"origin_server_ts": 1432735824653,
"unsigned": {
"age": 1234
},
"state_key": "@alice:example.org"
}
]
},
"timeline": {
"events": [
{
"content": {
"membership": "join",
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice Margatroid"
},
"type": "m.room.member",
"event_id": "$143273582443PhrSn:example.org",
"room_id": "!726s6s6q:example.com",
"sender": "@example:example.org",
"origin_server_ts": 1432735824653,
"unsigned": {
"age": 1234
},
"state_key": "@alice:example.org"
},
{
"content": {
"body": "This is an example text message",
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"formatted_body": "<b>This is an example text message</b>"
},
"type": "m.room.message",
"event_id": "$143273582443PhrSn:example.org",
"room_id": "!726s6s6q:example.com",
"sender": "@example:example.org",
"origin_server_ts": 1432735824653,
"unsigned": {
"age": 1234
}
}
],
"limited": true,
"prev_batch": "t34-23535_0_0"
},
"ephemeral": {
"events": [
{
"content": {
"user_ids": [
"@alice:matrix.org",
"@bob:example.com"
]
},
"type": "m.typing",
"room_id": "!jEsUZKDJdhlrceRyVU:example.org"
}
]
},
"account_data": {
"events": [
{
"content": {
"tags": {
"u.work": {
"order": 0.9
}
}
},
"type": "m.tag"
},
{
"type": "org.example.custom.room.config",
"content": {
"custom_config_key": "custom_config_value"
}
}
]
}
}
},
"invite": {
"!696r7674:example.com": {
"invite_state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.name",
"state_key": "",
"content": {
"name": "My Room Name"
}
},
{
"sender": "@alice:example.com",
"type": "m.room.member",
"state_key": "@bob:example.com",
"content": {
"membership": "invite"
}
}
]
}
}
},
"leave": {}
}
}