-
Notifications
You must be signed in to change notification settings - Fork 332
feat(sdk): Spaces #5509
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
Merged
Merged
feat(sdk): Spaces #5509
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
99e7848
chore(spaces): setup a simple space service and some unit tests
stefanceriu b4606fe
chore(spaces): introduce a `SpaceServiceRoom`
stefanceriu 3cb5874
feat(spaces): introduce a `SpaceRoomList` that allows pagination and …
stefanceriu c667833
feat(spaces): add a reactive version of the `joined_spaces` method
stefanceriu 2459061
change(spaces): return only top level joined rooms from `SpaceService…
stefanceriu 3de2d8d
feat(spaces): have the `SpaceRoomList` publish updates as known room …
stefanceriu bafcd7d
fix(spaces): fix complement-crypto failures because of using an outda…
stefanceriu 4cd3e1e
fix(spaces): use wasm compatible executor `spawn` and `JoinHandle`
stefanceriu dd728d9
change(room_list): request both `m.space.parent` and `m.space.child` …
stefanceriu 8fddcee
chore(spaces): build a graph from joined spaces parent and child rela…
stefanceriu 13aad5c
feat(ffi): expose `SpaceService::subscribe_to_joined_spaces` and `Spa…
stefanceriu d6d1799
fix(spaces): return the TaskHandle from the FFI space service subscri…
stefanceriu 9246f0b
feat(spaces): expose the number of children each space room has
stefanceriu c71065c
change(spaces): put a limit of 1 on the max depth of the /hierarchy c…
stefanceriu 87558cd
fix(spaces): filter out the current parent space from the `/hierarchy…
stefanceriu 136e068
change(spaces): make the `SpaceService` constructor non-async and ins…
stefanceriu 0993802
chore(spaces): converge on single naming scheme for all spaces relate…
stefanceriu fb106b7
chore(spaces): add changelog
stefanceriu 6ecc270
change(spaces): publish VectorDiffs instead of a full vectors for joi…
stefanceriu 9e2e46c
chore(spaces): ignore room list change updates if empty
stefanceriu c00b826
docs(spaces): add documentation, comments and examples
stefanceriu b0d7831
chore(spaces): switch from manually dropping `JoinHandle`s to `AbortO…
stefanceriu b6b4611
fix(spaces): compute the initial `joined_spaces` value when first set…
stefanceriu a6be4ae
chore(spaces): use `Client::joined_space_rooms` within the space serv…
stefanceriu 4682668
chore(spaces): various documentation fixes
stefanceriu 90f323d
chore(spaces): simplify the `SpaceGraph` interfaces
stefanceriu 22e23a2
chore(spaces): switch some `flat_map`s to `filter_map`
stefanceriu af195ea
chore(spaces): fix typo
stefanceriu aa27a64
chore(spaces): improve how space graph edge additions work
stefanceriu 7a11db4
chore(spaces): simplify the `SpaceGraph` interfaces
stefanceriu 3c45e4b
chore(spaces): cleanup clone names in room updates listener
stefanceriu 6515ace
fix(spaces): mitigate eventual race conditions when updating the pagi…
stefanceriu 243e51a
chore(spaces): acquire and retain an async lock on the pagination tok…
stefanceriu 74fd754
fix(spaces): address potential race when setting up the room updates …
stefanceriu 89f2483
fix(spaces): mitigate eventual race conditions when updating the pagi…
stefanceriu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
// Copyright 2025 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. | ||
|
||
use std::{fmt::Debug, sync::Arc}; | ||
|
||
use eyeball_im::VectorDiff; | ||
use futures_util::{pin_mut, StreamExt}; | ||
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm}; | ||
use matrix_sdk_ui::spaces::{ | ||
room_list::SpaceRoomListPaginationState, SpaceRoom as UISpaceRoom, | ||
SpaceRoomList as UISpaceRoomList, SpaceService as UISpaceService, | ||
}; | ||
use ruma::RoomId; | ||
|
||
use crate::{ | ||
client::JoinRule, | ||
error::ClientError, | ||
room::{Membership, RoomHero}, | ||
room_preview::RoomType, | ||
runtime::get_runtime_handle, | ||
TaskHandle, | ||
}; | ||
|
||
/// The main entry point into the Spaces facilities. | ||
/// | ||
/// The spaces service is responsible for retrieving one's joined rooms, | ||
/// building a graph out of their `m.space.parent` and `m.space.child` state | ||
/// events, and providing access to the top-level spaces and their children. | ||
#[derive(uniffi::Object)] | ||
pub struct SpaceService { | ||
inner: UISpaceService, | ||
} | ||
|
||
impl SpaceService { | ||
/// Creates a new `SpaceService` instance. | ||
pub(crate) fn new(inner: UISpaceService) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
#[matrix_sdk_ffi_macros::export] | ||
impl SpaceService { | ||
/// Returns a list of all the top-level joined spaces. It will eagerly | ||
/// compute the latest version and also notify subscribers if there were | ||
/// any changes. | ||
pub async fn joined_spaces(&self) -> Vec<SpaceRoom> { | ||
self.inner.joined_spaces().await.into_iter().map(Into::into).collect() | ||
} | ||
|
||
/// Subscribes to updates on the joined spaces list. If space rooms are | ||
/// joined or left, the stream will yield diffs that reflect the changes. | ||
pub async fn subscribe_to_joined_spaces( | ||
stefanceriu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&self, | ||
listener: Box<dyn SpaceServiceJoinedSpacesListener>, | ||
) -> Arc<TaskHandle> { | ||
let (initial_values, mut stream) = self.inner.subscribe_to_joined_spaces().await; | ||
|
||
listener.on_update(vec![SpaceListUpdate::Reset { | ||
values: initial_values.into_iter().map(Into::into).collect(), | ||
}]); | ||
|
||
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move { | ||
while let Some(diffs) = stream.next().await { | ||
listener.on_update(diffs.into_iter().map(Into::into).collect()); | ||
} | ||
}))) | ||
} | ||
|
||
/// Returns a `SpaceRoomList` for the given space ID. | ||
#[allow(clippy::unused_async)] | ||
stefanceriu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This method doesn't need to be async but if its not the FFI layer panics | ||
// with "there is no no reactor running, must be called from the context | ||
// of a Tokio 1.x runtime" error because the underlying constructor spawns | ||
// an async task. | ||
pub async fn space_room_list( | ||
stefanceriu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&self, | ||
space_id: String, | ||
) -> Result<Arc<SpaceRoomList>, ClientError> { | ||
let space_id = RoomId::parse(space_id)?; | ||
Ok(Arc::new(SpaceRoomList::new(self.inner.space_room_list(space_id)))) | ||
} | ||
} | ||
|
||
/// The `SpaceRoomList`represents a paginated list of direct rooms | ||
/// that belong to a particular space. | ||
/// | ||
/// It can be used to paginate through the list (and have live updates on the | ||
/// pagination state) as well as subscribe to changes as rooms are joined or | ||
/// left. | ||
/// | ||
/// The `SpaceRoomList` also automatically subscribes to client room changes | ||
/// and updates the list accordingly as rooms are joined or left. | ||
#[derive(uniffi::Object)] | ||
pub struct SpaceRoomList { | ||
inner: UISpaceRoomList, | ||
} | ||
stefanceriu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl SpaceRoomList { | ||
/// Creates a new `SpaceRoomList` for the underlying UI crate room list. | ||
fn new(inner: UISpaceRoomList) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
#[matrix_sdk_ffi_macros::export] | ||
impl SpaceRoomList { | ||
/// Returns if the room list is currently paginating or not. | ||
pub fn pagination_state(&self) -> SpaceRoomListPaginationState { | ||
self.inner.pagination_state() | ||
} | ||
|
||
/// Subscribe to pagination updates. | ||
pub fn subscribe_to_pagination_state_updates( | ||
&self, | ||
listener: Box<dyn SpaceRoomListPaginationStateListener>, | ||
) -> Arc<TaskHandle> { | ||
let pagination_state = self.inner.subscribe_to_pagination_state_updates(); | ||
|
||
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move { | ||
pin_mut!(pagination_state); | ||
|
||
while let Some(state) = pagination_state.next().await { | ||
listener.on_update(state); | ||
} | ||
}))) | ||
} | ||
|
||
/// Return the current list of rooms. | ||
pub fn rooms(&self) -> Vec<SpaceRoom> { | ||
self.inner.rooms().into_iter().map(Into::into).collect() | ||
} | ||
|
||
/// Subscribes to room list updates. | ||
pub fn subscribe_to_room_update( | ||
&self, | ||
listener: Box<dyn SpaceRoomListEntriesListener>, | ||
) -> Arc<TaskHandle> { | ||
let (initial_values, mut stream) = self.inner.subscribe_to_room_updates(); | ||
|
||
listener.on_update(vec![SpaceListUpdate::Reset { | ||
values: initial_values.into_iter().map(Into::into).collect(), | ||
}]); | ||
|
||
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move { | ||
while let Some(diffs) = stream.next().await { | ||
listener.on_update(diffs.into_iter().map(Into::into).collect()); | ||
} | ||
}))) | ||
} | ||
|
||
/// Ask the list to retrieve the next page if the end hasn't been reached | ||
/// yet. Otherwise it no-ops. | ||
pub async fn paginate(&self) -> Result<(), ClientError> { | ||
self.inner.paginate().await.map_err(ClientError::from) | ||
} | ||
} | ||
|
||
#[matrix_sdk_ffi_macros::export(callback_interface)] | ||
pub trait SpaceRoomListPaginationStateListener: SendOutsideWasm + SyncOutsideWasm + Debug { | ||
fn on_update(&self, pagination_state: SpaceRoomListPaginationState); | ||
} | ||
|
||
#[matrix_sdk_ffi_macros::export(callback_interface)] | ||
pub trait SpaceRoomListEntriesListener: SendOutsideWasm + SyncOutsideWasm + Debug { | ||
fn on_update(&self, rooms: Vec<SpaceListUpdate>); | ||
} | ||
|
||
#[matrix_sdk_ffi_macros::export(callback_interface)] | ||
pub trait SpaceServiceJoinedSpacesListener: SendOutsideWasm + SyncOutsideWasm + Debug { | ||
fn on_update(&self, room_updates: Vec<SpaceListUpdate>); | ||
} | ||
|
||
/// Structure representing a room in a space and aggregated information | ||
/// relevant to the UI layer. | ||
#[derive(uniffi::Record)] | ||
pub struct SpaceRoom { | ||
/// The ID of the room. | ||
pub room_id: String, | ||
/// The canonical alias of the room, if any. | ||
pub canonical_alias: Option<String>, | ||
/// The name of the room, if any. | ||
pub name: Option<String>, | ||
/// The topic of the room, if any. | ||
pub topic: Option<String>, | ||
/// The URL for the room's avatar, if one is set. | ||
pub avatar_url: Option<String>, | ||
/// The type of room from `m.room.create`, if any. | ||
pub room_type: RoomType, | ||
/// The number of members joined to the room. | ||
pub num_joined_members: u64, | ||
/// The join rule of the room. | ||
pub join_rule: Option<JoinRule>, | ||
/// Whether the room may be viewed by users without joining. | ||
pub world_readable: Option<bool>, | ||
/// Whether guest users may join the room and participate in it. | ||
pub guest_can_join: bool, | ||
|
||
/// The number of children room this has, if a space. | ||
pub children_count: u64, | ||
/// Whether this room is joined, left etc. | ||
pub state: Option<Membership>, | ||
/// A list of room members considered to be heroes. | ||
pub heroes: Option<Vec<RoomHero>>, | ||
} | ||
|
||
impl From<UISpaceRoom> for SpaceRoom { | ||
fn from(room: UISpaceRoom) -> Self { | ||
Self { | ||
room_id: room.room_id.into(), | ||
canonical_alias: room.canonical_alias.map(|alias| alias.into()), | ||
name: room.name, | ||
topic: room.topic, | ||
avatar_url: room.avatar_url.map(|url| url.into()), | ||
room_type: room.room_type.into(), | ||
num_joined_members: room.num_joined_members, | ||
join_rule: room.join_rule.map(Into::into), | ||
world_readable: room.world_readable, | ||
guest_can_join: room.guest_can_join, | ||
children_count: room.children_count, | ||
state: room.state.map(Into::into), | ||
heroes: room.heroes.map(|heroes| heroes.into_iter().map(Into::into).collect()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(uniffi::Enum)] | ||
pub enum SpaceListUpdate { | ||
Append { values: Vec<SpaceRoom> }, | ||
Clear, | ||
PushFront { value: SpaceRoom }, | ||
PushBack { value: SpaceRoom }, | ||
PopFront, | ||
PopBack, | ||
Insert { index: u32, value: SpaceRoom }, | ||
Set { index: u32, value: SpaceRoom }, | ||
Remove { index: u32 }, | ||
Truncate { length: u32 }, | ||
Reset { values: Vec<SpaceRoom> }, | ||
} | ||
|
||
impl From<VectorDiff<UISpaceRoom>> for SpaceListUpdate { | ||
fn from(diff: VectorDiff<UISpaceRoom>) -> Self { | ||
match diff { | ||
VectorDiff::Append { values } => { | ||
Self::Append { values: values.into_iter().map(|v| v.into()).collect() } | ||
} | ||
VectorDiff::Clear => Self::Clear, | ||
VectorDiff::PushFront { value } => Self::PushFront { value: value.into() }, | ||
VectorDiff::PushBack { value } => Self::PushBack { value: value.into() }, | ||
VectorDiff::PopFront => Self::PopFront, | ||
VectorDiff::PopBack => Self::PopBack, | ||
VectorDiff::Insert { index, value } => { | ||
Self::Insert { index: index as u32, value: value.into() } | ||
} | ||
VectorDiff::Set { index, value } => { | ||
Self::Set { index: index as u32, value: value.into() } | ||
} | ||
VectorDiff::Remove { index } => Self::Remove { index: index as u32 }, | ||
VectorDiff::Truncate { length } => Self::Truncate { length: length as u32 }, | ||
VectorDiff::Reset { values } => { | ||
Self::Reset { values: values.into_iter().map(|v| v.into()).collect() } | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.