1
0
Fork 0
forked from alemi/upub

feat: added all inboxes and outboxes

base POST to /outbox still returns NOT IMPLEMENTED tho
This commit is contained in:
əlemi 2024-04-12 20:01:47 +02:00
parent af8d11e75b
commit 0fa88e51e6
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 119 additions and 71 deletions

View file

@ -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::<EmbeddedActivity>()
.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::<Vec<serde_json::Value>>()
.map(|x| x.into())
.collect()
).ld_context()
))
}
pub async fn post(
State(ctx): State<Context>,
Json(activity): Json<serde_json::Value>
) -> 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())
}
}
}

View file

@ -24,10 +24,12 @@ impl ActivityPubRouter for Router<crate::server::Context> {
// 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

View file

@ -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<Context>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/outbox"), None).ld_context()))
}
pub async fn page(
State(ctx): State<Context>,
Query(page): Query<Pagination>,
AuthIdentity(auth): AuthIdentity,
) -> Result<JsonLD<serde_json::Value>, 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::<EmbeddedActivity>()
.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<Context>,
AuthIdentity(_auth): AuthIdentity,
Json(_activity): Json<serde_json::Value>,
) -> Result<CreationResult, UpubError> {
// TODO administrative actions may be carried out against this outbox?
Err(StatusCode::NOT_IMPLEMENTED.into())
}

View file

@ -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<Context>,
@ -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::<Vec<serde_json::Value>>()
.map(|x| x.into())
.collect()
).ld_context()
))
},
@ -74,48 +68,6 @@ pub async fn post(
Path(_id): Path<String>,
Json(activity): Json<serde_json::Value>
) -> 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
}

View file

@ -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()
))