From ec9b699c6e1f2c1b9e8f2b632ef954d73aca2273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Wed, 22 Jan 2020 22:35:31 +0100 Subject: [PATCH] convert plume-common to rocket async it only took 3 hours of @jebrosen's patient help. --- plume-common/src/activity_pub/mod.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index 58c270e2..3a473499 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -4,7 +4,7 @@ use reqwest::r#async::ClientBuilder; use rocket::{ http::Status, request::{FromRequest, Request}, - response::{Responder, Response}, + response::{Responder, Response, ResultFuture}, Outcome, }; use serde_json; @@ -63,14 +63,21 @@ impl ActivityStream { } } -impl<'r, O: Object> Responder<'r> for ActivityStream { - fn respond_to(self, request: &Request<'_>) -> Result, Status> { - let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?; - json["@context"] = context(); - serde_json::to_string(&json).respond_to(request).map(|r| { - Response::build_from(r) - .raw_header("Content-Type", "application/activity+json") - .finalize() +impl<'r, O: Object + Send + 'r> Responder<'r> for ActivityStream { + fn respond_to(self, request: &'r Request<'_>) -> ResultFuture<'r> { + Box::pin(async move { + let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?; + json["@context"] = context(); + let result = serde_json::to_string(&json).map_err(rocket::response::Debug); + match result.respond_to(request).await { + Ok(r) => { + Response::build_from(r) + .raw_header("Content-Type", "application/activity+json") + .ok() + .await + } + Err(e) => Err(e), + } }) } }