mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-23 11:21:32 +00:00
Use pretty print for federation json responses (#2801)
This commit is contained in:
parent
9d26ac2c6f
commit
844cde9db3
7 changed files with 41 additions and 33 deletions
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()))?;
|
||||
|
||||
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<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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue