Skip to content

Commit

Permalink
refactor: make use of EventHandlerDropGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
torrybr committed Nov 14, 2024
1 parent eb1a0ce commit c2e3cf4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
19 changes: 0 additions & 19 deletions crates/matrix-sdk/src/live_location_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ use ruma::{
events::{beacon_info::BeaconInfoEventContent, location::LocationContent},
MilliSecondsSinceUnixEpoch, OwnedUserId,
};
use tokio::sync::broadcast;

use crate::event_handler::EventHandlerHandle;

/// Details of the last known location beacon.
#[derive(Clone, Debug)]
Expand All @@ -41,19 +38,3 @@ pub struct LiveLocationShare {
/// The user ID of the person sharing their live location.
pub user_id: OwnedUserId,
}

/// A subscription to live location sharing events.
///
/// This struct holds the `EventHandlerHandle` and the
/// `Receiver<LiveLocationShare>` for live location shares.
#[derive(Debug)]
pub struct LiveLocationSubscription {
/// Manages the event handler lifecycle.
pub event_handler_handle: EventHandlerHandle,
/// Receives live location shares.
pub receiver: broadcast::Receiver<LiveLocationShare>,
}

impl Drop for LiveLocationSubscription {
fn drop(&mut self) {}
}
9 changes: 6 additions & 3 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ use crate::{
error::{BeaconError, WrongRoomState},
event_cache::{self, EventCacheDropHandles, RoomEventCache},
event_handler::{EventHandler, EventHandlerDropGuard, EventHandlerHandle, SyncEvent},
live_location_share::{LastLocation, LiveLocationShare, LiveLocationSubscription},
live_location_share::{LastLocation, LiveLocationShare},
media::{MediaFormat, MediaRequestParameters},
notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode},
room::power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt},
Expand Down Expand Up @@ -3147,7 +3147,9 @@ impl Room {
///
/// The returned receiver will receive a new event for each sync response
/// that contains a `m.beacon` event.
pub fn subscribe_to_live_location_shares(&self) -> LiveLocationSubscription {
pub fn subscribe_to_live_location_shares(
&self,
) -> (EventHandlerDropGuard, broadcast::Receiver<LiveLocationShare>) {
let (sender, receiver) = broadcast::channel(128);

let room_id = self.room_id().to_owned();
Expand Down Expand Up @@ -3178,7 +3180,8 @@ impl Room {
}
});

LiveLocationSubscription { event_handler_handle: beacon_event_handler_handle, receiver }
let drop_guard = self.client().event_handler_drop_guard(beacon_event_handler_handle);
(drop_guard, receiver)
}

/// Load pinned state events for a room from the `/state` endpoint in the
Expand Down
12 changes: 5 additions & 7 deletions crates/matrix-sdk/tests/integration/room/beacon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async fn test_subscribe_to_live_location_shares() {

let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();

let mut subscription = room.subscribe_to_live_location_shares();
let (_drop_guard, mut receiver) = room.subscribe_to_live_location_shares();

let mut timeline_events = Vec::new();

Expand Down Expand Up @@ -250,7 +250,7 @@ async fn test_subscribe_to_live_location_shares() {

for i in 0..timeline_events.len() {
let live_location_share =
subscription.receiver.recv().await.expect("Failed to receive live location share");
receiver.recv().await.expect("Failed to receive live location share");

assert_eq!(live_location_share.user_id.to_string(), "@example:localhost");

Expand Down Expand Up @@ -348,7 +348,7 @@ async fn test_subscribe_to_live_location_shares_with_multiple_users() {

let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap();

let mut subscription = room.subscribe_to_live_location_shares();
let (_drop_guard, mut receiver) = room.subscribe_to_live_location_shares();

sync_builder.add_joined_room(JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID).add_timeline_bulk(
[
Expand Down Expand Up @@ -397,8 +397,7 @@ async fn test_subscribe_to_live_location_shares_with_multiple_users() {
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let live_location_share =
subscription.receiver.recv().await.expect("Failed to receive live location share");
let live_location_share = receiver.recv().await.expect("Failed to receive live location share");

assert_eq!(live_location_share.user_id.to_string(), "@user1:localhost");

Expand All @@ -420,8 +419,7 @@ async fn test_subscribe_to_live_location_shares_with_multiple_users() {
assert_eq!(live_location_share.beacon_info.ts, current_time);
assert_eq!(live_location_share.beacon_info.asset.type_, AssetType::Self_);

let live_location_share =
subscription.receiver.recv().await.expect("Failed to receive live location share");
let live_location_share = receiver.recv().await.expect("Failed to receive live location share");

assert_eq!(live_location_share.user_id.to_string(), "@user2:localhost");

Expand Down

0 comments on commit c2e3cf4

Please sign in to comment.