Skip to content

Commit

Permalink
feat: handler addition, one to rule them all
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 28, 2024
1 parent 87f60df commit b1b42ae
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 205 deletions.
14 changes: 6 additions & 8 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod account;
//! This library deals with our core logic, such as authorizing user interactions,
//! forwarding regular events and constructing custom requests.
pub mod session;
pub mod auth;
pub mod error;
pub mod mail;
Expand All @@ -9,14 +12,11 @@ pub use error::{Error, HttpStatusCode, Result};

use mail::service::MailService;
use room::service::RoomService;
use tokio::sync::mpsc::Receiver;
use url::Url;

use std::{fmt::Debug, str::FromStr, sync::Arc};

use matrix::Client as MatrixAdminClient;

use self::{account::service::AccountService, auth::service::AuthService};

pub mod env {
pub const COMMUNE_SYNAPSE_HOST: &str = "COMMUNE_SYNAPSE_HOST";
pub const COMMUNE_SYNAPSE_ADMIN_TOKEN: &str = "COMMUNE_SYNAPSE_ADMIN_TOKEN";
Expand Down Expand Up @@ -92,9 +92,7 @@ impl CommuneConfig {
}

pub struct Commune {
pub account: Arc<AccountService>,
pub auth: Arc<AuthService>,
pub room: Arc<RoomService>,

}

impl Commune {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 9 additions & 2 deletions crates/matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ edition = "2021"
publish = false

[dependencies]
ruma-events = { version = "0.27.11", features = ["html", "markdown"] }
ruma-events = { version = "0.27.11", default_features = false, features = [
"html",
"markdown",
] }
ruma-common = { version = "0.12.0", default_features = false, features = [
"api",
"rand",
] }
ruma-macros = "0.12.0"
ruma-macros = { version = "0.12.0", default_features = false }
ruma-client = { version = "0.12.0", default_features = false }

# Workspace Dependencies
mime = { workspace = true }
Expand All @@ -22,6 +26,9 @@ sha1 = { workspace = true }
url = { workspace = true, features = ["serde"] }
hex = { workspace = true }
hmac = { workspace = true }
http.workspace = true
bytes = "1.5.0"
async-trait = "0.1.77"

[features]
client = []
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//!
//! reference: https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/index.html
mod room;
mod session;
mod user;
pub mod room;
pub mod session;
pub mod user;
6 changes: 3 additions & 3 deletions crates/matrix/src/admin/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use ruma_common::{
use ruma_events::room::{history_visibility::HistoryVisibility, join_rules::JoinRule};
use serde::Deserialize;

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

#[derive(Clone, Debug, Deserialize)]
pub struct Room {
Expand Down
4 changes: 1 addition & 3 deletions crates/matrix/src/admin/room/delete_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ pub struct Request {
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub new_room: Option<NewRoomParams>,

#[serde(skip_serializing_if = "ruma_common::serde::is_default")]
pub block: bool,

#[serde(skip_serializing_if = "ruma_common::serde::is_true")]
pub purge: bool,

#[serde(skip_serializing_if = "ruma_common::serde::is_default")]
pub force_purge: bool,
}

#[response(error = crate::Error)]
pub struct Response {
delete_id: String,
pub delete_id: String,
}

#[derive(Clone, Debug, Serialize)]
Expand Down
12 changes: 6 additions & 6 deletions crates/matrix/src/admin/room/get_rooms.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruma_common::{
api::{request, response, Metadata, Direction},
api::{request, response, Direction, Metadata},
metadata,
};
use serde::Serialize;
Expand Down Expand Up @@ -39,16 +39,16 @@ pub struct Request {

#[response(error = crate::Error)]
pub struct Response {
rooms: Vec<Room>,
pub rooms: Vec<Room>,

offset: u64,
pub offset: u64,

#[serde(rename = "total_rooms")]
total: u64,
pub total: u64,

next_batch: Option<String>,
pub next_batch: Option<String>,

prev_batch: Option<String>,
pub prev_batch: Option<String>,
}

#[derive(Clone, Default, Debug, Serialize)]
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix/src/admin/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
use hmac::Mac;

mod get_nonce;
mod register;
pub mod get_nonce;
pub mod register;

#[derive(Clone, Debug)]
pub struct Hmac {
inner: Vec<u8>,
}

impl Hmac {
fn new(
pub fn new(
shared_secret: &str,
nonce: &str,
username: &str,
Expand All @@ -39,7 +39,7 @@ impl Hmac {
})
}

fn get(&self) -> String {
pub fn get(&self) -> String {
hex::encode(&self.inner)
}
}
2 changes: 1 addition & 1 deletion crates/matrix/src/admin/session/get_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ pub struct Request {}

#[response(error = crate::Error)]
pub struct Response {
nonce: String,
pub nonce: String,
}
2 changes: 1 addition & 1 deletion crates/matrix/src/client/room/create_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const METADATA: Metadata = metadata! {
rate_limited: true,
authentication: AccessToken,
history: {
1.9 => "/_matrix/client/v3/createRoom",
unstable => "/_matrix/client/v3/createRoom",
}
};

Expand Down
56 changes: 56 additions & 0 deletions crates/matrix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,61 @@
pub mod admin;
pub mod client;

use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use ruma_client::{DefaultConstructibleHttpClient, HttpClient, HttpClientExt};

#[allow(unused_imports)]
use ruma_common::api::error::MatrixError as Error;

#[derive(Debug)]
pub struct Handle {
inner: reqwest::Client,
}

impl Handle {
pub async fn new() {
self.send_matrix_request(, access_token, for_versions, request)
}

pub async fn dispatch(&self) {
self.send_matrix_request(, access_token, for_versions, request)
}
}

#[async_trait]
impl HttpClient for Handle {
type RequestBody = BytesMut;
type ResponseBody = Bytes;
type Error = reqwest::Error;

async fn send_http_request(
&self,
req: http::Request<BytesMut>,
) -> Result<http::Response<Bytes>, reqwest::Error> {
let req = req.map(|body| body.freeze()).try_into()?;
let mut res = self.inner.execute(req).await?;

let mut http_builder = http::Response::builder()
.status(res.status())
.version(res.version());
std::mem::swap(
http_builder
.headers_mut()
.expect("http::response::Builder to be usable"),
res.headers_mut(),
);

Ok(http_builder
.body(res.bytes().await?)
.expect("http::Response construction to work"))
}
}

impl DefaultConstructibleHttpClient for Handle {
fn default() -> Self {
Self {
inner: reqwest::Client::new(),
}
}
}
1 change: 0 additions & 1 deletion crates/matrix/src/token/mod.rs

This file was deleted.

Loading

0 comments on commit b1b42ae

Please sign in to comment.