plume-common: also make requests async

This commit is contained in:
Igor Galić 2020-01-23 19:38:59 +01:00
parent 75722abc9e
commit fd9764ff17
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86

View File

@ -3,7 +3,7 @@ use array_tool::vec::Uniq;
use reqwest::r#async::ClientBuilder; use reqwest::r#async::ClientBuilder;
use rocket::{ use rocket::{
http::Status, http::Status,
request::{FromRequest, Request}, request::{FromRequestFuture, FromRequestAsync, Request},
response::{Responder, Response, ResultFuture}, response::{Responder, Response, ResultFuture},
Outcome, Outcome,
}; };
@ -84,35 +84,38 @@ impl<'r, O: Object + Send + 'r> Responder<'r> for ActivityStream<O> {
#[derive(Clone)] #[derive(Clone)]
pub struct ApRequest; pub struct ApRequest;
impl<'a, 'r> FromRequest<'a, 'r> for ApRequest { impl<'a, 'r> FromRequestAsync<'a, 'r> for ApRequest {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> { fn from_request(request: &'a Request<'r>) -> FromRequestFuture<'a, Self, Self::Error> {
request Box::pin(async move {
.headers() request
.get_one("Accept") .headers()
.map(|header| { .get_one("Accept")
header .map(|header| {
.split(',') header
.map(|ct| match ct.trim() { .split(',')
// bool for Forward: true if found a valid Content-Type for Plume first (HTML), false otherwise .map(|ct| match ct.trim() {
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\"" // bool for Forward: true if found a valid Content-Type for Plume first (HTML),
| "application/ld+json;profile=\"https://w3.org/ns/activitystreams\"" // false otherwise
| "application/activity+json" "application/ld+json; profile=\"https://w3.org/ns/activitystreams\""
| "application/ld+json" => Outcome::Success(ApRequest), | "application/ld+json;profile=\"https://w3.org/ns/activitystreams\""
"text/html" => Outcome::Forward(true), | "application/activity+json"
_ => Outcome::Forward(false), | "application/ld+json" => Outcome::Success(ApRequest),
}) "text/html" => Outcome::Forward(true),
.fold(Outcome::Forward(false), |out, ct| { _ => Outcome::Forward(false),
if out.clone().forwarded().unwrap_or_else(|| out.is_success()) { })
out .fold(Outcome::Forward(false), |out, ct| {
} else { if out.clone().forwarded().unwrap_or_else(|| out.is_success()) {
ct out
} } else {
}) ct
.map_forward(|_| ()) }
}) })
.unwrap_or(Outcome::Forward(())) .map_forward(|_| ())
})
.unwrap_or(Outcome::Forward(()))
})
} }
} }
pub fn broadcast<S, A, T, C>(sender: &S, act: A, to: Vec<T>) pub fn broadcast<S, A, T, C>(sender: &S, act: A, to: Vec<T>)