Skip to content

Commit cd0e760

Browse files
committed
Accept any string as a key for m.direct account data
1 parent 982c6ea commit cd0e760

File tree

12 files changed

+75
-30
lines changed

12 files changed

+75
-30
lines changed

crates/matrix-sdk-base/src/response_processors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{
2020
use ruma::{
2121
events::{AnyGlobalAccountDataEvent, GlobalAccountDataEventType},
2222
serde::Raw,
23-
OwnedUserId, RoomId,
23+
RoomId,
2424
};
2525
use tracing::{debug, instrument, trace, warn};
2626

@@ -94,10 +94,10 @@ impl AccountDataProcessor {
9494
for event in events {
9595
let AnyGlobalAccountDataEvent::Direct(direct_event) = event else { continue };
9696

97-
let mut new_dms = HashMap::<&RoomId, HashSet<OwnedUserId>>::new();
97+
let mut new_dms = HashMap::<&RoomId, HashSet<String>>::new();
9898
for (user_id, rooms) in direct_event.content.iter() {
9999
for room_id in rooms {
100-
new_dms.entry(room_id).or_default().insert(user_id.clone());
100+
new_dms.entry(room_id).or_default().insert(user_id.to_string());
101101
}
102102
}
103103

crates/matrix-sdk-base/src/rooms/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct BaseRoomInfo {
127127
pub(crate) create: Option<MinimalStateEvent<RoomCreateWithCreatorEventContent>>,
128128
/// A list of user ids this room is considered as direct message, if this
129129
/// room is a DM.
130-
pub(crate) dm_targets: HashSet<OwnedUserId>,
130+
pub(crate) dm_targets: HashSet<String>,
131131
/// The `m.room.encryption` event content that enabled E2EE in this room.
132132
pub(crate) encryption: Option<RoomEncryptionEventContent>,
133133
/// The guest access policy of this room.

crates/matrix-sdk-base/src/rooms/normal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl Room {
460460
/// only be considered as guidance. We leave members in this list to allow
461461
/// us to re-find a DM with a user even if they have left, since we may
462462
/// want to re-invite them.
463-
pub fn direct_targets(&self) -> HashSet<OwnedUserId> {
463+
pub fn direct_targets(&self) -> HashSet<String> {
464464
self.inner.read().base_info.dm_targets.clone()
465465
}
466466

crates/matrix-sdk-base/src/sliding_sync/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ mod tests {
905905
api::client::sync::sync_events::UnreadNotificationsCount,
906906
assign, event_id,
907907
events::{
908-
direct::DirectEventContent,
908+
direct::{DirectEventContent, OwnedDirectUserIdentifier},
909909
room::{
910910
avatar::RoomAvatarEventContent,
911911
canonical_alias::RoomCanonicalAliasEventContent,
@@ -1331,7 +1331,7 @@ mod tests {
13311331
create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Join).await;
13321332

13331333
// (Sanity: B is a direct target, and is in Join state)
1334-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1334+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
13351335
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join);
13361336

13371337
// When B leaves
@@ -1340,7 +1340,7 @@ mod tests {
13401340
// Then B is still a direct target, and is in Leave state (B is a direct target
13411341
// because we want to return to our old DM in the UI even if the other
13421342
// user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017)
1343-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1343+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
13441344
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave);
13451345
}
13461346

@@ -1356,7 +1356,7 @@ mod tests {
13561356
create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Invite).await;
13571357

13581358
// (Sanity: B is a direct target, and is in Invite state)
1359-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1359+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
13601360
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite);
13611361

13621362
// When B declines the invitation (i.e. leaves)
@@ -1365,7 +1365,7 @@ mod tests {
13651365
// Then B is still a direct target, and is in Leave state (B is a direct target
13661366
// because we want to return to our old DM in the UI even if the other
13671367
// user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017)
1368-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1368+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
13691369
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave);
13701370
}
13711371

@@ -1383,7 +1383,7 @@ mod tests {
13831383
assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join);
13841384

13851385
// (Sanity: B is a direct target, and is in Join state)
1386-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1386+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
13871387
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join);
13881388

13891389
let room = client.get_room(room_id).unwrap();
@@ -1407,7 +1407,7 @@ mod tests {
14071407
assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join);
14081408

14091409
// (Sanity: B is a direct target, and is in Join state)
1410-
assert!(direct_targets(&client, room_id).contains(user_b_id));
1410+
assert!(direct_targets(&client, room_id).contains(user_b_id.as_str()));
14111411
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite);
14121412

14131413
let room = client.get_room(room_id).unwrap();
@@ -2552,9 +2552,10 @@ mod tests {
25522552
let mut room_response = http::response::Room::new();
25532553
set_room_joined(&mut room_response, user_a_id);
25542554
let mut response = response_with_room(room_id_1, room_response);
2555-
let mut direct_content = BTreeMap::new();
2556-
direct_content.insert(user_a_id.to_owned(), vec![room_id_1.to_owned()]);
2557-
direct_content.insert(user_b_id.to_owned(), vec![room_id_2.to_owned()]);
2555+
let mut direct_content: BTreeMap<OwnedDirectUserIdentifier, Vec<OwnedRoomId>> =
2556+
BTreeMap::new();
2557+
direct_content.insert(user_a_id.into(), vec![room_id_1.to_owned()]);
2558+
direct_content.insert(user_b_id.into(), vec![room_id_2.to_owned()]);
25582559
response
25592560
.extensions
25602561
.account_data
@@ -2665,7 +2666,7 @@ mod tests {
26652666
member.membership().clone()
26662667
}
26672668

2668-
fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet<OwnedUserId> {
2669+
fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet<String> {
26692670
let room = client.get_room(room_id).expect("Room not found!");
26702671
room.direct_targets()
26712672
}
@@ -2724,8 +2725,9 @@ mod tests {
27242725
user_id: OwnedUserId,
27252726
room_ids: Vec<OwnedRoomId>,
27262727
) {
2727-
let mut direct_content = BTreeMap::new();
2728-
direct_content.insert(user_id, room_ids);
2728+
let mut direct_content: BTreeMap<OwnedDirectUserIdentifier, Vec<OwnedRoomId>> =
2729+
BTreeMap::new();
2730+
direct_content.insert(user_id.into(), room_ids);
27292731
response
27302732
.extensions
27312733
.account_data

crates/matrix-sdk-base/src/store/migration_helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use ruma::{
3737
},
3838
EmptyStateKey, EventContent, RedactContent, StateEventContent, StateEventType,
3939
},
40-
OwnedRoomId, OwnedUserId, RoomId,
40+
OwnedRoomId, RoomId,
4141
};
4242
use serde::{Deserialize, Serialize};
4343

@@ -155,7 +155,7 @@ fn encryption_state_default() -> bool {
155155
struct BaseRoomInfoV1 {
156156
avatar: Option<MinimalStateEvent<RoomAvatarEventContent>>,
157157
canonical_alias: Option<MinimalStateEvent<RoomCanonicalAliasEventContent>>,
158-
dm_targets: HashSet<OwnedUserId>,
158+
dm_targets: HashSet<String>,
159159
encryption: Option<RoomEncryptionEventContent>,
160160
guest_access: Option<MinimalStateEvent<RoomGuestAccessEventContent>>,
161161
history_visibility: Option<MinimalStateEvent<RoomHistoryVisibilityEventContent>>,

crates/matrix-sdk/src/account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl Account {
861861
};
862862

863863
for user_id in user_ids {
864-
content.entry(user_id.to_owned()).or_default().push(room_id.to_owned());
864+
content.entry(user_id.into()).or_default().push(room_id.to_owned());
865865
}
866866

867867
// TODO: We should probably save the fact that we need to send this out

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl Client {
607607
// Find the room we share with the `user_id` and only with `user_id`
608608
let room = rooms.into_iter().find(|r| {
609609
let targets = r.direct_targets();
610-
targets.len() == 1 && targets.contains(user_id)
610+
targets.len() == 1 && targets.contains(user_id.as_str())
611611
});
612612

613613
trace!(?room, "Found room");

crates/matrix-sdk/src/room/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ impl Room {
12051205
room_members.retain(|member| member.user_id() != self.own_user_id());
12061206

12071207
for member in room_members {
1208-
let entry = content.entry(member.user_id().to_owned()).or_default();
1208+
let entry = content.entry(member.user_id().into()).or_default();
12091209
if !entry.iter().any(|room_id| room_id == this_room_id) {
12101210
entry.push(this_room_id.to_owned());
12111211
}

crates/matrix-sdk/tests/integration/client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ use ruma::{
3535
assign, device_id,
3636
directory::Filter,
3737
event_id,
38-
events::{direct::DirectEventContent, AnyInitialStateEvent},
38+
events::{
39+
direct::{DirectEventContent, OwnedDirectUserIdentifier},
40+
AnyInitialStateEvent,
41+
},
3942
room_id,
4043
serde::Raw,
4144
user_id, OwnedUserId,
@@ -496,7 +499,9 @@ async fn test_marking_room_as_dm() {
496499
"The body of the PUT /account_data request should be a valid DirectEventContent",
497500
);
498501

499-
let bob_entry = content.get(bob).expect("We should have bob in the direct event content");
502+
let bob_entry = content
503+
.get(&OwnedDirectUserIdentifier::from(bob.to_owned()))
504+
.expect("We should have bob in the direct event content");
500505

501506
assert_eq!(content.len(), 2, "We should have entries for bob and foo");
502507
assert_eq!(bob_entry.len(), 3, "Bob should have 3 direct rooms");

crates/matrix-sdk/tests/integration/room/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ async fn test_is_direct() {
833833
// The room is direct now.
834834
let direct_targets = room.direct_targets();
835835
assert_eq!(direct_targets.len(), 1);
836-
assert!(direct_targets.contains(*BOB));
836+
assert!(direct_targets.contains(BOB.as_str()));
837837
assert!(room.is_direct().await.unwrap());
838838

839839
// Unset the room as direct.

crates/matrix-sdk/tests/integration/room/joined.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use ruma::{
2727
},
2828
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, TransactionId,
2929
};
30-
use serde_json::{json, Value};
30+
use serde_json::{from_value, json, Value};
3131
use wiremock::{
3232
matchers::{body_json, body_partial_json, header, method, path_regex},
3333
Mock, ResponseTemplate,
@@ -630,6 +630,38 @@ async fn test_reset_power_levels() {
630630
room.reset_power_levels().await.unwrap();
631631
}
632632

633+
#[async_test]
634+
async fn test_is_direct_invite_by_3pid() {
635+
let (client, server) = logged_in_client_with_server().await;
636+
637+
let mut sync_builder = SyncResponseBuilder::new();
638+
sync_builder.add_joined_room(JoinedRoomBuilder::default());
639+
let data = json!({
640+
"content": {
641+
"invited@localhost.com": [*DEFAULT_TEST_ROOM_ID],
642+
},
643+
"event_id": "$757957878228ekrDs:localhost",
644+
"origin_server_ts": 17195787,
645+
"sender": "@example:localhost",
646+
"state_key": "",
647+
"type": "m.direct",
648+
"unsigned": {
649+
"age": 139298
650+
}
651+
});
652+
sync_builder.add_global_account_data_bulk(vec![from_value(data).unwrap()]);
653+
654+
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
655+
mock_encryption_state(&server, false).await;
656+
657+
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
658+
let _response = client.sync_once(sync_settings).await.unwrap();
659+
660+
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
661+
assert!(room.is_direct().await.unwrap());
662+
assert!(room.direct_targets().contains("invited@localhost.com"));
663+
}
664+
633665
#[async_test]
634666
async fn test_call_notifications_ring_for_dms() {
635667
let (client, server) = logged_in_client_with_server().await;

crates/matrix-sdk/tests/integration/room/left.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use matrix_sdk_test::{
77
async_test, test_json, GlobalAccountDataTestEvent, LeftRoomBuilder, SyncResponseBuilder,
88
DEFAULT_TEST_ROOM_ID,
99
};
10-
use ruma::{events::direct::DirectEventContent, user_id, OwnedRoomOrAliasId};
10+
use ruma::{
11+
events::direct::{DirectEventContent, OwnedDirectUserIdentifier},
12+
user_id, OwnedRoomOrAliasId,
13+
};
1114
use serde_json::json;
1215
use wiremock::{
1316
matchers::{header, method, path, path_regex},
@@ -70,7 +73,7 @@ async fn test_forget_direct_room() {
7073
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
7174
assert_eq!(room.state(), RoomState::Left);
7275
assert!(room.is_direct().await.unwrap());
73-
assert!(room.direct_targets().contains(invited_user_id));
76+
assert!(room.direct_targets().contains(invited_user_id.as_str()));
7477

7578
let direct_account_data = client
7679
.account()
@@ -80,7 +83,10 @@ async fn test_forget_direct_room() {
8083
.expect("no m.direct account data")
8184
.deserialize()
8285
.expect("failed to deserialize m.direct account data");
83-
assert_matches!(direct_account_data.get(invited_user_id), Some(invited_user_dms));
86+
assert_matches!(
87+
direct_account_data.get(&OwnedDirectUserIdentifier::from(invited_user_id)),
88+
Some(invited_user_dms)
89+
);
8490
assert_eq!(invited_user_dms, &[DEFAULT_TEST_ROOM_ID.to_owned()]);
8591

8692
Mock::given(method("POST"))

0 commit comments

Comments
 (0)