feat: work on authed outbox/inbox

This commit is contained in:
əlemi 2024-03-25 02:00:57 +01:00
parent 384d5147ed
commit acd4fa0bd4
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 110 additions and 48 deletions

View file

@ -3,7 +3,7 @@ use sea_orm::{sea_query::Expr, ColumnTrait, EntityTrait, IntoActiveModel, QueryF
use crate::{activitypub::JsonLD, activitystream::{object::{activity::{Activity, ActivityType}, Addressed, ObjectType}, Base, BaseType, Node}, errors::LoggableError, model::{self, activity, addressing, object}, server::Context}; use crate::{activitypub::JsonLD, activitystream::{object::{activity::{Activity, ActivityType}, Addressed, ObjectType}, Base, BaseType, Node}, errors::LoggableError, model::{self, activity, addressing, object}, server::Context};
pub async fn inbox( pub async fn post(
State(ctx): State<Context>, State(ctx): State<Context>,
Path(_id): Path<String>, Path(_id): Path<String>,
Json(object): Json<serde_json::Value> Json(object): Json<serde_json::Value>
@ -123,3 +123,7 @@ pub async fn inbox(
} }
} }
} }
pub async fn get() -> StatusCode {
StatusCode::NOT_IMPLEMENTED
}

View file

@ -1,8 +1,6 @@
mod inbox; pub mod inbox;
pub use inbox::inbox;
mod outbox; pub mod outbox;
pub use outbox::outbox;
mod following; mod following;
pub use following::follow___; pub use following::follow___;

View file

@ -1,16 +1,49 @@
use axum::{extract::{Path, Query, State}, http::StatusCode}; use axum::{extract::{Path, Query, State}, http::StatusCode, Json};
use sea_orm::{EntityTrait, Order, QueryOrder, QuerySelect}; use sea_orm::{ColumnTrait, Condition, EntityTrait, Order, QueryFilter, QueryOrder, QuerySelect};
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination}, activitystream::{object::{activity::ActivityMut, collection::{page::CollectionPageMut, CollectionMut, CollectionType}}, BaseMut, Node}, model::{activity, object}, server::Context, url}; use crate::{activitypub::{jsonld::LD, JsonLD, Pagination, PUBLIC_TARGET}, activitystream::{object::{activity::{accept::AcceptType, reject::RejectType, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}}, Base, BaseMut, BaseType, Node, ObjectType}, auth::{AuthIdentity, Identity}, model::{self, activity, object}, server::Context, url};
pub async fn outbox( pub async fn get(
State(ctx): State<Context>,
Path(id): Path<String>,
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
Ok(JsonLD(
serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/outbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/outbox/page")))
.ld_context()
))
}
pub async fn page(
State(ctx): State<Context>, State(ctx): State<Context>,
Path(id): Path<String>, Path(id): Path<String>,
Query(page): Query<Pagination>, Query(page): Query<Pagination>,
AuthIdentity(auth): AuthIdentity,
) -> Result<JsonLD<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
let limit = page.batch.unwrap_or(20).min(50); let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0); let offset = page.offset.unwrap_or(0);
if let Some(true) = page.page {
let mut conditions = Condition::any()
.add(model::addressing::Column::Actor.eq(PUBLIC_TARGET));
if let Identity::User(x) = auth {
conditions = conditions.add(model::addressing::Column::Actor.eq(x));
}
if let Identity::Server(x) = auth {
conditions = conditions.add(model::addressing::Column::Server.eq(x));
}
let x = model::addressing::Entity::find()
.filter(conditions)
.inner_join(model::activity::Entity)
.left_join(model::object::Entity)
.all(ctx.db())
.await;
match activity::Entity::find() match activity::Entity::find()
.find_also_related(object::Entity) .find_also_related(object::Entity)
.order_by(activity::Column::Published, Order::Desc) .order_by(activity::Column::Published, Order::Desc)
@ -20,34 +53,59 @@ pub async fn outbox(
{ {
Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR), Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR),
Ok(items) => { Ok(items) => {
let next = ctx.id(items.last().map(|(a, _o)| a.id.as_str()).unwrap_or("").to_string()); Ok(JsonLD(
let items = items serde_json::Value::new_object()
// TODO set id, calculate uri from given args
.set_id(Some(&url!(ctx, "/users/{id}/outbox/page?offset={offset}")))
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
.set_part_of(Node::link(url!(ctx, "/users/{id}/outbox")))
.set_next(Node::link(url!(ctx, "/users/{id}/outbox/page?offset={}", limit+offset)))
.set_ordered_items(Node::array(
items
.into_iter() .into_iter()
.map(|(a, o)| .map(|(a, o)|
super::super::activity::ap_activity(a) super::super::activity::ap_activity(a)
.set_object(Node::maybe_object(o.map(super::super::object::ap_object))) .set_object(Node::maybe_object(o.map(super::super::object::ap_object)))
) )
.collect(); .collect()
Ok(JsonLD( ))
serde_json::Value::new_object()
// TODO set id, calculate uri from given args
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
.set_part_of(Node::link(url!(ctx, "/users/{id}/outbox")))
.set_next(Node::link(url!(ctx, "/users/{id}/outbox?page=true&max_id={next}")))
.set_ordered_items(Node::array(items))
.ld_context() .ld_context()
)) ))
}, },
} }
}
pub async fn post(
State(ctx): State<Context>,
Path(id): Path<String>,
Json(activity): Json<serde_json::Value>,
AuthIdentity(auth): AuthIdentity,
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match auth {
Identity::Anonymous => Err(StatusCode::UNAUTHORIZED),
Identity::Server(_) => Err(StatusCode::NOT_IMPLEMENTED),
Identity::User(uid) => if ctx.uid(id) == uid {
match activity.base_type() {
None => Err(StatusCode::BAD_REQUEST),
Some(BaseType::Link(_)) => Err(StatusCode::UNPROCESSABLE_ENTITY),
// Some(BaseType::Object(ObjectType::Note)) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Undo))) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Accept(AcceptType::Accept)))) => {
// },
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Reject(RejectType::Reject)))) => {
// },
Some(_) => Err(StatusCode::NOT_IMPLEMENTED),
}
} else { } else {
Ok(JsonLD( Err(StatusCode::FORBIDDEN)
serde_json::Value::new_object() }
.set_id(Some(&url!(ctx, "/users/{id}/outbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/outbox?page=true")))
.ld_context()
))
} }
} }

View file

@ -21,8 +21,10 @@ pub async fn serve(db: DatabaseConnection, domain: String) {
.route("/nodeinfo/:version", get(ap::well_known::nodeinfo)) .route("/nodeinfo/:version", get(ap::well_known::nodeinfo))
// actor routes // actor routes
.route("/users/:id", get(ap::user::view)) .route("/users/:id", get(ap::user::view))
.route("/users/:id/inbox", post(ap::user::inbox)) .route("/users/:id/inbox", get(ap::user::inbox::get))
.route("/users/:id/outbox", get(ap::user::outbox)) .route("/users/:id/inbox", post(ap::user::inbox::post))
.route("/users/:id/outbox", get(ap::user::outbox::get))
.route("/users/:id/outbox/page", get(ap::user::outbox::page))
.route("/users/:id/followers", get(ap::user::follow___::<false>)) .route("/users/:id/followers", get(ap::user::follow___::<false>))
.route("/users/:id/following", get(ap::user::follow___::<true>)) .route("/users/:id/following", get(ap::user::follow___::<true>))
// specific object routes // specific object routes