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

ffi: expose room membership through the RoomListItem and allow invited rooms to be build differently than "full" ones #3869

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::sync::RwLock;

use crate::{
error::ClientError,
room::Room,
room::{Membership, Room},
room_info::RoomInfo,
timeline::{EventTimelineItem, Timeline},
timeline_event_filter::TimelineEventTypeFilter,
Expand All @@ -50,6 +50,8 @@ pub enum RoomListError {
InitializingTimeline { error: String },
#[error("Event cache ran into an error: {error}")]
EventCache { error: String },
#[error("The requested room doesn't match the membership requirements")]
IncorrectRoomMembership,
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
}

impl From<matrix_sdk_ui::room_list_service::Error> for RoomListError {
Expand Down Expand Up @@ -603,10 +605,35 @@ impl RoomListItem {
Ok(RoomInfo::new(self.inner.inner_room()).await?)
}

/// The room's current membership state
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
fn membership(&self) -> Membership {
self.inner.inner_room().state().into()
}

/// Builds a `Room` FFI from an invited room without initializing its
/// internal timeline
///
/// An error will be returned if the room is a state different than invited
///
/// ⚠️ Holding on to this room instance after it has been joined is not
/// safe. Use `full_room` instead
fn invited_room(&self) -> Result<Arc<Room>, RoomListError> {
if !matches!(self.membership(), Membership::Invited) {
return Err(RoomListError::IncorrectRoomMembership);
}

Ok(Arc::new(Room::new(self.inner.inner_room().clone())))
}

/// Build a full `Room` FFI object, filling its associated timeline.
///
/// If its internal timeline hasn't been initialized, it'll fail.
/// An error will be returned if the room is a state different than joined
/// or if its internal timeline hasn't been initialized.
fn full_room(&self) -> Result<Arc<Room>, RoomListError> {
if !matches!(self.membership(), Membership::Joined) {
return Err(RoomListError::IncorrectRoomMembership);
}

if let Some(timeline) = self.inner.timeline() {
Ok(Arc::new(Room::with_timeline(
self.inner.inner_room().clone(),
Expand Down
Loading