Merge pull request 'Timo's Conditionally emit rocket::http_verb attr macros if lib/bin' (#157) from timo-make-lib into master
Reviewed-on: https://git.koesters.xyz/timo/conduit/pulls/157
This commit is contained in:
commit
c4260bde81
9 changed files with 457 additions and 174 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -261,7 +261,6 @@ checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd"
|
|||
name = "conduit"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.12.3",
|
||||
"directories",
|
||||
"http",
|
||||
"image",
|
||||
|
@ -276,7 +275,6 @@ dependencies = [
|
|||
"serde_json",
|
||||
"sled",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
44
Cargo.toml
44
Cargo.toml
|
@ -12,19 +12,31 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "8d779caa22c63b15a6c3ceb75d8f6d4971b2eb67", features = ["tls"] }
|
||||
http = "0.2.1"
|
||||
log = "0.4.8"
|
||||
sled = "0.32.0"
|
||||
directories = "2.0.2"
|
||||
js_int = "0.1.5"
|
||||
serde_json = { version = "1.0.53", features = ["raw_value"] }
|
||||
serde = "1.0.111"
|
||||
tokio = { version = "0.2.21", features = ["macros"] }
|
||||
rand = "0.7.3"
|
||||
rust-argon2 = "0.8.2"
|
||||
reqwest = "0.10.6"
|
||||
base64 = "0.12.1"
|
||||
thiserror = "1.0.19"
|
||||
image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] }
|
||||
ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "e047c647ddcb368e7eb1e05ae8823a9494273457" }
|
||||
# TODO: This can become optional as soon as proper configs are supported
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "8d779caa22c63b15a6c3ceb75d8f6d4971b2eb67", features = ["tls"], optional = false } # Used to handle requests
|
||||
ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "e047c647ddcb368e7eb1e05ae8823a9494273457" } # Used for matrix spec type definitions and helpers
|
||||
sled = "0.32.0" # Used for storing data permanently
|
||||
log = "0.4.8" # Used for emitting log entries
|
||||
http = "0.2.1" # Used for rocket<->ruma conversions
|
||||
directories = "2.0.2" # Used to find data directory for default db path
|
||||
js_int = "0.1.5" # Used for number types for ruma
|
||||
serde_json = { version = "1.0.53", features = ["raw_value"] } # Used for ruma wrapper
|
||||
serde = "1.0.111" # Used for pdu definition
|
||||
rand = "0.7.3" # Used for secure identifiers
|
||||
rust-argon2 = "0.8.2" # Used to hash passwords
|
||||
reqwest = "0.10.6" # Used to send requests
|
||||
thiserror = "1.0.19" # Used for conduit::Error type
|
||||
image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } # Used to generate thumbnails for images
|
||||
|
||||
[features]
|
||||
default = ["conduit_bin"]
|
||||
conduit_bin = [] # TODO: add rocket to this when it is optional
|
||||
|
||||
[[bin]]
|
||||
name = "conduit"
|
||||
path = "src/main.rs"
|
||||
required-features = ["conduit_bin"]
|
||||
|
||||
[lib]
|
||||
name = "conduit"
|
||||
path = "src/lib.rs"
|
||||
|
|
File diff suppressed because it is too large
Load diff
23
src/error.rs
23
src/error.rs
|
@ -1,16 +1,18 @@
|
|||
use crate::RumaResponse;
|
||||
use http::StatusCode;
|
||||
use log::error;
|
||||
use rocket::{
|
||||
response::{self, Responder},
|
||||
Request,
|
||||
};
|
||||
use ruma::api::client::{
|
||||
error::{Error as RumaError, ErrorKind},
|
||||
r0::uiaa::{UiaaInfo, UiaaResponse},
|
||||
};
|
||||
use ruma::api::client::{error::ErrorKind, r0::uiaa::UiaaInfo};
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::RumaResponse,
|
||||
http::StatusCode,
|
||||
rocket::{
|
||||
response::{self, Responder},
|
||||
Request,
|
||||
},
|
||||
ruma::api::client::{error::Error as RumaError, r0::uiaa::UiaaResponse},
|
||||
};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -46,6 +48,7 @@ impl Error {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
impl<'r, 'o> Responder<'r, 'o> for Error
|
||||
where
|
||||
'o: 'r,
|
||||
|
|
24
src/lib.rs
Normal file
24
src/lib.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
pub mod client_server;
|
||||
mod database;
|
||||
mod error;
|
||||
mod pdu;
|
||||
pub mod push_rules;
|
||||
mod ruma_wrapper;
|
||||
mod utils;
|
||||
|
||||
pub use database::Database;
|
||||
pub use error::{Error, Result};
|
||||
pub use pdu::PduEvent;
|
||||
pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse};
|
||||
use std::ops::Deref;
|
||||
|
||||
pub struct State<'r, T: Send + Sync + 'static>(&'r T);
|
||||
|
||||
impl<'r, T: Send + Sync + 'static> Deref for State<'r, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &T {
|
||||
self.0
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ mod utils;
|
|||
pub use database::Database;
|
||||
pub use error::{Error, Result};
|
||||
pub use pdu::PduEvent;
|
||||
pub use rocket::State;
|
||||
pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse};
|
||||
|
||||
use rocket::{fairing::AdHoc, routes};
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use js_int::uint;
|
||||
use ruma::{
|
||||
push::{
|
||||
Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule,
|
||||
|
@ -185,7 +184,7 @@ pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule {
|
|||
rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(),
|
||||
conditions: vec![
|
||||
PushCondition::RoomMemberCount {
|
||||
is: RoomMemberCountIs::from(uint!(2)..),
|
||||
is: RoomMemberCountIs::from(2_u32.into()..),
|
||||
},
|
||||
PushCondition::EventMatch {
|
||||
key: "type".to_owned(),
|
||||
|
@ -208,7 +207,7 @@ pub fn room_one_to_one_rule() -> ConditionalPushRule {
|
|||
rule_id: ".m.rule.room_one_to_one".to_owned(),
|
||||
conditions: vec![
|
||||
PushCondition::RoomMemberCount {
|
||||
is: RoomMemberCountIs::from(uint!(2)..),
|
||||
is: RoomMemberCountIs::from(2_u32.into()..),
|
||||
},
|
||||
PushCondition::EventMatch {
|
||||
key: "type".to_owned(),
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
use crate::{utils, Error};
|
||||
use log::warn;
|
||||
use rocket::{
|
||||
data::{Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed},
|
||||
http::Status,
|
||||
response::{self, Responder},
|
||||
Outcome::*,
|
||||
Request, State,
|
||||
use crate::Error;
|
||||
use ruma::identifiers::{DeviceId, UserId};
|
||||
use std::{convert::TryInto, ops::Deref};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use {
|
||||
crate::utils,
|
||||
log::warn,
|
||||
rocket::{
|
||||
data::{
|
||||
Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed,
|
||||
},
|
||||
http::Status,
|
||||
response::{self, Responder},
|
||||
tokio::io::AsyncReadExt,
|
||||
Outcome::*,
|
||||
Request, State,
|
||||
},
|
||||
ruma::api::Endpoint,
|
||||
std::io::Cursor,
|
||||
};
|
||||
use ruma::{api::Endpoint, DeviceId, UserId};
|
||||
use std::{convert::TryInto, io::Cursor, ops::Deref};
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
/// This struct converts rocket requests into ruma structs by converting them into http requests
|
||||
/// first.
|
||||
|
@ -20,6 +29,7 @@ pub struct Ruma<T> {
|
|||
pub json_body: Option<Box<serde_json::value::RawValue>>, // This is None when body is not a valid string
|
||||
}
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
impl<'a, T: Endpoint> FromTransformedData<'a> for Ruma<T> {
|
||||
type Error = (); // TODO: Better error handling
|
||||
type Owned = Data;
|
||||
|
@ -119,6 +129,7 @@ impl<T: TryInto<http::Response<Vec<u8>>>> From<T> for RumaResponse<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
impl<'r, 'o, T> Responder<'r, 'o> for RumaResponse<T>
|
||||
where
|
||||
T: Send + TryInto<http::Response<Vec<u8>>>,
|
||||
|
|
|
@ -130,14 +130,14 @@ pub async fn send_request<T: Endpoint>(
|
|||
}
|
||||
}
|
||||
|
||||
#[get("/.well-known/matrix/server")]
|
||||
#[cfg_attr(feature = "conduit_bin",get("/.well-known/matrix/server"))]
|
||||
pub fn well_known_server() -> Json<String> {
|
||||
rocket::response::content::Json(
|
||||
json!({ "m.server": "matrixtesting.koesters.xyz:14004"}).to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/_matrix/federation/v1/version")]
|
||||
#[cfg_attr(feature = "conduit_bin",get("/_matrix/federation/v1/version"))]
|
||||
pub fn get_server_version() -> MatrixResult<get_server_version::Response, Error> {
|
||||
MatrixResult(Ok(get_server_version::Response {
|
||||
server: Some(get_server_version::Server {
|
||||
|
@ -147,7 +147,7 @@ pub fn get_server_version() -> MatrixResult<get_server_version::Response, Error>
|
|||
}))
|
||||
}
|
||||
|
||||
#[get("/_matrix/key/v2/server")]
|
||||
#[cfg_attr(feature = "conduit_bin",get("/_matrix/key/v2/server"))]
|
||||
pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
||||
let mut verify_keys = BTreeMap::new();
|
||||
verify_keys.insert(
|
||||
|
@ -177,7 +177,7 @@ pub fn get_server_keys(db: State<'_, Database>) -> Json<String> {
|
|||
Json(response.to_string())
|
||||
}
|
||||
|
||||
#[get("/_matrix/key/v2/server/<_key_id>")]
|
||||
#[cfg_attr(feature = "conduit_bin",get("/_matrix/key/v2/server/<_key_id>"))]
|
||||
pub fn get_server_keys_deprecated(db: State<'_, Database>, _key_id: String) -> Json<String> {
|
||||
get_server_keys(db)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue