Skip to content

Commit 5370b93

Browse files
committed
refactor(live_location_share) : remove the event_cache early return
1 parent 543555f commit 5370b93

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

crates/matrix-sdk/src/live_location_share.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,21 @@ impl LiveLocationShares {
116116
async fn get_initial_live_location_shares(
117117
room: &Room,
118118
) -> crate::Result<Vector<LiveLocationShare>> {
119-
let Ok((event_cache, _handles)) = room.event_cache().await else {
120-
return Ok(Vector::new());
121-
};
119+
// Beacon infos are stored in the state store, not the event cache.
122120
let beacon_infos = room.get_state_events_static::<BeaconInfoEventContent>().await?;
121+
// Event cache is only needed for finding last location (optional).
122+
let event_cache = room.event_cache().await.ok().map(|(cache, _handles)| cache);
123123
let mut shares = Vector::new();
124124
for raw_beacon_info in beacon_infos {
125125
let Ok(event) = raw_beacon_info.deserialize() else { continue };
126126
let Some((user_id, beacon_info, event_id)) = Self::extract_live_beacon_info(event)
127127
else {
128128
continue;
129129
};
130-
let last_location = Self::find_last_location(&event_cache, &event_id).await;
130+
let last_location = match &event_cache {
131+
Some(cache) => Self::find_last_location(cache, &event_id).await,
132+
None => None,
133+
};
131134
shares.push_back(LiveLocationShare {
132135
user_id,
133136
beacon_info,

crates/matrix-sdk/tests/integration/room/beacon/mod.rs

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ async fn test_observe_single_live_location_share() {
313313
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
314314
server.reset().await;
315315

316-
// Enable the event cache so the initial snapshot can be loaded from it.
317-
client.event_cache().subscribe().unwrap();
318-
319316
let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();
320317
let live_location_shares = room.subscribe_to_live_location_shares().await;
321318
let (initial, stream) = live_location_shares.subscribe();
@@ -444,9 +441,6 @@ async fn test_location_update_for_already_tracked_user() {
444441
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
445442
server.reset().await;
446443

447-
// Enable the event cache so the initial snapshot can be loaded from it.
448-
client.event_cache().subscribe().unwrap();
449-
450444
let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();
451445
let live_location_shares = room.subscribe_to_live_location_shares().await;
452446
let (initial, stream) = live_location_shares.subscribe();
@@ -537,9 +531,6 @@ async fn test_beacon_info_stop_removes_user_from_stream() {
537531
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
538532
server.reset().await;
539533

540-
// Enable the event cache so the initial snapshot can be loaded from it.
541-
client.event_cache().subscribe().unwrap();
542-
543534
let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();
544535
let live_location_shares = room.subscribe_to_live_location_shares().await;
545536
let (initial, stream) = live_location_shares.subscribe();
@@ -549,28 +540,6 @@ async fn test_beacon_info_stop_removes_user_from_stream() {
549540
assert_eq!(initial.len(), 1);
550541
assert!(initial[0].last_location.is_none());
551542

552-
// Alice's beacon arrives — updates the existing share with last_location.
553-
sync_builder.add_joined_room(
554-
JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID).add_timeline_event(
555-
f.beacon(owned_event_id!("$alice_beacon_info"), 10.0, 20.0, 5, None)
556-
.event_id(event_id!("$loc1"))
557-
.sender(user_id!("@alice:localhost"))
558-
.server_ts(now)
559-
.into_raw_sync(),
560-
),
561-
);
562-
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
563-
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
564-
server.reset().await;
565-
566-
let diffs = stream.next().await.expect("Expected alice added");
567-
let shares = diffs.into_iter().fold(initial, |mut v, diff| {
568-
diff.apply(&mut v);
569-
v
570-
});
571-
assert_eq!(shares.len(), 1);
572-
assert_eq!(shares[0].user_id, user_id!("@alice:localhost"));
573-
574543
// Alice stops her share — beacon_info timeline event with live: false.
575544
sync_builder.add_joined_room(
576545
JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID).add_timeline_event(
@@ -587,7 +556,7 @@ async fn test_beacon_info_stop_removes_user_from_stream() {
587556

588557
// Stream emits an update with an empty list — alice is removed.
589558
let diffs = stream.next().await.expect("Expected share removal");
590-
let shares = diffs.into_iter().fold(shares, |mut v, diff| {
559+
let shares = diffs.into_iter().fold(initial, |mut v, diff| {
591560
diff.apply(&mut v);
592561
v
593562
});
@@ -630,9 +599,6 @@ async fn test_multiple_users_in_stream() {
630599
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
631600
server.reset().await;
632601

633-
// Enable the event cache so the initial snapshot can be loaded from it.
634-
client.event_cache().subscribe().unwrap();
635-
636602
let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();
637603
let live_location_shares = room.subscribe_to_live_location_shares().await;
638604
let (initial, stream) = live_location_shares.subscribe();
@@ -697,3 +663,64 @@ async fn test_multiple_users_in_stream() {
697663
"geo:50,60;u=8"
698664
);
699665
}
666+
667+
#[async_test]
668+
async fn test_initial_load_contains_location_from_event_cache() {
669+
let (client, server) = logged_in_client_with_server().await;
670+
671+
// Enable event cache BEFORE syncing so events get cached.
672+
client.event_cache().subscribe().unwrap();
673+
674+
let now = MilliSecondsSinceUnixEpoch::now();
675+
let f = EventFactory::new();
676+
let mut sync_builder = SyncResponseBuilder::new();
677+
678+
// Alice has a live beacon_info.
679+
let beacon_info_event = f
680+
.beacon_info(
681+
Some("Alice location".to_owned()),
682+
Duration::from_millis(300_000),
683+
true,
684+
Some(now),
685+
)
686+
.event_id(event_id!("$alice_beacon_info"))
687+
.sender(user_id!("@alice:localhost"))
688+
.state_key(user_id!("@alice:localhost"))
689+
.into_raw_sync_state();
690+
691+
// A beacon event in the timeline.
692+
let beacon_event = f
693+
.beacon(owned_event_id!("$alice_beacon_info"), 48.8566, 2.3522, 10, Some(now))
694+
.event_id(event_id!("$alice_location"))
695+
.sender(user_id!("@alice:localhost"))
696+
.server_ts(now)
697+
.into_raw_sync();
698+
699+
sync_builder.add_joined_room(
700+
JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID)
701+
.add_state_event(beacon_info_event)
702+
.add_timeline_event(beacon_event),
703+
);
704+
705+
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
706+
707+
let sync_settings =
708+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
709+
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
710+
server.reset().await;
711+
712+
let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();
713+
let live_location_shares = room.subscribe_to_live_location_shares().await;
714+
let (initial, _stream) = live_location_shares.subscribe();
715+
716+
// Initial snapshot should contain both beacon_info AND last_location.
717+
assert_eq!(initial.len(), 1);
718+
let share = &initial[0];
719+
assert_eq!(share.user_id, user_id!("@alice:localhost"));
720+
assert_eq!(share.beacon_info.description, Some("Alice location".to_owned()));
721+
722+
let last_location =
723+
share.last_location.as_ref().expect("Expected last_location in initial load");
724+
assert_eq!(last_location.location.uri, "geo:48.8566,2.3522;u=10");
725+
assert_eq!(last_location.ts, now);
726+
}

0 commit comments

Comments
 (0)