From 2022ae9f0d1d0ffc52294abbdd797d2ab71e4231 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Wed, 16 Oct 2024 16:25:51 +0200 Subject: [PATCH] Accept any string as a key for m.direct account data --- Cargo.lock | 117 ++++++++++-------- Cargo.toml | 4 +- .../src/response_processors.rs | 12 +- crates/matrix-sdk-base/src/rooms/mod.rs | 3 +- crates/matrix-sdk-base/src/rooms/normal.rs | 3 +- .../matrix-sdk-base/src/sliding_sync/mod.rs | 28 +++-- .../src/store/migration_helpers.rs | 5 +- crates/matrix-sdk/src/account.rs | 2 +- crates/matrix-sdk/src/encryption/mod.rs | 7 +- crates/matrix-sdk/src/room/mod.rs | 2 +- crates/matrix-sdk/tests/integration/client.rs | 9 +- .../tests/integration/room/common.rs | 3 +- .../tests/integration/room/joined.rs | 35 +++++- .../matrix-sdk/tests/integration/room/left.rs | 12 +- 14 files changed, 154 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80ca196886a..e891f53833a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -666,7 +666,7 @@ dependencies = [ "semver", "serde", "serde_json", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -2753,7 +2753,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "thiserror", + "thiserror 1.0.63", "tower", "tower-http", "tracing", @@ -2796,7 +2796,7 @@ dependencies = [ "serde_with", "sha2", "signature", - "thiserror", + "thiserror 1.0.63", "tracing", "url", ] @@ -2825,7 +2825,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "serde_with", - "thiserror", + "thiserror 1.0.63", "tower", "tracing", "url", @@ -2853,7 +2853,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e2551de3bba2cc65b52dc6b268df6114011fe118ac24870fbcf1b35537bd721" dependencies = [ "matrix-pickle-derive", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -2927,7 +2927,7 @@ dependencies = [ "similar-asserts", "stream_assert", "tempfile", - "thiserror", + "thiserror 1.0.63", "tokio", "tokio-stream", "tokio-test", @@ -2971,7 +2971,7 @@ dependencies = [ "serde", "serde_json", "stream_assert", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", "uniffi", @@ -2995,7 +2995,7 @@ dependencies = [ "ruma", "serde", "serde_json", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", "tracing-subscriber", @@ -3044,7 +3044,7 @@ dependencies = [ "similar-asserts", "stream_assert", "subtle", - "thiserror", + "thiserror 1.0.63", "time", "tokio", "tokio-stream", @@ -3077,7 +3077,7 @@ dependencies = [ "serde_json", "sha2", "tempfile", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing-subscriber", "uniffi", @@ -3107,7 +3107,7 @@ dependencies = [ "ruma", "serde", "serde_json", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", "tracing-appender", @@ -3155,7 +3155,7 @@ dependencies = [ "serde-wasm-bindgen", "serde_json", "sha2", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", "tracing-subscriber", @@ -3205,7 +3205,7 @@ dependencies = [ "image", "qrcode", "ruma-common", - "thiserror", + "thiserror 1.0.63", "vodozemac", ] @@ -3230,7 +3230,7 @@ dependencies = [ "serde_json", "similar-asserts", "tempfile", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", "vodozemac", @@ -3252,7 +3252,7 @@ dependencies = [ "serde", "serde_json", "sha2", - "thiserror", + "thiserror 1.0.63", "zeroize", ] @@ -3317,7 +3317,7 @@ dependencies = [ "serde_json", "stream_assert", "tempfile", - "thiserror", + "thiserror 1.0.63", "tokio", "tokio-stream", "tracing", @@ -3583,7 +3583,7 @@ dependencies = [ "serde_json", "serde_path_to_error", "sha2", - "thiserror", + "thiserror 1.0.63", "url", ] @@ -3603,7 +3603,7 @@ dependencies = [ "serde_json", "serde_with", "sha2", - "thiserror", + "thiserror 1.0.63", "url", ] @@ -3661,7 +3661,7 @@ dependencies = [ "serde_with", "sha2", "subtle", - "thiserror", + "thiserror 1.0.63", "url", ] @@ -3720,7 +3720,7 @@ dependencies = [ "js-sys", "once_cell", "pin-project-lite", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -3743,7 +3743,7 @@ dependencies = [ "opentelemetry", "percent-encoding", "rand", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -4070,7 +4070,7 @@ dependencies = [ "smallvec", "symbolic-demangle", "tempfile", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -4230,7 +4230,7 @@ dependencies = [ "quinn-udp", "rustc-hash 1.1.0", "rustls", - "thiserror", + "thiserror 1.0.63", "tokio", "tracing", ] @@ -4247,7 +4247,7 @@ dependencies = [ "rustc-hash 2.0.0", "rustls", "slab", - "thiserror", + "thiserror 1.0.63", "tinyvec", "tracing", ] @@ -4384,7 +4384,7 @@ checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" dependencies = [ "getrandom", "libredox", - "thiserror", + "thiserror 1.0.63", ] [[package]] @@ -4580,8 +4580,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94984418ae8a5e1160e6c87608141330e9ae26330abf22e3d15416efa96d48a" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "assign", "js_int", @@ -4598,8 +4597,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "325e054db8d5545c00767d9868356d61e63f2c6cb8b54768346d66696ea4ad48" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "as_variant", "assign", @@ -4614,7 +4612,7 @@ dependencies = [ "serde", "serde_html_form", "serde_json", - "thiserror", + "thiserror 2.0.3", "url", "web-time", ] @@ -4622,8 +4620,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad71c7f49abaa047ba228339d34f9aaefa4d8b50ebeb8e859d0340cc2138bda8" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "as_variant", "base64 0.22.1", @@ -4643,7 +4640,7 @@ dependencies = [ "serde", "serde_html_form", "serde_json", - "thiserror", + "thiserror 2.0.3", "time", "tracing", "url", @@ -4655,8 +4652,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.29.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be86dccf3504588c1f4dc1bda4ce1f8bbd646fc6dda40c77cc7de6e203e62dad" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "as_variant", "indexmap 2.6.0", @@ -4671,7 +4667,7 @@ dependencies = [ "ruma-macros", "serde", "serde_json", - "thiserror", + "thiserror 2.0.3", "tracing", "url", "web-time", @@ -4681,8 +4677,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5a09ac22b3352bf7a350514dc9a87e1b56aba04c326ac9ce142740f7218afa" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "http", "js_int", @@ -4696,8 +4691,7 @@ dependencies = [ [[package]] name = "ruma-html" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7571886b6df90a4ed72e7481a5a39cc2a5b3a4e956e9366ad798e4e2e9fe8005" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "as_variant", "html5ever", @@ -4709,18 +4703,16 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7f9b534a65698d7db3c08d94bf91de0046fe6c7893a7b360502f65e7011ac4" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "js_int", - "thiserror", + "thiserror 2.0.3", ] [[package]] name = "ruma-macros" version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8d57d3cb20e8e758e8f7c5e408ce831d46758003b615100099852e468631934" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "cfg-if", "once_cell", @@ -4736,8 +4728,7 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfced466fbed6277f74ac3887eeb96c185a09f4323dc3c39bcea04870430fe9a" +source = "git+https://github.com/ruma/ruma.git?rev=35fda7f2f7811156df2e60b223dbf136fc143bc8#35fda7f2f7811156df2e60b223dbf136fc143bc8" dependencies = [ "js_int", "ruma-common", @@ -5411,9 +5402,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.79" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -5470,7 +5461,16 @@ version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.63", +] + +[[package]] +name = "thiserror" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +dependencies = [ + "thiserror-impl 2.0.3", ] [[package]] @@ -5484,6 +5484,17 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror-impl" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -5608,7 +5619,7 @@ checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0" dependencies = [ "either", "futures-util", - "thiserror", + "thiserror 1.0.63", "tokio", ] @@ -5763,7 +5774,7 @@ version = "0.2.3" source = "git+https://github.com/element-hq/tracing.git?rev=ca9431f74d37c9d3b5e6a9f35b2c706711dab7dd#ca9431f74d37c9d3b5e6a9f35b2c706711dab7dd" dependencies = [ "crossbeam-channel", - "thiserror", + "thiserror 1.0.63", "time", "tracing-subscriber", ] @@ -6188,7 +6199,7 @@ dependencies = [ "serde_json", "sha2", "subtle", - "thiserror", + "thiserror 1.0.63", "x25519-dalek", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index 1a06ef22aaa..ba6a6caac01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,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 = { version = "0.11.1", features = [ +ruma = { git = "https://github.com/ruma/ruma.git", rev = "35fda7f2f7811156df2e60b223dbf136fc143bc8", features = [ "client-api-c", "compat-upload-signatures", "compat-user-id", @@ -59,7 +59,7 @@ ruma = { version = "0.11.1", features = [ "unstable-msc4075", "unstable-msc4140", ] } -ruma-common = "0.14.1" +ruma-common = { git = "https://github.com/ruma/ruma.git", rev = "35fda7f2f7811156df2e60b223dbf136fc143bc8" } serde = "1.0.151" serde_html_form = "0.2.0" serde_json = "1.0.91" diff --git a/crates/matrix-sdk-base/src/response_processors.rs b/crates/matrix-sdk-base/src/response_processors.rs index a6a35a5446c..512f2bd2f10 100644 --- a/crates/matrix-sdk-base/src/response_processors.rs +++ b/crates/matrix-sdk-base/src/response_processors.rs @@ -18,9 +18,11 @@ use std::{ }; use ruma::{ - events::{AnyGlobalAccountDataEvent, GlobalAccountDataEventType}, + events::{ + direct::OwnedDirectUserIdentifier, AnyGlobalAccountDataEvent, GlobalAccountDataEventType, + }, serde::Raw, - OwnedUserId, RoomId, + RoomId, }; use tracing::{debug, instrument, trace, warn}; @@ -94,10 +96,10 @@ impl AccountDataProcessor { for event in events { let AnyGlobalAccountDataEvent::Direct(direct_event) = event else { continue }; - let mut new_dms = HashMap::<&RoomId, HashSet>::new(); - for (user_id, rooms) in direct_event.content.iter() { + let mut new_dms = HashMap::<&RoomId, HashSet>::new(); + for (user_identifier, rooms) in direct_event.content.iter() { for room_id in rooms { - new_dms.entry(room_id).or_default().insert(user_id.clone()); + new_dms.entry(room_id).or_default().insert(user_identifier.clone()); } } diff --git a/crates/matrix-sdk-base/src/rooms/mod.rs b/crates/matrix-sdk-base/src/rooms/mod.rs index cd884e9c898..7815b216d7d 100644 --- a/crates/matrix-sdk-base/src/rooms/mod.rs +++ b/crates/matrix-sdk-base/src/rooms/mod.rs @@ -21,6 +21,7 @@ use ruma::{ events::{ beacon_info::BeaconInfoEventContent, call::member::{CallMemberEventContent, CallMemberStateKey}, + direct::OwnedDirectUserIdentifier, macros::EventContent, room::{ avatar::RoomAvatarEventContent, @@ -127,7 +128,7 @@ pub struct BaseRoomInfo { pub(crate) create: Option>, /// A list of user ids this room is considered as direct message, if this /// room is a DM. - pub(crate) dm_targets: HashSet, + pub(crate) dm_targets: HashSet, /// The `m.room.encryption` event content that enabled E2EE in this room. pub(crate) encryption: Option, /// The guest access policy of this room. diff --git a/crates/matrix-sdk-base/src/rooms/normal.rs b/crates/matrix-sdk-base/src/rooms/normal.rs index 81bca1b6454..248e5485cb7 100644 --- a/crates/matrix-sdk-base/src/rooms/normal.rs +++ b/crates/matrix-sdk-base/src/rooms/normal.rs @@ -33,6 +33,7 @@ use ruma::{ api::client::sync::sync_events::v3::RoomSummary as RumaSummary, events::{ call::member::{CallMemberStateKey, MembershipData}, + direct::OwnedDirectUserIdentifier, ignored_user_list::IgnoredUserListEventContent, receipt::{Receipt, ReceiptThread, ReceiptType}, room::{ @@ -460,7 +461,7 @@ impl Room { /// only be considered as guidance. We leave members in this list to allow /// us to re-find a DM with a user even if they have left, since we may /// want to re-invite them. - pub fn direct_targets(&self) -> HashSet { + pub fn direct_targets(&self) -> HashSet { self.inner.read().base_info.dm_targets.clone() } diff --git a/crates/matrix-sdk-base/src/sliding_sync/mod.rs b/crates/matrix-sdk-base/src/sliding_sync/mod.rs index 9350492a098..80006d084a5 100644 --- a/crates/matrix-sdk-base/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk-base/src/sliding_sync/mod.rs @@ -905,7 +905,7 @@ mod tests { api::client::sync::sync_events::UnreadNotificationsCount, assign, event_id, events::{ - direct::DirectEventContent, + direct::{DirectEventContent, DirectUserIdentifier, OwnedDirectUserIdentifier}, room::{ avatar::RoomAvatarEventContent, canonical_alias::RoomCanonicalAliasEventContent, @@ -1331,7 +1331,7 @@ mod tests { create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Join).await; // (Sanity: B is a direct target, and is in Join state) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join); // When B leaves @@ -1340,7 +1340,7 @@ mod tests { // Then B is still a direct target, and is in Leave state (B is a direct target // because we want to return to our old DM in the UI even if the other // user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave); } @@ -1356,7 +1356,7 @@ mod tests { create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Invite).await; // (Sanity: B is a direct target, and is in Invite state) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite); // When B declines the invitation (i.e. leaves) @@ -1365,7 +1365,7 @@ mod tests { // Then B is still a direct target, and is in Leave state (B is a direct target // because we want to return to our old DM in the UI even if the other // user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave); } @@ -1383,7 +1383,7 @@ mod tests { assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join); // (Sanity: B is a direct target, and is in Join state) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join); let room = client.get_room(room_id).unwrap(); @@ -1407,7 +1407,7 @@ mod tests { assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join); // (Sanity: B is a direct target, and is in Join state) - assert!(direct_targets(&client, room_id).contains(user_b_id)); + assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id))); assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite); let room = client.get_room(room_id).unwrap(); @@ -2552,9 +2552,10 @@ mod tests { let mut room_response = http::response::Room::new(); set_room_joined(&mut room_response, user_a_id); let mut response = response_with_room(room_id_1, room_response); - let mut direct_content = BTreeMap::new(); - direct_content.insert(user_a_id.to_owned(), vec![room_id_1.to_owned()]); - direct_content.insert(user_b_id.to_owned(), vec![room_id_2.to_owned()]); + let mut direct_content: BTreeMap> = + BTreeMap::new(); + direct_content.insert(user_a_id.into(), vec![room_id_1.to_owned()]); + direct_content.insert(user_b_id.into(), vec![room_id_2.to_owned()]); response .extensions .account_data @@ -2665,7 +2666,7 @@ mod tests { member.membership().clone() } - fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet { + fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet { let room = client.get_room(room_id).expect("Room not found!"); room.direct_targets() } @@ -2724,8 +2725,9 @@ mod tests { user_id: OwnedUserId, room_ids: Vec, ) { - let mut direct_content = BTreeMap::new(); - direct_content.insert(user_id, room_ids); + let mut direct_content: BTreeMap> = + BTreeMap::new(); + direct_content.insert(user_id.into(), room_ids); response .extensions .account_data diff --git a/crates/matrix-sdk-base/src/store/migration_helpers.rs b/crates/matrix-sdk-base/src/store/migration_helpers.rs index 76acb86cf48..1943b6578ae 100644 --- a/crates/matrix-sdk-base/src/store/migration_helpers.rs +++ b/crates/matrix-sdk-base/src/store/migration_helpers.rs @@ -23,6 +23,7 @@ use std::{ use matrix_sdk_common::deserialized_responses::SyncTimelineEvent; use ruma::{ events::{ + direct::OwnedDirectUserIdentifier, room::{ avatar::RoomAvatarEventContent, canonical_alias::RoomCanonicalAliasEventContent, @@ -37,7 +38,7 @@ use ruma::{ }, EmptyStateKey, EventContent, RedactContent, StateEventContent, StateEventType, }, - OwnedRoomId, OwnedUserId, RoomId, + OwnedRoomId, RoomId, }; use serde::{Deserialize, Serialize}; @@ -155,7 +156,7 @@ fn encryption_state_default() -> bool { struct BaseRoomInfoV1 { avatar: Option>, canonical_alias: Option>, - dm_targets: HashSet, + dm_targets: HashSet, encryption: Option, guest_access: Option>, history_visibility: Option>, diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index 2e08bb64154..b077d610641 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -861,7 +861,7 @@ impl Account { }; for user_id in user_ids { - content.entry(user_id.to_owned()).or_default().push(room_id.to_owned()); + content.entry(user_id.into()).or_default().push(room_id.to_owned()); } // TODO: We should probably save the fact that we need to send this out diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index a22ebee2c00..bb6ef4795cb 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -47,7 +47,10 @@ use ruma::{ uiaa::{AuthData, UiaaInfo}, }, assign, - events::room::{MediaSource, ThumbnailInfo}, + events::{ + direct::DirectUserIdentifier, + room::{MediaSource, ThumbnailInfo}, + }, DeviceId, OwnedDeviceId, OwnedUserId, TransactionId, UserId, }; use serde::Deserialize; @@ -607,7 +610,7 @@ impl Client { // Find the room we share with the `user_id` and only with `user_id` let room = rooms.into_iter().find(|r| { let targets = r.direct_targets(); - targets.len() == 1 && targets.contains(user_id) + targets.len() == 1 && targets.contains(<&DirectUserIdentifier>::from(user_id)) }); trace!(?room, "Found room"); diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 00484d31457..a0ba7c32c50 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -1205,7 +1205,7 @@ impl Room { room_members.retain(|member| member.user_id() != self.own_user_id()); for member in room_members { - let entry = content.entry(member.user_id().to_owned()).or_default(); + let entry = content.entry(member.user_id().into()).or_default(); if !entry.iter().any(|room_id| room_id == this_room_id) { entry.push(this_room_id.to_owned()); } diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index a8d7a9e1115..2269d0018fb 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -35,7 +35,10 @@ use ruma::{ assign, device_id, directory::Filter, event_id, - events::{direct::DirectEventContent, AnyInitialStateEvent}, + events::{ + direct::{DirectEventContent, OwnedDirectUserIdentifier}, + AnyInitialStateEvent, + }, room_id, serde::Raw, user_id, OwnedUserId, @@ -496,7 +499,9 @@ async fn test_marking_room_as_dm() { "The body of the PUT /account_data request should be a valid DirectEventContent", ); - let bob_entry = content.get(bob).expect("We should have bob in the direct event content"); + let bob_entry = content + .get(&OwnedDirectUserIdentifier::from(bob.to_owned())) + .expect("We should have bob in the direct event content"); assert_eq!(content.len(), 2, "We should have entries for bob and foo"); assert_eq!(bob_entry.len(), 3, "Bob should have 3 direct rooms"); diff --git a/crates/matrix-sdk/tests/integration/room/common.rs b/crates/matrix-sdk/tests/integration/room/common.rs index 33efb813eac..76465cb2c0e 100644 --- a/crates/matrix-sdk/tests/integration/room/common.rs +++ b/crates/matrix-sdk/tests/integration/room/common.rs @@ -14,6 +14,7 @@ use matrix_sdk_test::{ use ruma::{ event_id, events::{ + direct::DirectUserIdentifier, room::{ avatar::{self, RoomAvatarEventContent}, member::MembershipState, @@ -833,7 +834,7 @@ async fn test_is_direct() { // The room is direct now. let direct_targets = room.direct_targets(); assert_eq!(direct_targets.len(), 1); - assert!(direct_targets.contains(*BOB)); + assert!(direct_targets.contains(<&DirectUserIdentifier>::from(*BOB))); assert!(room.is_direct().await.unwrap()); // Unset the room as direct. diff --git a/crates/matrix-sdk/tests/integration/room/joined.rs b/crates/matrix-sdk/tests/integration/room/joined.rs index 2c81f1caaca..85a7a9fcfc0 100644 --- a/crates/matrix-sdk/tests/integration/room/joined.rs +++ b/crates/matrix-sdk/tests/integration/room/joined.rs @@ -21,13 +21,14 @@ use ruma::{ api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType}, assign, event_id, events::{ + direct::DirectUserIdentifier, receipt::ReceiptThread, room::message::{RoomMessageEventContent, RoomMessageEventContentWithoutRelation}, TimelineEventType, }, int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, TransactionId, }; -use serde_json::{json, Value}; +use serde_json::{from_value, json, Value}; use wiremock::{ matchers::{body_json, body_partial_json, header, method, path_regex}, Mock, ResponseTemplate, @@ -630,6 +631,38 @@ async fn test_reset_power_levels() { room.reset_power_levels().await.unwrap(); } +#[async_test] +async fn test_is_direct_invite_by_3pid() { + let (client, server) = logged_in_client_with_server().await; + + let mut sync_builder = SyncResponseBuilder::new(); + sync_builder.add_joined_room(JoinedRoomBuilder::default()); + let data = json!({ + "content": { + "invited@localhost.com": [*DEFAULT_TEST_ROOM_ID], + }, + "event_id": "$757957878228ekrDs:localhost", + "origin_server_ts": 17195787, + "sender": "@example:localhost", + "state_key": "", + "type": "m.direct", + "unsigned": { + "age": 139298 + } + }); + sync_builder.add_global_account_data_bulk(vec![from_value(data).unwrap()]); + + mock_sync(&server, sync_builder.build_json_sync_response(), None).await; + mock_encryption_state(&server, false).await; + + let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); + let _response = client.sync_once(sync_settings).await.unwrap(); + + let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap(); + assert!(room.is_direct().await.unwrap()); + assert!(room.direct_targets().contains(<&DirectUserIdentifier>::from("invited@localhost.com"))); +} + #[async_test] async fn test_call_notifications_ring_for_dms() { let (client, server) = logged_in_client_with_server().await; diff --git a/crates/matrix-sdk/tests/integration/room/left.rs b/crates/matrix-sdk/tests/integration/room/left.rs index f2de95649b5..accd7aff0d0 100644 --- a/crates/matrix-sdk/tests/integration/room/left.rs +++ b/crates/matrix-sdk/tests/integration/room/left.rs @@ -7,7 +7,10 @@ use matrix_sdk_test::{ async_test, test_json, GlobalAccountDataTestEvent, LeftRoomBuilder, SyncResponseBuilder, DEFAULT_TEST_ROOM_ID, }; -use ruma::{events::direct::DirectEventContent, user_id, OwnedRoomOrAliasId}; +use ruma::{ + events::direct::{DirectEventContent, DirectUserIdentifier, OwnedDirectUserIdentifier}, + user_id, OwnedRoomOrAliasId, +}; use serde_json::json; use wiremock::{ matchers::{header, method, path, path_regex}, @@ -70,7 +73,7 @@ async fn test_forget_direct_room() { let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap(); assert_eq!(room.state(), RoomState::Left); assert!(room.is_direct().await.unwrap()); - assert!(room.direct_targets().contains(invited_user_id)); + assert!(room.direct_targets().contains(<&DirectUserIdentifier>::from(invited_user_id))); let direct_account_data = client .account() @@ -80,7 +83,10 @@ async fn test_forget_direct_room() { .expect("no m.direct account data") .deserialize() .expect("failed to deserialize m.direct account data"); - assert_matches!(direct_account_data.get(invited_user_id), Some(invited_user_dms)); + assert_matches!( + direct_account_data.get(&OwnedDirectUserIdentifier::from(invited_user_id)), + Some(invited_user_dms) + ); assert_eq!(invited_user_dms, &[DEFAULT_TEST_ROOM_ID.to_owned()]); Mock::given(method("POST"))