Skip to content

Commit

Permalink
feate: implemented space child creation
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Mar 29, 2024
1 parent 17390a2 commit 6237893
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod util;
pub mod account;
pub mod profile;
pub mod spaces;
pub mod rooms;

use std::sync::RwLock;

Expand Down
1 change: 1 addition & 0 deletions crates/core/src/rooms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod create;
52 changes: 52 additions & 0 deletions crates/core/src/rooms/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use matrix::{
client::create_room::{Request, Response, RoomCreationContent, RoomVisibility},
ruma_common::{room::RoomType, OwnedRoomOrAliasId, RoomVersionId},
ruma_events::{
room::power_levels::RoomPowerLevelsEventContent,
space::parent::{InitialSpaceParentEvent, SpaceParentEventContent},
},
};

use crate::{commune, error::Result};

pub async fn service(
access_token: impl AsRef<str>,
parent: OwnedRoomOrAliasId,
alias: Option<String>,
name: Option<String>,
topic: Option<String>,
) -> Result<Response> {
let creation_content = Some(RoomCreationContent {
kind: None,
federate: true,
room_version: RoomVersionId::V11,
predecessor: None,
});

let req = Request::new(
creation_content,
Vec::new(),
Vec::new(),
false,
name,
None,
alias,
topic,
RoomVisibility::Public,
);

let resp = commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await?;

let mut content =
SpaceParentEventContent::new(vec![commune().config.matrix.server_name.clone()]);
content.canonical = true;

let state_event = InitialSpaceParentEvent {
content,
state_key: resp.room_id.clone(),
};

Ok(resp)
}
4 changes: 2 additions & 2 deletions crates/core/src/spaces/create.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use matrix::{
client::create_room::{Request, Response, RoomCreationContent, RoomVisibility},
ruma_common::{room::RoomType, OwnedRoomAliasId, RoomVersionId},
ruma_common::{room::RoomType, RoomVersionId},
ruma_events::room::power_levels::RoomPowerLevelsEventContent,
};

Expand All @@ -16,7 +16,7 @@ pub async fn service(
power_levels.events_default = 100.into();

let creation_content = Some(RoomCreationContent {
kind: RoomType::Space,
kind: Some(RoomType::Space),
federate: true,
room_version: RoomVersionId::V11,
predecessor: None,
Expand Down
1 change: 1 addition & 0 deletions crates/matrix/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod login;
pub mod logout;
pub mod register;
pub mod create_room;
pub mod rooms;

pub mod account;
pub mod profile;
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix/src/client/create_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct RoomCreationContent {
pub predecessor: Option<PreviousRoom>,

#[serde(rename = "type")]
pub kind: RoomType,
pub kind: Option<RoomType>,
}

#[derive(Clone, Default, Debug, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/matrix/src/client/rooms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod state;
27 changes: 27 additions & 0 deletions crates/matrix/src/client/rooms/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use ruma_common::{
api::{request, response, Metadata},
metadata,
};

#[allow(dead_code)]
const METADATA: Metadata = metadata! {
method: POST,
rate_limited: false,
authentication: AccessToken,
history: {
unstable => "/_matrix/client/v3/logout",
}
};

#[request(error = crate::Error)]
pub struct Request {}

#[allow(clippy::new_without_default)]
impl Request {
pub fn new() -> Self {
Self {}
}
}

#[response(error = crate::Error)]
pub struct Response {}
1 change: 1 addition & 0 deletions crates/router/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod account;
pub mod relative;
pub mod register;
pub mod spaces;
pub mod rooms;
1 change: 1 addition & 0 deletions crates/router/src/api/rooms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod root;
Empty file.
41 changes: 41 additions & 0 deletions crates/router/src/api/rooms/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use axum::{
response::{IntoResponse, Response},
Json,
};
use axum_extra::{
headers::{authorization::Bearer, Authorization},
TypedHeader,
};
use matrix::ruma_common::OwnedRoomOrAliasId;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Payload {
pub alias: Option<String>,
pub name: Option<String>,
pub topic: Option<String>,
pub parent: OwnedRoomOrAliasId,
}

pub async fn handler(
TypedHeader(access_token): TypedHeader<Authorization<Bearer>>,
Json(payload): Json<Payload>,
) -> Response {
use commune::rooms::create::service;

match service(
access_token.token(),
payload.alias,
payload.name,
payload.topic,
)
.await
{
Ok(resp) => Json(resp).into_response(),
Err(e) => {
tracing::warn!(?e, "failed to create space");

e.into_response()
}
}
}
Empty file.

0 comments on commit 6237893

Please sign in to comment.