Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ once_cell = "1.16.0"
pin-project-lite = "0.2.9"
rand = "0.8.5"
reqwest = { version = "0.12.4", default-features = false }
ruma = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c454302e1e8f5b5f62f249a57", features = [
ruma = { git = "https://github.com/matrix-org/ruma", rev = "e87f1d839e3821f0fb0ba20fb3164fedbc90a25c", features = [
"client-api-c",
"compat-upload-signatures",
"compat-user-id",
Expand All @@ -61,7 +61,7 @@ ruma = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c4543
"unstable-msc4075",
"unstable-msc4140",
] }
ruma-common = { git = "https://github.com/matrix-org/ruma", rev = "79289381444e886c454302e1e8f5b5f62f249a57" }
ruma-common = { git = "https://github.com/matrix-org/ruma", rev = "e87f1d839e3821f0fb0ba20fb3164fedbc90a25c" }
serde = "1.0.151"
serde_html_form = "0.2.0"
serde_json = "1.0.91"
Expand Down
77 changes: 72 additions & 5 deletions crates/matrix-sdk-base/src/rooms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use ruma::{
RedactedStateEventContent, StaticStateEventContent, SyncStateEvent,
},
room::RoomType,
EventId, OwnedUserId, RoomVersionId,
EventId, OwnedUserId, RoomVersionId, UserId,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -195,9 +195,12 @@ impl BaseRoomInfo {
let mut o_ev = o_ev.clone();
o_ev.content.set_created_ts_if_none(o_ev.origin_server_ts);

let Some(owned_user_id) = get_user_id_for_state_key(m.state_key()) else {
return false;
};

// add the new event.
self.rtc_member
.insert(m.state_key().clone(), SyncStateEvent::Original(o_ev).into());
self.rtc_member.insert(owned_user_id, SyncStateEvent::Original(o_ev).into());

// Remove all events that don't contain any memberships anymore.
self.rtc_member.retain(|_, ev| {
Expand Down Expand Up @@ -319,6 +322,33 @@ impl BaseRoomInfo {
}
}

/// Extract a user ID from a state key that matches one of these formats:
/// - `<user ID>`
/// - `<user ID>_<string>`
/// - `_<user ID>_<string>`
fn get_user_id_for_state_key(state_key: &str) -> Option<OwnedUserId> {
if let Ok(user_id) = UserId::parse(state_key) {
return Some(user_id);
}

// Ignore leading underscore if present
// (used for avoiding auth rules on @-prefixed state keys)
let state_key = state_key.strip_prefix('_').unwrap_or(state_key);
if state_key.starts_with('@') {
if let Some(colon_idx) = state_key.find(':') {
let state_key_user_id = match state_key[colon_idx + 1..].find('_') {
None => state_key,
Some(suffix_idx) => &state_key[..colon_idx + 1 + suffix_idx],
};
if let Ok(user_id) = UserId::parse(state_key_user_id) {
return Some(user_id);
}
}
}

None
}

bitflags! {
/// Notable tags, i.e. subset of tags that we are more interested by.
///
Expand Down Expand Up @@ -536,9 +566,13 @@ impl RoomMemberships {
mod tests {
use std::ops::Not;

use ruma::events::tag::{TagInfo, TagName, Tags};
use assert_matches::assert_matches;
use ruma::{
events::tag::{TagInfo, TagName, Tags},
user_id,
};

use super::{BaseRoomInfo, RoomNotableTags};
use super::{get_user_id_for_state_key, BaseRoomInfo, RoomNotableTags};

#[test]
fn test_handle_notable_tags_favourite() {
Expand Down Expand Up @@ -569,4 +603,37 @@ mod tests {
base_room_info.handle_notable_tags(&tags);
assert!(base_room_info.notable_tags.contains(RoomNotableTags::LOW_PRIORITY).not());
}

#[test]
fn test_get_user_id_for_state_key() {
assert_matches!(get_user_id_for_state_key(""), None);
assert_matches!(get_user_id_for_state_key("abc"), None);
assert_matches!(get_user_id_for_state_key("@nocolon"), None);
assert_matches!(get_user_id_for_state_key("@noserverpart:"), None);
assert_matches!(get_user_id_for_state_key("@noserverpart:_suffix"), None);

let user_id = user_id!("@username:example.org");

assert_matches!(
get_user_id_for_state_key(user_id.as_str()),
Some(captured_user_id) => assert_eq!(captured_user_id, user_id));
assert_matches!(
get_user_id_for_state_key(format!("{user_id}_valid_suffix").as_str()),
Some(captured_user_id) => assert_eq!(captured_user_id, user_id));
assert_matches!(
get_user_id_for_state_key(format!("{user_id}:invalid_suffix").as_str()),
None
);

assert_matches!(
get_user_id_for_state_key(format!("_{user_id}").as_str()),
Some(captured_user_id) => assert_eq!(captured_user_id, user_id));
assert_matches!(
get_user_id_for_state_key(format!("_{user_id}_valid_suffix").as_str()),
Some(captured_user_id) => assert_eq!(captured_user_id, user_id));
assert_matches!(
get_user_id_for_state_key(format!("_{user_id}:invalid_suffix").as_str()),
None
);
}
}
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,7 @@ mod tests {
// we can simply use now here since this will be dropped when using a MinimalStateEvent
// in the roomInfo
origin_server_ts: timestamp(0),
state_key: user_id.to_owned(),
state_key: user_id.to_string(),
unsigned: StateUnsigned::new(),
}))
}
Expand Down