Compare commits

...

6 commits

Author SHA1 Message Date
Jonas Platte
42a7a9f795
WIP 2021-04-29 20:50:09 +02:00
Jonas Platte
54a0f21347
WIP 2021-04-29 20:17:14 +02:00
Jonas Platte
54f4fd2cc8
Remove explicit setting of content-type header
`try_into_http_request` takes care of this already.
2021-04-29 20:16:55 +02:00
Jonas Platte
ac5dd5e251
Fix formatting 2021-04-29 20:16:52 +02:00
Jonas Platte
6510ffa0fa
Fix clippy warnings 2021-04-29 20:16:49 +02:00
Jonas Platte
caa186ad22
Bump ruma 2021-04-29 20:15:51 +02:00
5 changed files with 123 additions and 64 deletions

80
Cargo.lock generated
View file

@ -42,6 +42,27 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f093eed78becd229346bf859eec0aa4dd7ddde0757287b2b4107a1f09c80002"
[[package]]
name = "async-stream"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a26cb53174ddd320edfff199a853f93d571f48eeb4dde75e67a9a3dbb7b7e5e"
dependencies = [
"async-stream-impl",
"futures-core",
]
[[package]]
name = "async-stream-impl"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db134ba52475c060f3329a8ef0f8786d6b872ed01515d4b79c162e5798da1340"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "async-trait"
version = "0.1.50"
@ -1634,12 +1655,13 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.0.3"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"assign",
"js_int",
"ruma-api",
"ruma-appservice-api",
"ruma-client",
"ruma-client-api",
"ruma-common",
"ruma-events",
@ -1654,7 +1676,7 @@ dependencies = [
[[package]]
name = "ruma-api"
version = "0.17.0-alpha.4"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"bytes",
"http",
@ -1670,7 +1692,7 @@ dependencies = [
[[package]]
name = "ruma-api-macros"
version = "0.17.0-alpha.4"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -1681,7 +1703,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.2.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"ruma-api",
"ruma-common",
@ -1692,10 +1714,31 @@ dependencies = [
"serde_json",
]
[[package]]
name = "ruma-client"
version = "0.5.0-alpha.2"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"assign",
"async-stream",
"async-trait",
"bytes",
"futures-core",
"http",
"reqwest",
"ruma-api",
"ruma-common",
"ruma-events",
"ruma-identifiers",
"ruma-serde",
"serde",
"serde_json",
]
[[package]]
name = "ruma-client-api"
version = "0.10.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"assign",
"bytes",
@ -1715,7 +1758,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"indexmap",
"js_int",
@ -1731,7 +1774,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.22.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"js_int",
"ruma-common",
@ -1745,7 +1788,7 @@ dependencies = [
[[package]]
name = "ruma-events-macros"
version = "0.22.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -1756,7 +1799,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.1.0-alpha.2"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"js_int",
"ruma-api",
@ -1771,7 +1814,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers"
version = "0.19.0"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"paste",
"rand",
@ -1785,7 +1828,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-macros"
version = "0.19.0"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"proc-macro2",
"quote",
@ -1796,13 +1839,14 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
[[package]]
name = "ruma-identity-service-api"
version = "0.1.0-alpha.1"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"js_int",
"ruma-api",
"ruma-common",
"ruma-identifiers",
@ -1814,7 +1858,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.1.0-alpha.1"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"js_int",
"ruma-api",
@ -1829,7 +1873,7 @@ dependencies = [
[[package]]
name = "ruma-serde"
version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"bytes",
"form_urlencoded",
@ -1843,7 +1887,7 @@ dependencies = [
[[package]]
name = "ruma-serde-macros"
version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -1854,7 +1898,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.7.0"
source = "git+https://github.com/ruma/ruma?rev=d27584ae3bdc035529e7389f1c392d4c96f9f8eb#d27584ae3bdc035529e7389f1c392d4c96f9f8eb"
source = "git+https://github.com/ruma/ruma?rev=097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2#097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2"
dependencies = [
"base64 0.13.0",
"ring",
@ -2167,7 +2211,7 @@ dependencies = [
[[package]]
name = "state-res"
version = "0.1.0"
source = "git+https://github.com/ruma/state-res?rev=ce665d213fffeaa47e146d01c6b87f9eb9feaa52#ce665d213fffeaa47e146d01c6b87f9eb9feaa52"
source = "git+https://github.com/ruma/state-res?rev=bca91bcc17df4d16942ff8b8c3be622b1795e3f0#bca91bcc17df4d16942ff8b8c3be622b1795e3f0"
dependencies = [
"itertools 0.10.0",
"log",

View file

@ -18,12 +18,12 @@ rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "801e04bd5
#rocket = { git = "https://github.com/timokoesters/Rocket.git", branch = "empty_parameters", default-features = false, features = ["tls"] }
# Used for matrix spec type definitions and helpers
ruma = { git = "https://github.com/ruma/ruma", rev = "d27584ae3bdc035529e7389f1c392d4c96f9f8eb", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/ruma/ruma", rev = "097f20c837fcbaef1d9c18c30d3ec5a24a6e4ec2", features = ["client-reqwest", "compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { git = "https://github.com/timokoesters/ruma", rev = "220d5b4a76b3b781f7f8297fbe6b14473b04214b", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { path = "../ruma/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] }
# Used when doing state resolution
state-res = { git = "https://github.com/ruma/state-res", rev = "ce665d213fffeaa47e146d01c6b87f9eb9feaa52", features = ["unstable-pre-spec"] }
state-res = { git = "https://github.com/ruma/state-res", rev = "bca91bcc17df4d16942ff8b8c3be622b1795e3f0", features = ["unstable-pre-spec"] }
#state-res = { path = "../state-res", features = ["unstable-pre-spec"] }
# Used for long polling and federation sender, should be the same as rocket::tokio

View file

@ -1,8 +1,10 @@
use crate::{utils, Error, Result};
use bytes::BytesMut;
use http::header::{HeaderValue, CONTENT_TYPE};
use log::warn;
use ruma::api::{IncomingResponse, OutgoingRequest, SendAccessToken};
use ruma::{
api::{IncomingResponse, OutgoingRequest, SendAccessToken},
client::HttpClientExt,
};
use std::{
convert::{TryFrom, TryInto},
fmt::Debug,
@ -20,33 +22,33 @@ where
let destination = registration.get("url").unwrap().as_str().unwrap();
let hs_token = registration.get("hs_token").unwrap().as_str().unwrap();
let mut http_request = request
.try_into_http_request::<BytesMut>(&destination, SendAccessToken::IfRequired(""))
.unwrap()
.map(|body| body.freeze());
globals
.reqwest_client()
.send_customized_matrix_request(
&destination,
SendAccessToken::IfRequired(""),
request,
|http_request| {
let mut parts = http_request.uri().clone().into_parts();
let old_path_and_query = parts.path_and_query.unwrap().as_str().to_owned();
let symbol = if old_path_and_query.contains('?') {
"&"
} else {
"?"
};
let mut parts = http_request.uri().clone().into_parts();
let old_path_and_query = parts.path_and_query.unwrap().as_str().to_owned();
let symbol = if old_path_and_query.contains('?') {
"&"
} else {
"?"
};
parts.path_and_query =
Some((old_path_and_query + symbol + "access_token=" + hs_token).parse()?);
*http_request.uri_mut() = parts.try_into()?;
parts.path_and_query = Some(
(old_path_and_query + symbol + "access_token=" + hs_token)
.parse()
.unwrap(),
);
*http_request.uri_mut() = parts.try_into().expect("our manipulation is always valid");
http_request.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_str("application/json").unwrap(),
);
let mut reqwest_request = reqwest::Request::try_from(http_request)
.expect("all http requests are valid reqwest requests");
Ok(())
},
)
.await
.map_err(|e| {
warn!("Appservice error: {}", e);
Error::BadServerResponse("Server returned an error.")
})?;
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));

View file

@ -8,9 +8,11 @@ use ruma::{
set_room_account_data,
},
},
events::{custom::CustomEventContent, BasicEvent},
events::{custom::CustomEventContent, AnyBasicEventContent, BasicEvent},
serde::Raw,
};
use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue;
#[cfg(feature = "conduit_bin")]
use rocket::{get, put};
@ -91,14 +93,17 @@ pub async fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data = db
let event = db
.account_data
.get::<Raw<ruma::events::AnyBasicEvent>>(None, sender_user, body.event_type.clone().into())?
.get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
db.flush().await?;
Ok(get_global_account_data::Response { account_data: data }.into())
let account_data = serde_json::from_str::<ExtractEventContent>(event.get())
.expect("event to contain a content field")
.content;
Ok(get_global_account_data::Response { account_data }.into())
}
#[cfg_attr(
@ -115,16 +120,24 @@ pub async fn get_room_account_data_route(
) -> ConduitResult<get_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let data = db
let event = db
.account_data
.get::<Raw<ruma::events::AnyBasicEvent>>(
.get::<Box<RawJsonValue>>(
Some(&body.room_id),
sender_user,
body.event_type.clone().into(),
)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
db.flush().await?;
Ok(get_room_account_data::Response { account_data: data }.into())
let account_data = serde_json::from_str::<ExtractEventContent>(event.get())
.expect("event to contain a content field")
.content;
Ok(get_room_account_data::Response { account_data }.into())
}
#[derive(Deserialize)]
struct ExtractEventContent {
content: Raw<AnyBasicEventContent>,
}

View file

@ -49,11 +49,11 @@ pub async fn set_displayname_route(
)?
.ok_or_else(|| {
Error::bad_database(
"Tried to send displayname update for user not in the room.",
)
"Tried to send displayname update for user not in the \
room.",
)
})?
.content
.clone(),
.content,
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
@ -150,11 +150,11 @@ pub async fn set_avatar_url_route(
)?
.ok_or_else(|| {
Error::bad_database(
"Tried to send displayname update for user not in the room.",
)
"Tried to send displayname update for user not in the \
room.",
)
})?
.content
.clone(),
.content,
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()