Skip to content

Commit 2022ae9

Browse files
committed
Accept any string as a key for m.direct account data
1 parent aca83fb commit 2022ae9

File tree

14 files changed

+154
-88
lines changed

14 files changed

+154
-88
lines changed

Cargo.lock

Lines changed: 64 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ once_cell = "1.16.0"
4545
pin-project-lite = "0.2.9"
4646
rand = "0.8.5"
4747
reqwest = { version = "0.12.4", default-features = false }
48-
ruma = { version = "0.11.1", features = [
48+
ruma = { git = "https://github.com/ruma/ruma.git", rev = "35fda7f2f7811156df2e60b223dbf136fc143bc8", features = [
4949
"client-api-c",
5050
"compat-upload-signatures",
5151
"compat-user-id",
@@ -59,7 +59,7 @@ ruma = { version = "0.11.1", features = [
5959
"unstable-msc4075",
6060
"unstable-msc4140",
6161
] }
62-
ruma-common = "0.14.1"
62+
ruma-common = { git = "https://github.com/ruma/ruma.git", rev = "35fda7f2f7811156df2e60b223dbf136fc143bc8" }
6363
serde = "1.0.151"
6464
serde_html_form = "0.2.0"
6565
serde_json = "1.0.91"

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use std::{
1818
};
1919

2020
use ruma::{
21-
events::{AnyGlobalAccountDataEvent, GlobalAccountDataEventType},
21+
events::{
22+
direct::OwnedDirectUserIdentifier, AnyGlobalAccountDataEvent, GlobalAccountDataEventType,
23+
},
2224
serde::Raw,
23-
OwnedUserId, RoomId,
25+
RoomId,
2426
};
2527
use tracing::{debug, instrument, trace, warn};
2628

@@ -94,10 +96,10 @@ impl AccountDataProcessor {
9496
for event in events {
9597
let AnyGlobalAccountDataEvent::Direct(direct_event) = event else { continue };
9698

97-
let mut new_dms = HashMap::<&RoomId, HashSet<OwnedUserId>>::new();
98-
for (user_id, rooms) in direct_event.content.iter() {
99+
let mut new_dms = HashMap::<&RoomId, HashSet<OwnedDirectUserIdentifier>>::new();
100+
for (user_identifier, rooms) in direct_event.content.iter() {
99101
for room_id in rooms {
100-
new_dms.entry(room_id).or_default().insert(user_id.clone());
102+
new_dms.entry(room_id).or_default().insert(user_identifier.clone());
101103
}
102104
}
103105

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ruma::{
2121
events::{
2222
beacon_info::BeaconInfoEventContent,
2323
call::member::{CallMemberEventContent, CallMemberStateKey},
24+
direct::OwnedDirectUserIdentifier,
2425
macros::EventContent,
2526
room::{
2627
avatar::RoomAvatarEventContent,
@@ -127,7 +128,7 @@ pub struct BaseRoomInfo {
127128
pub(crate) create: Option<MinimalStateEvent<RoomCreateWithCreatorEventContent>>,
128129
/// A list of user ids this room is considered as direct message, if this
129130
/// room is a DM.
130-
pub(crate) dm_targets: HashSet<OwnedUserId>,
131+
pub(crate) dm_targets: HashSet<OwnedDirectUserIdentifier>,
131132
/// The `m.room.encryption` event content that enabled E2EE in this room.
132133
pub(crate) encryption: Option<RoomEncryptionEventContent>,
133134
/// The guest access policy of this room.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use ruma::{
3333
api::client::sync::sync_events::v3::RoomSummary as RumaSummary,
3434
events::{
3535
call::member::{CallMemberStateKey, MembershipData},
36+
direct::OwnedDirectUserIdentifier,
3637
ignored_user_list::IgnoredUserListEventContent,
3738
receipt::{Receipt, ReceiptThread, ReceiptType},
3839
room::{
@@ -460,7 +461,7 @@ impl Room {
460461
/// only be considered as guidance. We leave members in this list to allow
461462
/// us to re-find a DM with a user even if they have left, since we may
462463
/// want to re-invite them.
463-
pub fn direct_targets(&self) -> HashSet<OwnedUserId> {
464+
pub fn direct_targets(&self) -> HashSet<OwnedDirectUserIdentifier> {
464465
self.inner.read().base_info.dm_targets.clone()
465466
}
466467

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, DirectUserIdentifier, 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(<&DirectUserIdentifier>::from(user_b_id)));
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(<&DirectUserIdentifier>::from(user_b_id)));
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(<&DirectUserIdentifier>::from(user_b_id)));
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(<&DirectUserIdentifier>::from(user_b_id)));
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(<&DirectUserIdentifier>::from(user_b_id)));
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(<&DirectUserIdentifier>::from(user_b_id)));
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<OwnedDirectUserIdentifier> {
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::{
2323
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
2424
use ruma::{
2525
events::{
26+
direct::OwnedDirectUserIdentifier,
2627
room::{
2728
avatar::RoomAvatarEventContent,
2829
canonical_alias::RoomCanonicalAliasEventContent,
@@ -37,7 +38,7 @@ use ruma::{
3738
},
3839
EmptyStateKey, EventContent, RedactContent, StateEventContent, StateEventType,
3940
},
40-
OwnedRoomId, OwnedUserId, RoomId,
41+
OwnedRoomId, RoomId,
4142
};
4243
use serde::{Deserialize, Serialize};
4344

@@ -155,7 +156,7 @@ fn encryption_state_default() -> bool {
155156
struct BaseRoomInfoV1 {
156157
avatar: Option<MinimalStateEvent<RoomAvatarEventContent>>,
157158
canonical_alias: Option<MinimalStateEvent<RoomCanonicalAliasEventContent>>,
158-
dm_targets: HashSet<OwnedUserId>,
159+
dm_targets: HashSet<OwnedDirectUserIdentifier>,
159160
encryption: Option<RoomEncryptionEventContent>,
160161
guest_access: Option<MinimalStateEvent<RoomGuestAccessEventContent>>,
161162
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ use ruma::{
4747
uiaa::{AuthData, UiaaInfo},
4848
},
4949
assign,
50-
events::room::{MediaSource, ThumbnailInfo},
50+
events::{
51+
direct::DirectUserIdentifier,
52+
room::{MediaSource, ThumbnailInfo},
53+
},
5154
DeviceId, OwnedDeviceId, OwnedUserId, TransactionId, UserId,
5255
};
5356
use serde::Deserialize;
@@ -607,7 +610,7 @@ impl Client {
607610
// Find the room we share with the `user_id` and only with `user_id`
608611
let room = rooms.into_iter().find(|r| {
609612
let targets = r.direct_targets();
610-
targets.len() == 1 && targets.contains(user_id)
613+
targets.len() == 1 && targets.contains(<&DirectUserIdentifier>::from(user_id))
611614
});
612615

613616
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use matrix_sdk_test::{
1414
use ruma::{
1515
event_id,
1616
events::{
17+
direct::DirectUserIdentifier,
1718
room::{
1819
avatar::{self, RoomAvatarEventContent},
1920
member::MembershipState,
@@ -833,7 +834,7 @@ async fn test_is_direct() {
833834
// The room is direct now.
834835
let direct_targets = room.direct_targets();
835836
assert_eq!(direct_targets.len(), 1);
836-
assert!(direct_targets.contains(*BOB));
837+
assert!(direct_targets.contains(<&DirectUserIdentifier>::from(*BOB)));
837838
assert!(room.is_direct().await.unwrap());
838839

839840
// Unset the room as direct.

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ use ruma::{
2121
api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType},
2222
assign, event_id,
2323
events::{
24+
direct::DirectUserIdentifier,
2425
receipt::ReceiptThread,
2526
room::message::{RoomMessageEventContent, RoomMessageEventContentWithoutRelation},
2627
TimelineEventType,
2728
},
2829
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, TransactionId,
2930
};
30-
use serde_json::{json, Value};
31+
use serde_json::{from_value, json, Value};
3132
use wiremock::{
3233
matchers::{body_json, body_partial_json, header, method, path_regex},
3334
Mock, ResponseTemplate,
@@ -630,6 +631,38 @@ async fn test_reset_power_levels() {
630631
room.reset_power_levels().await.unwrap();
631632
}
632633

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