Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Room::subscribe_to_live_location_shares to Room::observable_live_location_share #1

Open
wants to merge 2 commits into
base: feat/subscribe-beacons
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/matrix-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub use sliding_sync::{
#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();

pub mod live_location_share;
#[cfg(any(test, feature = "testing"))]
pub mod test_utils;

Expand Down
82 changes: 82 additions & 0 deletions crates/matrix-sdk/src/live_location_share.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Types for live location sharing.

use async_stream::stream;
use ruma::{
events::{
beacon::OriginalSyncBeaconEvent, beacon_info::BeaconInfoEventContent,
location::LocationContent,
},
MilliSecondsSinceUnixEpoch, OwnedUserId, RoomId,
};

use crate::{event_handler::ObservableEventHandler, Client, Room};
use futures_util::Stream;

/// An observable live location.
#[derive(Debug)]
pub struct ObservableLiveLocation {
observable_room_events: ObservableEventHandler<(OriginalSyncBeaconEvent, Room)>,
}

impl ObservableLiveLocation {
/// Create a new `ObservableLiveLocation` for a particular room.
pub(crate) fn new(client: &Client, room_id: &RoomId) -> Self {
Self { observable_room_events: client.observe_room_events(room_id) }
}

/// Get a stream of [`LiveLocationShare`].
pub fn subscribe(&self) -> impl Stream<Item = LiveLocationShare> {
let stream = self.observable_room_events.subscribe();

stream! {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot use StreamExt::map because room.get_user_beacon_info is used inside this map, and is an async method. Instead, we use the stream! macro, which does the job too.

for await (event, room) in stream {
yield LiveLocationShare {
last_location: LastLocation {
location: event.content.location,
ts: event.content.ts,
},
beacon_info: room
.get_user_beacon_info(&event.sender)
.await
.ok()
.map(|info| info.content),
user_id: event.sender,
};
}
}
}
}

/// Details of the last known location beacon.
#[derive(Clone, Debug)]
pub struct LastLocation {
/// The most recent location content of the user.
pub location: LocationContent,
/// The timestamp of when the location was updated.
pub ts: MilliSecondsSinceUnixEpoch,
}

/// Details of a users live location share.
#[derive(Clone, Debug)]
pub struct LiveLocationShare {
/// The user's last known location.
pub last_location: LastLocation,
/// Information about the associated beacon event.
pub beacon_info: Option<BeaconInfoEventContent>,
/// The user ID of the person sharing their live location.
pub user_id: OwnedUserId,
}
17 changes: 12 additions & 5 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ use crate::{
error::{BeaconError, WrongRoomState},
event_cache::{self, EventCacheDropHandles, RoomEventCache},
event_handler::{EventHandler, EventHandlerDropGuard, EventHandlerHandle, SyncEvent},
live_location_share::ObservableLiveLocation,
media::{MediaFormat, MediaRequestParameters},
notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode},
room::power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt},
Expand Down Expand Up @@ -2990,17 +2991,18 @@ impl Room {
Ok(())
}

/// Get the beacon information event in the room for the current user.
/// Get the beacon information event in the room for the `user_id`.
///
/// # Errors
///
/// Returns an error if the event is redacted, stripped, not found or could
/// not be deserialized.
async fn get_user_beacon_info(
pub(crate) async fn get_user_beacon_info(
&self,
user_id: &UserId,
) -> Result<OriginalSyncStateEvent<BeaconInfoEventContent>, BeaconError> {
let raw_event = self
.get_state_event_static_for_key::<BeaconInfoEventContent, _>(self.own_user_id())
.get_state_event_static_for_key::<BeaconInfoEventContent, _>(user_id)
.await?
.ok_or(BeaconError::NotFound)?;

Expand Down Expand Up @@ -3053,7 +3055,7 @@ impl Room {
) -> Result<send_state_event::v3::Response, BeaconError> {
self.ensure_room_joined()?;

let mut beacon_info_event = self.get_user_beacon_info().await?;
let mut beacon_info_event = self.get_user_beacon_info(self.own_user_id()).await?;
beacon_info_event.content.stop();
Ok(self.send_state_event_for_key(self.own_user_id(), beacon_info_event.content).await?)
}
Expand All @@ -3075,7 +3077,7 @@ impl Room {
) -> Result<send_message_event::v3::Response, BeaconError> {
self.ensure_room_joined()?;

let beacon_info_event = self.get_user_beacon_info().await?;
let beacon_info_event = self.get_user_beacon_info(self.own_user_id()).await?;

if beacon_info_event.content.is_live() {
let content = BeaconEventContent::new(beacon_info_event.event_id, geo_uri, None);
Expand All @@ -3085,6 +3087,11 @@ impl Room {
}
}

/// Observe live location shares.
pub fn observe_live_location_share(&self) -> ObservableLiveLocation {
ObservableLiveLocation::new(&self.client, self.room_id())
}

/// Send a call notification event in the current room.
///
/// This is only supposed to be used in **custom** situations where the user
Expand Down
Loading