Add a ApRequest request guard to accept multiple ActivityPub header on one route

This commit is contained in:
Bat 2018-07-09 17:24:53 +02:00
parent 3796aa48c2
commit 8e47219d82

View File

@ -2,9 +2,10 @@ use activitypub::{Activity, Actor, Object, Link};
use array_tool::vec::Uniq; use array_tool::vec::Uniq;
use reqwest::Client; use reqwest::Client;
use rocket::{ use rocket::{
Outcome,
http::Status, http::Status,
response::{Response, Responder}, response::{Response, Responder},
request::Request request::{FromRequest, Request}
}; };
use serde_json; use serde_json;
@ -59,6 +60,25 @@ impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
} }
} }
pub struct ApRequest;
impl<'a, 'r> FromRequest<'a, 'r> for ApRequest {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
request.headers().get_one("Content-Type").map(|header| header.split(",").map(|ct| match ct {
"application/ld+json; profile=\"w3.org/ns/activitystreams\"" |
"application/ld+json;profile=\"w3.org/ns/activitystreams\"" |
"application/activity+json" |
"application/ld+json" => Outcome::Success(ApRequest),
_ => Outcome::Forward(())
}).fold(Outcome::Forward(()), |out, ct| if out.is_success() {
out
} else {
ct
})).unwrap_or(Outcome::Forward(()))
}
}
pub fn broadcast<A: Activity, S: sign::Signer, T: inbox::WithInbox + Actor>(sender: &S, act: A, to: Vec<T>) { pub fn broadcast<A: Activity, S: sign::Signer, T: inbox::WithInbox + Actor>(sender: &S, act: A, to: Vec<T>) {
let boxes = to.into_iter() let boxes = to.into_iter()
.map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url())) .map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url()))