Skip to content

Commit

Permalink
feat(ffi): Implement RoomList::invites
Browse files Browse the repository at this point in the history
feat(ffi): Implement `RoomList::invites`
  • Loading branch information
Hywan authored Jun 16, 2023
2 parents 305d55d + bae6fc7 commit 85b6eaa
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 16 deletions.
18 changes: 18 additions & 0 deletions bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ impl RoomList {
})
}

async fn invites(
&self,
listener: Box<dyn RoomListEntriesListener>,
) -> Result<RoomListEntriesResult, RoomListError> {
let (entries, entries_stream) = self.inner.invites().await.map_err(RoomListError::from)?;

Ok(RoomListEntriesResult {
entries: entries.into_iter().map(Into::into).collect(),
entries_stream: Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
pin_mut!(entries_stream);

while let Some(diff) = entries_stream.next().await {
listener.on_update(diff.into());
}
}))),
})
}

async fn apply_input(&self, input: RoomListInput) -> Result<(), RoomListError> {
self.inner.apply_input(input.into()).await.map_err(Into::into)
}
Expand Down
13 changes: 13 additions & 0 deletions crates/matrix-sdk-ui/src/room_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ impl RoomList {
.ok_or_else(|| Error::UnknownList(ALL_ROOMS_LIST_NAME.to_owned()))
}

/// Get all previous invites, in addition to a [`Stream`] to invites.
///
/// Invites are taking the form of `RoomListEntry`, it's like a “sub” room
/// list.
pub async fn invites(
&self,
) -> Result<(Vector<RoomListEntry>, impl Stream<Item = VectorDiff<RoomListEntry>>), Error> {
self.sliding_sync
.on_list(INVITES_LIST_NAME, |list| ready(list.room_list_stream()))
.await
.ok_or_else(|| Error::UnknownList(INVITES_LIST_NAME.to_owned()))
}

/// Pass an [`Input`] onto the state machine.
pub async fn apply_input(&self, input: Input) -> Result<(), Error> {
use Input::*;
Expand Down
58 changes: 57 additions & 1 deletion crates/matrix-sdk-ui/src/room_list/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::Error;

pub const ALL_ROOMS_LIST_NAME: &str = "all_rooms";
pub const VISIBLE_ROOMS_LIST_NAME: &str = "visible_rooms";
pub const INVITES_LIST_NAME: &str = "invites";

/// The state of the [`super::RoomList`]' state machine.
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -130,6 +131,36 @@ impl Action for SetAllRoomsListToGrowingSyncMode {
}
}

struct AddInvitesList;

#[async_trait]
impl Action for AddInvitesList {
async fn run(&self, sliding_sync: &SlidingSync) -> Result<(), Error> {
sliding_sync
.add_list(
SlidingSyncList::builder(INVITES_LIST_NAME)
.sync_mode(SlidingSyncMode::new_growing(100))
.timeline_limit(0)
.required_state(vec![
(StateEventType::RoomAvatar, "".to_owned()),
(StateEventType::RoomEncryption, "".to_owned()),
(StateEventType::RoomMember, "$ME".to_owned()),
(StateEventType::RoomCanonicalAlias, "".to_owned()),
])
.filters(Some(assign!(SyncRequestListFilters::default(), {
is_invite: Some(true),
is_tombstoned: Some(false),
not_room_types: vec!["m.space".to_owned()],

}))),
)
.await
.map_err(Error::SlidingSync)?;

Ok(())
}
}

/// Type alias to represent one action.
type OneAction = Box<dyn Action + Send + Sync>;

Expand Down Expand Up @@ -169,7 +200,7 @@ macro_rules! actions {
impl Actions {
actions! {
none => [],
first_rooms_are_loaded => [SetAllRoomsListToGrowingSyncMode, AddVisibleRoomsList],
first_rooms_are_loaded => [SetAllRoomsListToGrowingSyncMode, AddVisibleRoomsList, AddInvitesList],
refresh_lists => [SetAllRoomsListToGrowingSyncMode],
}

Expand Down Expand Up @@ -303,4 +334,29 @@ mod tests {

Ok(())
}

#[async_test]
async fn test_action_add_invitess_list() -> Result<(), Error> {
let room_list = new_room_list().await?;
let sliding_sync = room_list.sliding_sync();

// List is absent.
assert_eq!(sliding_sync.on_list(INVITES_LIST_NAME, |_list| ready(())).await, None);

// Run the action!
AddInvitesList.run(sliding_sync).await?;

// List is present!
assert_eq!(
sliding_sync
.on_list(INVITES_LIST_NAME, |list| ready(matches!(
list.sync_mode(),
SlidingSyncMode::Growing { batch_size, .. } if batch_size == 100
)))
.await,
Some(true)
);

Ok(())
}
}
Loading

0 comments on commit 85b6eaa

Please sign in to comment.