lemmy/crates/apub/src/http/mod.rs

102 lines
2.7 KiB
Rust
Raw Normal View History

use crate::{
activity_lists::SharedInboxActivities,
2021-11-05 00:24:10 +00:00
fetcher::user_or_community::UserOrCommunity,
protocol::objects::tombstone::Tombstone,
CONTEXT,
};
use activitypub_federation::{
actix_web::inbox::receive_activity,
config::Data,
protocol::context::WithContext,
FEDERATION_CONTENT_TYPE,
};
use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
use http::{header::LOCATION, StatusCode};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::DbUrl, source::activity::SentActivity};
use lemmy_utils::error::{LemmyError, LemmyResult};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use url::Url;
mod comment;
mod community;
mod person;
mod post;
pub mod routes;
pub mod site;
pub async fn shared_inbox(
request: HttpRequest,
body: Bytes,
data: Data<LemmyContext>,
) -> 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.
///
/// 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,
{
let json = serde_json::to_string_pretty(&WithContext::new(data, CONTEXT.clone()))?;
Ok(
HttpResponse::Ok()
.content_type(FEDERATION_CONTENT_TYPE)
.body(json),
)
}
fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> LemmyResult<HttpResponse> {
let tombstone = Tombstone::new(id.into());
let json = serde_json::to_string_pretty(&WithContext::new(tombstone, CONTEXT.deref().clone()))?;
Ok(
HttpResponse::Gone()
.content_type(FEDERATION_CONTENT_TYPE)
.status(StatusCode::GONE)
.body(json),
)
}
2020-10-23 13:02:45 +00:00
fn redirect_remote_object(url: &DbUrl) -> HttpResponse {
let mut res = HttpResponse::PermanentRedirect();
res.insert_header((LOCATION, url.as_str()));
res.finish()
}
2020-10-23 13:02:45 +00:00
#[derive(Deserialize)]
pub struct ActivityQuery {
2020-10-23 13:02:45 +00:00
type_: String,
id: String,
}
/// Return the ActivityPub json representation of a local activity over HTTP.
#[tracing::instrument(skip_all)]
pub(crate) async fn get_activity(
info: web::Path<ActivityQuery>,
2020-10-23 13:02:45 +00:00
context: web::Data<LemmyContext>,
2021-12-14 13:30:37 +00:00
) -> Result<HttpResponse, LemmyError> {
let settings = context.settings();
let activity_id = Url::parse(&format!(
2020-10-23 13:02:45 +00:00
"{}/activities/{}/{}",
settings.get_protocol_and_hostname(),
info.type_,
info.id
))?
.into();
let activity = SentActivity::read_from_apub_id(&mut context.pool(), &activity_id).await?;
2020-10-23 13:02:45 +00:00
let sensitive = activity.sensitive;
if sensitive {
Ok(HttpResponse::Forbidden().finish())
} else {
create_apub_response(&activity.data)
}
2020-10-23 13:02:45 +00:00
}