Skip to content

Commit

Permalink
wip: space support
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Apr 1, 2024
1 parent 6237893 commit 12bd98c
Show file tree
Hide file tree
Showing 57 changed files with 687 additions and 101 deletions.
4 changes: 4 additions & 0 deletions crates/core/src/direct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! This library deals with personal messages, enabling `m.direct` on all rooms created through
//! this endpoint. The name `direct` was chosen to avoid confusion with regular Matrix rooms.

pub mod create;
37 changes: 37 additions & 0 deletions crates/core/src/direct/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use matrix::{
client::create_room::{Request, Response, RoomCreationContent, RoomVisibility},
ruma_common::{OwnedUserId, RoomVersionId},
};

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

pub async fn service(
access_token: impl AsRef<str>,
name: Option<String>,
topic: Option<String>,
invite: Vec<OwnedUserId>,
) -> 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(),
invite,
true,
name,
None,
None,
topic,
RoomVisibility::Private,
);

commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await
.map_err(Into::into)
}
3 changes: 3 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum Error {
#[error("forwarding a Matrix request failed: {0}")]
Matrix(#[from] matrix::HandleError),

#[error("(de)serializing type failed: {0}")]
Serde(#[from] serde_json::Error),

#[error("instance does not allow email address originating from this domain")]
EmailDomain,

Expand Down
22 changes: 22 additions & 0 deletions crates/core/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use matrix::{
client::directory::room::Request,
ruma_common::{OwnedRoomId, OwnedRoomOrAliasId},
};

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

pub async fn get_room_id(
room_or_alias_id: impl Into<OwnedRoomOrAliasId>,
) -> Result<OwnedRoomId, Error> {
// this cannot error, `Result<T>` is just provided in place of an enum
// https://github.com/ruma/ruma/issues/1761

match room_or_alias_id.into().try_into() {
Ok(room_id) => Ok(room_id),
Err(room_alias) => commune()
.send_matrix_request(Request::new(room_alias), None)
.await
.map(|resp| resp.room_id)
.map_err(Into::into),
}
}
4 changes: 3 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

pub mod config;
pub mod error;
pub mod helpers;
pub mod util;

pub mod account;
pub mod direct;
pub mod profile;
pub mod spaces;
pub mod rooms;
pub mod membership;

use std::sync::RwLock;

Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/membership.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod join;
pub mod leave;
19 changes: 19 additions & 0 deletions crates/core/src/membership/join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use matrix::{
client::membership::join::{Request, Response},
ruma_common::OwnedRoomOrAliasId,
};

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

pub async fn service(
access_token: impl AsRef<str>,
room_id: impl Into<OwnedRoomOrAliasId>,
reason: Option<String>,
) -> Result<Response> {
let req = Request::new(room_id.into(), reason);

commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await
.map_err(Into::into)
}
38 changes: 38 additions & 0 deletions crates/core/src/membership/leave.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use matrix::{
client::{
self,
membership::leave::{Request, Response},
},
ruma_common::OwnedRoomOrAliasId,
};

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

pub async fn service(
access_token: impl AsRef<str>,
room_or_alias_id: impl Into<OwnedRoomOrAliasId>,
reason: Option<String>,
) -> Result<Response> {
let room_or_alias_id: OwnedRoomOrAliasId = room_or_alias_id.into();

// this cannot error, `Result<T>` is just provided in place of an enum
// https://github.com/ruma/ruma/issues/1761
let room_id = match room_or_alias_id.try_into() {
Ok(room_id) => room_id,
Err(room_alias) => {
let req = client::directory::room::Request::new(room_alias);

commune()
.send_matrix_request(req, None)
.await
.map(|resp| resp.room_id)?
}
};

let req = Request::new(room_id, reason);

commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await
.map_err(Into::into)
}
52 changes: 0 additions & 52 deletions crates/core/src/rooms/create.rs

This file was deleted.

1 change: 1 addition & 0 deletions crates/core/src/spaces.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod create;
pub mod channels;
1 change: 1 addition & 0 deletions crates/core/src/spaces/channels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod create;
76 changes: 76 additions & 0 deletions crates/core/src/spaces/channels/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use matrix::{
client::{
create_room::{Request, Response, RoomCreationContent, RoomVisibility},
rooms,
},
ruma_common::{OwnedRoomId, RoomVersionId},
ruma_events::{
space::{child::SpaceChildEventContent, parent::SpaceParentEventContent},
EventContent,
},
};

use crate::{commune, error::Result, util::opaque_id::OpaqueId};

pub async fn service(
access_token: impl AsRef<str>,
space_id: OpaqueId,
name: Option<String>,
topic: Option<String>,
) -> Result<Response> {
let server_name = &commune().config.matrix.server_name;
// this should never panic
let space_id = OwnedRoomId::try_from(format!("!{space_id}:{server_name}"))
.expect("failed to parse room ID");

let req = Request::new(
Some(RoomCreationContent {
kind: None,
federate: true,
room_version: RoomVersionId::V11,
predecessor: None,
}),
Vec::new(),
Vec::new(),
false,
name,
None,
None,
topic,
RoomVisibility::Public,
);

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

let mut parent_content = SpaceParentEventContent::new(vec![server_name.to_owned()]);
parent_content.canonical = true;

let req = rooms::state::create::Request::new(
parent_content.event_type(),
resp.room_id.clone(),
Some(resp.room_id.to_string()),
parent_content,
)?;

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

let mut child_content = SpaceChildEventContent::new(vec![server_name.to_owned()]);
child_content.suggested = true;

let req = rooms::state::create::Request::new(
child_content.event_type(),
space_id.clone(),
Some(space_id.to_string()),
child_content,
)?;

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

Ok(resp)
}
1 change: 1 addition & 0 deletions crates/core/src/spaces/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub async fn service(
name: Option<String>,
topic: Option<String>,
) -> Result<Response> {
// Only state events should be allowed in spaces
let mut power_levels = RoomPowerLevelsEventContent::new();
power_levels.events_default = 100.into();

Expand Down
1 change: 1 addition & 0 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod secret;
pub mod opaque_id;
34 changes: 34 additions & 0 deletions crates/core/src/util/opaque_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{fmt::Display, str::FromStr};

use matrix::ruma_identifiers_validation::Error;
use serde::{de, Deserialize, Deserializer};

// helper type to validate the opaque part of a room ID separately.
pub struct OpaqueId(String);

impl Display for OpaqueId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0.as_str())
}
}

impl FromStr for OpaqueId {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.chars().all(char::is_alphanumeric) {
true => Ok(OpaqueId(s.to_owned())),
false => Err(Error::InvalidCharacters),
}
}
}

impl<'de> Deserialize<'de> for OpaqueId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(de::Error::custom)
}
}
2 changes: 1 addition & 1 deletion crates/matrix/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//! reference: https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/index.html

pub mod registration_tokens;
// pub mod room;
pub mod rooms;
// pub mod session;
// pub mod user;
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use ruma_common::{
use ruma_events::room::{history_visibility::HistoryVisibility, join_rules::JoinRule};
use serde::Deserialize;

pub mod delete_room;
pub mod get_members;
pub mod get_room;
pub mod get_rooms;
pub mod get_state;
pub mod members;
pub mod state;

pub mod get;
pub mod get_many;
pub mod delete;

#[derive(Clone, Debug, Deserialize)]
pub struct Room {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub struct Request {
pub room_id: OwnedRoomId,
}

impl Request {
pub fn new(room_id: OwnedRoomId) -> Self {
Self { room_id }
}
}

#[response(error = crate::Error)]
pub struct Response {
#[ruma_api(body)]
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions crates/matrix/src/admin/rooms/members.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod get;
File renamed without changes.
1 change: 1 addition & 0 deletions crates/matrix/src/admin/rooms/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod get;
File renamed without changes.
7 changes: 5 additions & 2 deletions crates/matrix/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
pub mod login;
pub mod logout;
pub mod register;
pub mod account;
pub mod profile;

pub mod create_room;
pub mod rooms;
pub mod directory;
pub mod membership;

pub mod account;
pub mod profile;
pub mod uiaa;
Loading

0 comments on commit 12bd98c

Please sign in to comment.