From 99abc490405647d5b1ef869fa4f33b4db975fa3d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 23 Oct 2020 15:02:45 +0200 Subject: [PATCH] Add /activities endpoint (ref #1220) --- lemmy_apub/src/activities/send/mod.rs | 2 +- lemmy_apub/src/http/mod.rs | 34 +++++++++++++++++-- .../down.sql | 1 + .../up.sql | 1 + src/routes/federation.rs | 4 ++- 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 migrations/2020-10-23-115011_activity_ap_id_column/down.sql create mode 100644 migrations/2020-10-23-115011_activity_ap_id_column/up.sql diff --git a/lemmy_apub/src/activities/send/mod.rs b/lemmy_apub/src/activities/send/mod.rs index fe898bd8f..537103b6a 100644 --- a/lemmy_apub/src/activities/send/mod.rs +++ b/lemmy_apub/src/activities/send/mod.rs @@ -15,7 +15,7 @@ where T: ToString, { let id = format!( - "{}/receive/{}/{}", + "{}/activities/{}/{}", Settings::get().get_protocol_and_hostname(), kind.to_string().to_lowercase(), Uuid::new_v4() diff --git a/lemmy_apub/src/http/mod.rs b/lemmy_apub/src/http/mod.rs index 89d0b33bf..9f6c766c2 100644 --- a/lemmy_apub/src/http/mod.rs +++ b/lemmy_apub/src/http/mod.rs @@ -1,6 +1,10 @@ use crate::APUB_JSON_CONTENT_TYPE; -use actix_web::{body::Body, HttpResponse}; -use serde::Serialize; +use actix_web::{body::Body, web, HttpResponse}; +use lemmy_db::activity::Activity; +use lemmy_structs::blocking; +use lemmy_utils::{settings::Settings, LemmyError}; +use lemmy_websocket::LemmyContext; +use serde::{Deserialize, Serialize}; pub mod comment; pub mod community; @@ -26,3 +30,29 @@ where .content_type(APUB_JSON_CONTENT_TYPE) .json(data) } + +#[derive(Deserialize)] +pub struct CommunityQuery { + type_: String, + id: String, +} + +/// Return the ActivityPub json representation of a local community over HTTP. +pub async fn get_activity( + info: web::Path, + context: web::Data, +) -> Result, LemmyError> { + let settings = Settings::get(); + let activity_id = format!( + "{}/activities/{}/{}", + settings.get_protocol_and_hostname(), + info.type_, + info.id + ); + let activity = blocking(context.pool(), move |conn| { + Activity::read_from_apub_id(&conn, &activity_id) + }) + .await??; + + Ok(create_apub_response(&activity.data)) +} diff --git a/migrations/2020-10-23-115011_activity_ap_id_column/down.sql b/migrations/2020-10-23-115011_activity_ap_id_column/down.sql new file mode 100644 index 000000000..7978df72e --- /dev/null +++ b/migrations/2020-10-23-115011_activity_ap_id_column/down.sql @@ -0,0 +1 @@ +ALTER TABLE activity DROP COLUMN ap_id; \ No newline at end of file diff --git a/migrations/2020-10-23-115011_activity_ap_id_column/up.sql b/migrations/2020-10-23-115011_activity_ap_id_column/up.sql new file mode 100644 index 000000000..ab22a4c44 --- /dev/null +++ b/migrations/2020-10-23-115011_activity_ap_id_column/up.sql @@ -0,0 +1 @@ +ALTER TABLE activity ADD COLUMN ap_id TEXT; \ No newline at end of file diff --git a/src/routes/federation.rs b/src/routes/federation.rs index 900631e51..7a5e5946d 100644 --- a/src/routes/federation.rs +++ b/src/routes/federation.rs @@ -4,6 +4,7 @@ use lemmy_apub::{ http::{ comment::get_apub_comment, community::{get_apub_community_followers, get_apub_community_http, get_apub_community_outbox}, + get_activity, post::get_apub_post, user::get_apub_user_http, }, @@ -36,7 +37,8 @@ pub fn config(cfg: &mut web::ServiceConfig) { ) .route("/u/{user_name}", web::get().to(get_apub_user_http)) .route("/post/{post_id}", web::get().to(get_apub_post)) - .route("/comment/{comment_id}", web::get().to(get_apub_comment)), + .route("/comment/{comment_id}", web::get().to(get_apub_comment)) + .route("/activities/{type_}/{id}", web::get().to(get_activity)), ) // Inboxes dont work with the header guard for some reason. .service(