}
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())
}
}
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())
}
}
) -> 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
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)]
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.
return Err(LemmyError::from_message("deleted"));
}
let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
- Ok(create_apub_response(&featured))
+ create_apub_response(&featured)
}
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;
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 {
} else if sensitive {
Ok(HttpResponse::Forbidden().finish())
} else {
- Ok(create_json_apub_response(activity.data))
+ create_apub_response(&activity.data)
}
}
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())
}
}
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)
}
}
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())
}
}
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)]
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)]
error: String,
}
+pub type LemmyResult<T> = Result<T, LemmyError>;
+
pub struct LemmyError {
pub message: Option<String>,
pub inner: anyhow::Error,