Skip to content
Merged
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
34 changes: 34 additions & 0 deletions bindings/matrix-sdk-ffi/src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ impl SpaceService {
Ok(Arc::new(SpaceRoomList::new(self.inner.space_room_list(space_id).await)))
}

/// Returns all known direct-parents of a given space room ID.
pub async fn joined_parents_of_child(
&self,
child_id: String,
) -> Result<Vec<SpaceRoom>, ClientError> {
let child_id = RoomId::parse(child_id)?;

let parents = self.inner.joined_parents_of_child(&child_id).await;

Ok(parents.into_iter().map(Into::into).collect())
}

pub async fn add_child_to_space(
&self,
child_id: String,
space_id: String,
) -> Result<(), ClientError> {
let space_id = RoomId::parse(space_id)?;
let child_id = RoomId::parse(child_id)?;

self.inner.add_child_to_space(child_id, space_id).await.map_err(ClientError::from)
}

pub async fn remove_child_from_space(
&self,
child_id: String,
space_id: String,
) -> Result<(), ClientError> {
let space_id = RoomId::parse(space_id)?;
let child_id = RoomId::parse(child_id)?;

self.inner.remove_child_from_space(child_id, space_id).await.map_err(ClientError::from)
}

/// Start a space leave process returning a [`LeaveSpaceHandle`] from which
/// rooms can be retrieved in reversed BFS order starting from the requested
/// `space_id` graph node. If the room is unknown then an error will be
Expand Down
12 changes: 10 additions & 2 deletions crates/matrix-sdk-ui/src/spaces/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl SpaceGraph {
.map_or(vec![], |node| node.children.iter().map(|id| id.as_ref()).collect())
}

/// Returns the parents of a given node. If the node does not exist, it
/// returns an empty vector.
pub(super) fn parents_of(&self, node_id: &RoomId) -> Vec<&RoomId> {
self.nodes
.get(node_id)
.map_or(vec![], |node| node.parents.iter().map(|id| id.as_ref()).collect())
}

/// Adds a node to the graph. If the node already exists, it does nothing.
pub(super) fn add_node(&mut self, node_id: OwnedRoomId) {
self.nodes.entry(node_id.clone()).or_insert(SpaceGraphNode::new(node_id));
Expand Down Expand Up @@ -184,8 +192,8 @@ mod tests {

assert_eq!(graph.root_nodes(), vec![&a]);

assert!(graph.nodes[&b].parents.contains(&a));
assert!(graph.nodes[&c].parents.contains(&a));
assert_eq!(graph.parents_of(&b), vec![&a]);
assert_eq!(graph.parents_of(&c), vec![&a]);

assert_eq!(graph.children_of(&a), vec![&b, &c]);
}
Expand Down
Loading
Loading