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

69 lines
1.7 KiB
Rust
Raw Normal View History

2020-10-23 13:02:45 +00:00
use actix_web::{body::Body, web, HttpResponse};
use http::StatusCode;
use lemmy_api_common::blocking;
use lemmy_apub::APUB_JSON_CONTENT_TYPE;
use lemmy_db_queries::source::activity::Activity_;
use lemmy_db_schema::source::activity::Activity;
use lemmy_utils::{settings::structs::Settings, LemmyError};
2020-10-23 13:02:45 +00:00
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
pub mod comment;
pub mod community;
2021-03-10 22:33:55 +00:00
pub mod person;
pub mod post;
/// 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<Body>
where
T: Serialize,
{
HttpResponse::Ok()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
where
T: Serialize,
{
HttpResponse::Gone()
.content_type(APUB_JSON_CONTENT_TYPE)
.status(StatusCode::GONE)
.json(data)
}
2020-10-23 13:02:45 +00:00
#[derive(Deserialize)]
pub struct CommunityQuery {
type_: String,
id: String,
}
/// Return the ActivityPub json representation of a local community over HTTP.
pub(crate) async fn get_activity(
2020-10-23 13:02:45 +00:00
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let settings = Settings::get();
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();
2020-10-23 13:02:45 +00:00
let activity = blocking(context.pool(), move |conn| {
2021-07-05 16:07:26 +00:00
Activity::read_from_apub_id(conn, &activity_id)
2020-10-23 13:02:45 +00:00
})
.await??;
2020-11-10 16:11:08 +00:00
let sensitive = activity.sensitive.unwrap_or(true);
if !activity.local || sensitive {
Ok(HttpResponse::NotFound().finish())
} else {
Ok(create_apub_response(&activity.data))
}
2020-10-23 13:02:45 +00:00
}