]> Untitled Git - lemmy.git/commitdiff
Use pretty print for federation json responses (#2801)
authorNutomic <me@nutomic.com>
Thu, 13 Apr 2023 00:17:23 +0000 (02:17 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Apr 2023 00:17:23 +0000 (20:17 -0400)
crates/apub/src/http/comment.rs
crates/apub/src/http/community.rs
crates/apub/src/http/mod.rs
crates/apub/src/http/person.rs
crates/apub/src/http/post.rs
crates/apub/src/http/site.rs
crates/utils/src/error.rs

index c02bd05450f9561283fa3674cb7c29696cbc0e49..9e78da2ab572091035b01511575663d59f877eb2 100644 (file)
@@ -27,8 +27,8 @@ pub(crate) async fn get_apub_comment(
   }
 
   if !comment.deleted && !comment.removed {
-    Ok(create_apub_response(&comment.into_json(&context).await?))
+    create_apub_response(&comment.into_json(&context).await?)
   } else {
-    Ok(create_apub_tombstone_response(comment.ap_id.clone()))
+    create_apub_tombstone_response(comment.ap_id.clone())
   }
 }
index 4ae50bb05fcc0b1a6c36061315aedf5c917b3756..04ac8e3fd93504e99372e7fb6606d20d53a38548 100644 (file)
@@ -40,9 +40,9 @@ pub(crate) async fn get_apub_community_http(
   if !community.deleted && !community.removed {
     let apub = community.into_json(&context).await?;
 
-    Ok(create_apub_response(&apub))
+    create_apub_response(&apub)
   } else {
-    Ok(create_apub_tombstone_response(community.actor_id.clone()))
+    create_apub_tombstone_response(community.actor_id.clone())
   }
 }
 
@@ -66,7 +66,7 @@ pub(crate) async fn get_apub_community_followers(
 ) -> Result<HttpResponse, LemmyError> {
   let community = Community::read_from_name(context.pool(), &info.community_name, false).await?;
   let followers = GroupFollowers::new(community, &context).await?;
-  Ok(create_apub_response(&followers))
+  create_apub_response(&followers)
 }
 
 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
@@ -83,7 +83,7 @@ pub(crate) async fn get_apub_community_outbox(
     return Err(LemmyError::from_message("deleted"));
   }
   let outbox = ApubCommunityOutbox::read_local(&community, &context).await?;
-  Ok(create_apub_response(&outbox))
+  create_apub_response(&outbox)
 }
 
 #[tracing::instrument(skip_all)]
@@ -99,7 +99,7 @@ pub(crate) async fn get_apub_community_moderators(
     return Err(LemmyError::from_message("deleted"));
   }
   let moderators = ApubCommunityModerators::read_local(&community, &context).await?;
-  Ok(create_apub_response(&moderators))
+  create_apub_response(&moderators)
 }
 
 /// Returns collection of featured (stickied) posts.
@@ -115,5 +115,5 @@ pub(crate) async fn get_apub_community_featured(
     return Err(LemmyError::from_message("deleted"));
   }
   let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
-  Ok(create_apub_response(&featured))
+  create_apub_response(&featured)
 }
index 72819ccbe39b9752e7ebf20ea77a3339ca7b1591..7a4a362faa652360e526fb0ceaaf29a0c62f7a32 100644 (file)
@@ -14,7 +14,7 @@ use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
 use http::StatusCode;
 use lemmy_api_common::context::LemmyContext;
 use lemmy_db_schema::source::activity::Activity;
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::error::{LemmyError, LemmyResult};
 use serde::{Deserialize, Serialize};
 use std::ops::Deref;
 use url::Url;
@@ -30,34 +30,40 @@ pub async fn shared_inbox(
   request: HttpRequest,
   body: Bytes,
   data: Data<LemmyContext>,
-) -> Result<HttpResponse, LemmyError> {
+) -> LemmyResult<HttpResponse> {
   receive_activity::<SharedInboxActivities, UserOrCommunity, LemmyContext>(request, body, &data)
     .await
 }
 
 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
 /// headers.
-fn create_apub_response<T>(data: &T) -> HttpResponse
+///
+/// actix-web doesn't allow pretty-print for json so we need to do this manually.
+fn create_apub_response<T>(data: &T) -> LemmyResult<HttpResponse>
 where
   T: Serialize,
 {
-  HttpResponse::Ok()
-    .content_type(FEDERATION_CONTENT_TYPE)
-    .json(WithContext::new(data, CONTEXT.deref().clone()))
-}
+  let json = serde_json::to_string_pretty(&WithContext::new(data, CONTEXT.clone()))?;
 
-fn create_json_apub_response(data: serde_json::Value) -> HttpResponse {
-  HttpResponse::Ok()
-    .content_type(FEDERATION_CONTENT_TYPE)
-    .json(data)
+  Ok(
+    HttpResponse::Ok()
+      .content_type(FEDERATION_CONTENT_TYPE)
+      .content_type("application/json")
+      .body(json),
+  )
 }
 
-fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
+fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> LemmyResult<HttpResponse> {
   let tombstone = Tombstone::new(id.into());
-  HttpResponse::Gone()
-    .content_type(FEDERATION_CONTENT_TYPE)
-    .status(StatusCode::GONE)
-    .json(WithContext::new(tombstone, CONTEXT.deref().clone()))
+  let json = serde_json::to_string_pretty(&WithContext::new(tombstone, CONTEXT.deref().clone()))?;
+
+  Ok(
+    HttpResponse::Gone()
+      .content_type(FEDERATION_CONTENT_TYPE)
+      .status(StatusCode::GONE)
+      .content_type("application/json")
+      .body(json),
+  )
 }
 
 fn err_object_not_local() -> LemmyError {
@@ -92,6 +98,6 @@ pub(crate) async fn get_activity(
   } else if sensitive {
     Ok(HttpResponse::Forbidden().finish())
   } else {
-    Ok(create_json_apub_response(activity.data))
+    create_apub_response(&activity.data)
   }
 }
index 79696660ce5bc8e2b8b9ac4a96a304446b8c8949..453905eeea872638982eecdda8bdad7826ed911c 100644 (file)
@@ -37,9 +37,9 @@ pub(crate) async fn get_apub_person_http(
   if !person.deleted {
     let apub = person.into_json(&context).await?;
 
-    Ok(create_apub_response(&apub))
+    create_apub_response(&apub)
   } else {
-    Ok(create_apub_tombstone_response(person.actor_id.clone()))
+    create_apub_tombstone_response(person.actor_id.clone())
   }
 }
 
@@ -63,5 +63,5 @@ pub(crate) async fn get_apub_person_outbox(
   let person = Person::read_from_name(context.pool(), &info.user_name, false).await?;
   let outbox_id = generate_outbox_url(&person.actor_id)?.into();
   let outbox = EmptyOutbox::new(outbox_id)?;
-  Ok(create_apub_response(&outbox))
+  create_apub_response(&outbox)
 }
index ed1815c075bba7886974d9854ca23ee6ee49d698..a2e600268a89e501552d70555b1ae5620eaafec7 100644 (file)
@@ -27,8 +27,8 @@ pub(crate) async fn get_apub_post(
   }
 
   if !post.deleted && !post.removed {
-    Ok(create_apub_response(&post.into_json(&context).await?))
+    create_apub_response(&post.into_json(&context).await?)
   } else {
-    Ok(create_apub_tombstone_response(post.ap_id.clone()))
+    create_apub_tombstone_response(post.ap_id.clone())
   }
 }
index 61802eae7b7ef09ac57c0d8dd4b14e8b46a50555..676ce9bd660a97cc0924ec1cc59605523610aea5 100644 (file)
@@ -22,7 +22,7 @@ pub(crate) async fn get_apub_site_http(
   let site: ApubSite = SiteView::read_local(context.pool()).await?.site.into();
 
   let apub = site.into_json(&context).await?;
-  Ok(create_apub_response(&apub))
+  create_apub_response(&apub)
 }
 
 #[tracing::instrument(skip_all)]
@@ -34,7 +34,7 @@ pub(crate) async fn get_apub_site_outbox(
     context.settings().get_protocol_and_hostname()
   );
   let outbox = EmptyOutbox::new(Url::parse(&outbox_id)?)?;
-  Ok(create_apub_response(&outbox))
+  create_apub_response(&outbox)
 }
 
 #[tracing::instrument(skip_all)]
index 41b437587284298bb2e713049088395028d66f44..6fb5e5155b08aa7cdab01f28100396f4fc5543da 100644 (file)
@@ -9,6 +9,8 @@ struct ApiError {
   error: String,
 }
 
+pub type LemmyResult<T> = Result<T, LemmyError>;
+
 pub struct LemmyError {
   pub message: Option<String>,
   pub inner: anyhow::Error,