Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pretty print for federation json responses #2801

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/apub/src/http/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
12 changes: 6 additions & 6 deletions crates/apub/src/http/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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
Expand All @@ -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)]
Expand 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.
Expand All @@ -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)
}
40 changes: 23 additions & 17 deletions crates/apub/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
6 changes: 3 additions & 3 deletions crates/apub/src/http/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions crates/apub/src/http/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
4 changes: 2 additions & 2 deletions crates/apub/src/http/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand 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)]
Expand Down
2 changes: 2 additions & 0 deletions crates/utils/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct ApiError {
error: String,
}

pub type LemmyResult<T> = Result<T, LemmyError>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorter to write, we can use this all across the code.


pub struct LemmyError {
pub message: Option<String>,
pub inner: anyhow::Error,
Expand Down