From 0fa88e51e68902868877f358357797b3ed1f5522 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 12 Apr 2024 20:01:47 +0200 Subject: [PATCH] feat: added all inboxes and outboxes base POST to /outbox still returns NOT IMPLEMENTED tho --- src/routes/activitypub/inbox.rs | 63 ++++++++++++++++++++++++--- src/routes/activitypub/mod.rs | 8 ++-- src/routes/activitypub/outbox.rs | 50 +++++++++++++++++++++ src/routes/activitypub/user/inbox.rs | 58 +++--------------------- src/routes/activitypub/user/outbox.rs | 11 +---- 5 files changed, 119 insertions(+), 71 deletions(-) diff --git a/src/routes/activitypub/inbox.rs b/src/routes/activitypub/inbox.rs index 4cb77b2..d13d9fe 100644 --- a/src/routes/activitypub/inbox.rs +++ b/src/routes/activitypub/inbox.rs @@ -1,9 +1,10 @@ -use axum::{extract::{Query, State}, http::StatusCode}; +use apb::{server::Inbox, ActivityType, Base, BaseType, ObjectType}; +use axum::{extract::{Query, State}, http::StatusCode, Json}; use sea_orm::{Order, QueryFilter, QueryOrder, QuerySelect}; -use crate::{server::auth::AuthIdentity, errors::UpubError, model, server::Context, url}; +use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, server::{auth::AuthIdentity, Context}, url}; -use super::{activity::ap_activity, jsonld::LD, JsonLD, Pagination}; +use super::{jsonld::LD, JsonLD, Pagination}; pub async fn get( @@ -22,9 +23,9 @@ pub async fn page( let activities = model::addressing::Entity::find_activities() .filter(auth.filter_condition()) .order_by(model::addressing::Column::Published, Order::Asc) - .find_also_related(model::activity::Entity) .limit(limit) .offset(offset) + .into_model::() .all(ctx.db()) .await?; Ok(JsonLD( @@ -33,8 +34,58 @@ pub async fn page( offset, limit, activities .into_iter() - .filter_map(|(_, a)| Some(ap_activity(a?))) - .collect::>() + .map(|x| x.into()) + .collect() ).ld_context() )) } + +pub async fn post( + State(ctx): State, + Json(activity): Json +) -> Result<(), UpubError> { + match activity.base_type() { + None => { Err(StatusCode::BAD_REQUEST.into()) }, + + Some(BaseType::Link(_x)) => { + tracing::warn!("skipping remote activity: {}", serde_json::to_string_pretty(&activity).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // we could but not yet + }, + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => { + tracing::warn!("skipping unprocessable base activity: {}", serde_json::to_string_pretty(&activity).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // won't ingest useless stuff + }, + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Delete))) => + Ok(ctx.delete(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => + Ok(ctx.follow(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Accept(_)))) => + Ok(ctx.accept(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Reject(_)))) => + Ok(ctx.reject(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => + Ok(ctx.like(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => + Ok(ctx.create(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(ActivityType::Update))) => + Ok(ctx.update(activity).await?), + + Some(BaseType::Object(ObjectType::Activity(_x))) => { + tracing::info!("received unimplemented activity on inbox: {}", serde_json::to_string_pretty(&activity).unwrap()); + Err(StatusCode::NOT_IMPLEMENTED.into()) + }, + + Some(_x) => { + tracing::warn!("ignoring non-activity object in inbox: {}", serde_json::to_string_pretty(&activity).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY.into()) + } + } +} diff --git a/src/routes/activitypub/mod.rs b/src/routes/activitypub/mod.rs index 30565f3..181b985 100644 --- a/src/routes/activitypub/mod.rs +++ b/src/routes/activitypub/mod.rs @@ -24,10 +24,12 @@ impl ActivityPubRouter for Router { // core server inbox/outbox, maybe for feeds? TODO do we need these? .route("/", get(ap::application::view)) // TODO shared inboxes and instance stream will come later, just use users *boxes for now + .route("/inbox", post(ap::inbox::post)) .route("/inbox", get(ap::inbox::get)) - // .route("/inbox", post(ap::inbox::post)) - // .route("/outbox", get(ap::outbox::get)) - // .route("/outbox", get(ap::outbox::post)) + .route("/inbox/page", get(ap::inbox::page)) + .route("/outbox", post(ap::outbox::post)) + .route("/outbox", get(ap::outbox::get)) + .route("/outbox/page", get(ap::outbox::page)) // AUTH routes .route("/auth", post(ap::auth::login)) // .well-known and discovery diff --git a/src/routes/activitypub/outbox.rs b/src/routes/activitypub/outbox.rs index e69de29..9823ff6 100644 --- a/src/routes/activitypub/outbox.rs +++ b/src/routes/activitypub/outbox.rs @@ -0,0 +1,50 @@ +use axum::{extract::{Query, State}, http::StatusCode, Json}; +use sea_orm::{Order, QueryFilter, QueryOrder, QuerySelect}; + +use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, routes::activitypub::{jsonld::LD, CreationResult, JsonLD, Pagination}, server::{auth::AuthIdentity, Context}, url}; + +pub async fn get(State(ctx): State) -> Result, StatusCode> { + Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/outbox"), None).ld_context())) +} + +pub async fn page( + State(ctx): State, + Query(page): Query, + AuthIdentity(auth): AuthIdentity, +) -> Result, StatusCode> { + let limit = page.batch.unwrap_or(20).min(50); + let offset = page.offset.unwrap_or(0); + + match model::addressing::Entity::find_activities() + .filter(auth.filter_condition()) + // TODO also limit to only local activities + .order_by(model::addressing::Column::Published, Order::Desc) + .limit(limit) + .offset(offset) + .into_model::() + .all(ctx.db()).await + { + Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR), + Ok(items) => { + Ok(JsonLD( + ctx.ap_collection_page( + &url!(ctx, "/outbox/page"), + offset, limit, + items + .into_iter() + .map(|x| x.into()) + .collect() + ).ld_context() + )) + }, + } +} + +pub async fn post( + State(_ctx): State, + AuthIdentity(_auth): AuthIdentity, + Json(_activity): Json, +) -> Result { + // TODO administrative actions may be carried out against this outbox? + Err(StatusCode::NOT_IMPLEMENTED.into()) +} diff --git a/src/routes/activitypub/user/inbox.rs b/src/routes/activitypub/user/inbox.rs index 26718bf..52b9fc0 100644 --- a/src/routes/activitypub/user/inbox.rs +++ b/src/routes/activitypub/user/inbox.rs @@ -1,8 +1,7 @@ use axum::{extract::{Path, Query, State}, http::StatusCode, Json}; -use apb::{server::Inbox, ActivityMut, ActivityType, Base, BaseType, ObjectType}; use sea_orm::{ColumnTrait, Condition, QueryFilter, QuerySelect}; -use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, routes::activitypub::{activity::ap_activity, jsonld::LD, object::ap_object, JsonLD, Pagination}, server::{auth::{AuthIdentity, Identity}, Context}, url}; +use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, routes::activitypub::{jsonld::LD, JsonLD, Pagination}, server::{auth::{AuthIdentity, Identity}, Context}, url}; pub async fn get( State(ctx): State, @@ -48,13 +47,8 @@ pub async fn page( offset, limit, activities .into_iter() - .map(|EmbeddedActivity { activity, object }| match object { - None => ap_activity(activity), - Some(x) => - ap_activity(activity) - .set_object(apb::Node::object(ap_object(x))), - }) - .collect::>() + .map(|x| x.into()) + .collect() ).ld_context() )) }, @@ -74,48 +68,6 @@ pub async fn post( Path(_id): Path, Json(activity): Json ) -> Result<(), UpubError> { - match activity.base_type() { - None => { Err(StatusCode::BAD_REQUEST.into()) }, - - Some(BaseType::Link(_x)) => { - tracing::warn!("skipping remote activity: {}", serde_json::to_string_pretty(&activity).unwrap()); - Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // we could but not yet - }, - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => { - tracing::warn!("skipping unprocessable base activity: {}", serde_json::to_string_pretty(&activity).unwrap()); - Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // won't ingest useless stuff - }, - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Delete))) => - Ok(ctx.delete(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => - Ok(ctx.follow(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Accept(_)))) => - Ok(ctx.accept(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Reject(_)))) => - Ok(ctx.reject(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => - Ok(ctx.like(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => - Ok(ctx.create(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(ActivityType::Update))) => - Ok(ctx.update(activity).await?), - - Some(BaseType::Object(ObjectType::Activity(_x))) => { - tracing::info!("received unimplemented activity on inbox: {}", serde_json::to_string_pretty(&activity).unwrap()); - Err(StatusCode::NOT_IMPLEMENTED.into()) - }, - - Some(_x) => { - tracing::warn!("ignoring non-activity object in inbox: {}", serde_json::to_string_pretty(&activity).unwrap()); - Err(StatusCode::UNPROCESSABLE_ENTITY.into()) - } - } + // POSTing to user inboxes is effectively the same as POSTing to the main inbox + super::super::inbox::post(State(ctx), Json(activity)).await } diff --git a/src/routes/activitypub/user/outbox.rs b/src/routes/activitypub/user/outbox.rs index c6c7aa8..396553c 100644 --- a/src/routes/activitypub/user/outbox.rs +++ b/src/routes/activitypub/user/outbox.rs @@ -1,7 +1,7 @@ use axum::{extract::{Path, Query, State}, http::StatusCode, Json}; use sea_orm::{ColumnTrait, Condition, Order, QueryFilter, QueryOrder, QuerySelect}; -use apb::{server::Outbox, AcceptType, ActivityMut, ActivityType, Base, BaseType, Node, ObjectType, RejectType}; +use apb::{server::Outbox, AcceptType, ActivityType, Base, BaseType, ObjectType, RejectType}; use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, routes::activitypub::{jsonld::LD, CreationResult, JsonLD, Pagination}, server::{auth::{AuthIdentity, Identity}, Context}, url}; pub async fn get( @@ -40,14 +40,7 @@ pub async fn page( offset, limit, items .into_iter() - .map(|EmbeddedActivity { activity, object }| { - let oid = activity.object.clone(); - super::super::activity::ap_activity(activity) - .set_object(match object { - Some(o) => Node::object(super::super::object::ap_object(o)), - None => Node::maybe_link(oid), - }) - }) + .map(|x| x.into()) .collect() ).ld_context() ))