diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index c02bd0545..9e78da2ab 100644 --- a/crates/apub/src/http/comment.rs +++ b/crates/apub/src/http/comment.rs @@ -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()) } } diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 4ae50bb05..04ac8e3fd 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -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 { 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) } diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 72819ccbe..7a4a362fa 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -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, -) -> Result { +) -> LemmyResult { receive_activity::(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(data: &T) -> HttpResponse +/// +/// actix-web doesn't allow pretty-print for json so we need to do this manually. +fn create_apub_response(data: &T) -> LemmyResult 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()))?; + + Ok( + HttpResponse::Ok() + .content_type(FEDERATION_CONTENT_TYPE) + .content_type("application/json") + .body(json), + ) } -fn create_json_apub_response(data: serde_json::Value) -> HttpResponse { - HttpResponse::Ok() - .content_type(FEDERATION_CONTENT_TYPE) - .json(data) -} - -fn create_apub_tombstone_response>(id: T) -> HttpResponse { +fn create_apub_tombstone_response>(id: T) -> LemmyResult { 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) } } diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index 79696660c..453905eee 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -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) } diff --git a/crates/apub/src/http/post.rs b/crates/apub/src/http/post.rs index ed1815c07..a2e600268 100644 --- a/crates/apub/src/http/post.rs +++ b/crates/apub/src/http/post.rs @@ -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()) } } diff --git a/crates/apub/src/http/site.rs b/crates/apub/src/http/site.rs index 61802eae7..676ce9bd6 100644 --- a/crates/apub/src/http/site.rs +++ b/crates/apub/src/http/site.rs @@ -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)] diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 41b437587..6fb5e5155 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -9,6 +9,8 @@ struct ApiError { error: String, } +pub type LemmyResult = Result; + pub struct LemmyError { pub message: Option, pub inner: anyhow::Error,