From e8a7e4e31c206de0c133dd7ab177309433c23725 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 15 Mar 2024 19:43:29 +0100 Subject: [PATCH] feat: add types enum and basic inbox match --- src/activitystream/mod.rs | 4 +++ src/activitystream/object.rs | 6 ++-- src/activitystream/types.rs | 68 ++++++++++++++++++++++++++++++++++++ src/main.rs | 36 ++++++++++++++++--- 4 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 src/activitystream/types.rs diff --git a/src/activitystream/mod.rs b/src/activitystream/mod.rs index 3837ac9e..8c9161b1 100644 --- a/src/activitystream/mod.rs +++ b/src/activitystream/mod.rs @@ -5,3 +5,7 @@ pub use object::{Object, Link, ObjectOrLink}; pub mod activity; pub use activity::Activity; + + +pub mod types; +pub use types::Type; diff --git a/src/activitystream/object.rs b/src/activitystream/object.rs index 72c7fad8..b3941c02 100644 --- a/src/activitystream/object.rs +++ b/src/activitystream/object.rs @@ -27,7 +27,7 @@ pub trait Link { pub trait Object { fn id(&self) -> Option<&str> { None } - fn object_type(&self) -> Option<&str> { None } + fn object_type(&self) -> Option { None } fn attachment (&self) -> Option<&str> { None } fn attributed_to (&self) -> Option<&str> { None } fn audience (&self) -> Option<&str> { None } @@ -66,8 +66,8 @@ impl Object for serde_json::Value { self.get("id")?.as_str() } - fn object_type(&self) -> Option<&str> { - self.get("type")?.as_str() + fn object_type(&self) -> Option { + todo!() } // ... diff --git a/src/activitystream/types.rs b/src/activitystream/types.rs new file mode 100644 index 00000000..ebcab58c --- /dev/null +++ b/src/activitystream/types.rs @@ -0,0 +1,68 @@ +pub enum Type { + Object, + ObjectType(ObjectType), + Link, + Mention, // TODO what about this??? + Activity, + IntransitiveActivity, + ActivityType(ActivityType), + Collection, + OrderedCollection, + CollectionPage, + OrderedCollectionPage, + ActorType(ActorType), +} + +pub enum ActivityType { + Accept, + Add, + Announce, + Arrive, + Block, + Create, + Delete, + Dislike, + Flag, + Follow, + Ignore, + Invite, + Join, + Leave, + Like, + Listen, + Move, + Offer, + Question, + Reject, + Read, + Remove, + TentativeReject, + TentativeAccept, + Travel, + Undo, + Update, + View, +} + +pub enum ActorType { + Application, + Group, + Organization, + Person, + Service, +} + +pub enum ObjectType { + Article, + Audio, + Document, + Event, + Image, + Note, + Page, + Place, + Profile, + Relationship, + Tombstone, + Video, +} diff --git a/src/main.rs b/src/main.rs index 7dbb9b40..9ff0730a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,21 @@ pub mod model; pub mod activitystream; +pub mod activitypub; +pub mod server; +pub mod storage; -use axum::{ - routing::get, - Router, -}; +use activitystream::{types::{ActivityType, ObjectType}, Object, Type}; +use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::{get, post}, Json, Router}; #[tokio::main] async fn main() { // build our application with a single route - let app = Router::new().route("/", get(|| async { "Hello, World!" })); + let app = Router::new() + .with_state(()) + .route("/inbox", post(inbox)) + .route("/outbox", get(|| async { todo!() })) + .route("/users/:id", get(user)) + .route("/objects/:id", get(object)); // run our app with hyper, listening globally on port 3000 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); @@ -18,3 +24,23 @@ async fn main() { .await .unwrap(); } + +async fn inbox(State(ctx) : State<()>, Json(object): Json) -> Result, StatusCode> { + match object.object_type() { + None => { Err(StatusCode::BAD_REQUEST) }, + Some(Type::Activity) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }, + Some(Type::ActivityType(ActivityType::Follow)) => { todo!() }, + Some(Type::ActivityType(ActivityType::Create)) => { todo!() }, + Some(Type::ActivityType(ActivityType::Like)) => { todo!() }, + Some(Type::ActivityType(x)) => { Err(StatusCode::NOT_IMPLEMENTED) }, + Some(x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) } + } +} + +async fn user(State(ctx) : State<()>, Path(id): Path) -> Result, StatusCode> { + todo!() +} + +async fn object(State(ctx) : State<()>, Path(id): Path) -> Result, StatusCode> { + todo!() +}