Skip to content

Commit becbb63

Browse files
committed
feat(ffi): Subscribe to a room's RoomInfo through Client
This helps in the case we want to observe the membership state changes - or some other info - for a room that's still not known so we can't just use `Client::get_room` to fetch it.
1 parent 34d3cd4 commit becbb63

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Breaking changes:
2121

2222
Additions:
2323

24+
- `Client::subscribe_to_room_info` allows clients to subscribe to room info updates in rooms which may not be known yet.
25+
This is useful when displaying a room preview for an unknown room, so when we receive any membership change for it,
26+
we can automatically update the UI.
2427
- `Client::get_max_media_upload_size` to get the max size of a request sent to the homeserver so we can tweak our media
2528
uploads by compressing/transcoding the media.
2629
- Add `ClientBuilder::enable_share_history_on_invite` to enable experimental support for sharing encrypted room history on invite, per [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268).

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use matrix_sdk::{
3737
},
3838
sliding_sync::Version as SdkSlidingSyncVersion,
3939
store::RoomLoadSettings as SdkRoomLoadSettings,
40+
sync::RoomUpdate,
4041
AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
4142
STATE_STORE_DATABASE_NAME,
4243
};
@@ -93,8 +94,9 @@ use crate::{
9394
encryption::Encryption,
9495
notification::NotificationClient,
9596
notification_settings::NotificationSettings,
96-
room::RoomHistoryVisibility,
97+
room::{RoomHistoryVisibility, RoomInfoListener},
9798
room_directory_search::RoomDirectorySearch,
99+
room_info::RoomInfo,
98100
room_preview::RoomPreview,
99101
ruma::{
100102
AccountDataEvent, AccountDataEventType, AuthData, InviteAvatars, MediaPreviewConfig,
@@ -1561,6 +1563,47 @@ impl Client {
15611563
let max_upload_size = self.inner.load_or_fetch_max_upload_size().await?;
15621564
Ok(max_upload_size.into())
15631565
}
1566+
1567+
/// Subscribe to [`RoomInfo`] updates given a provided [`RoomId`].
1568+
///
1569+
/// This works even for rooms we haven't received yet, so we can subscribe
1570+
/// to this and wait until we receive updates from them when sync responses
1571+
/// are processed.
1572+
///
1573+
/// Note this method should be used sparingly since using callback
1574+
/// interfaces is expensive, as well as keeping them alive for a long
1575+
/// time. Usages of this method should be short-lived and dropped as
1576+
/// soon as possible.
1577+
pub async fn subscribe_to_room_info(
1578+
&self,
1579+
room_id: String,
1580+
listener: Box<dyn RoomInfoListener>,
1581+
) -> Result<Arc<TaskHandle>, ClientError> {
1582+
let room_id = RoomId::parse(room_id)?;
1583+
let receiver = self.inner.subscribe_to_room_updates(&room_id);
1584+
1585+
// Emit the initial event, if present
1586+
if let Some(room) = self.inner.get_room(&room_id) {
1587+
if let Ok(room_info) = RoomInfo::new(&room).await {
1588+
listener.call(room_info);
1589+
}
1590+
}
1591+
1592+
Ok(Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
1593+
pin_mut!(receiver);
1594+
while let Ok(room_update) = receiver.recv().await {
1595+
let room = match room_update {
1596+
RoomUpdate::Invited { room, .. } => room,
1597+
RoomUpdate::Joined { room, .. } => room,
1598+
RoomUpdate::Knocked { room, .. } => room,
1599+
RoomUpdate::Left { room, .. } => room,
1600+
};
1601+
if let Ok(room_info) = RoomInfo::new(&room).await {
1602+
listener.call(room_info);
1603+
}
1604+
}
1605+
}))))
1606+
}
15641607
}
15651608

15661609
#[matrix_sdk_ffi_macros::export(callback_interface)]

bindings/matrix-sdk-ffi/src/ruma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ pub use galleries::*;
17641764
mod galleries {
17651765
use ruma::{
17661766
events::room::message::{
1767-
FormattedBody as RumaFormattedBody, GalleryItemType as RumaGalleryItemType,
1767+
GalleryItemType as RumaGalleryItemType,
17681768
GalleryMessageEventContent as RumaGalleryMessageEventContent,
17691769
},
17701770
serde::JsonObject,

0 commit comments

Comments
 (0)