From 25a3533792efaeb94cbe37e77e6693db5a47f913 Mon Sep 17 00:00:00 2001 From: Devin R Date: Sun, 5 Apr 2020 08:23:59 -0400 Subject: [PATCH] don't unwrap UserId from state key --- src/base_client.rs | 11 --- src/models/room.rs | 21 ++++-- tests/data/sync2.json | 160 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 tests/data/sync2.json diff --git a/src/base_client.rs b/src/base_client.rs index bc18efca..900bad02 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -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, - /// The canonical alias of the room ex. `#room-name:example.com` and port number. - canonical_alias: Option, - /// List of `RoomAliasId`s the room has been given. - aliases: Vec, -} - /// A no IO Client implementation. /// /// This Client is a state machine that receives responses and events and diff --git a/src/models/room.rs b/src/models/room.rs index 7b19cabf..7f6bde57 100644 --- a/src/models/room.rs +++ b/src/models/room.rs @@ -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), diff --git a/tests/data/sync2.json b/tests/data/sync2.json new file mode 100644 index 00000000..6adb2c13 --- /dev/null +++ b/tests/data/sync2.json @@ -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": "This is an example text message" + }, + "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": {} + } + } + \ No newline at end of file