feat: add types enum and basic inbox match

This commit is contained in:
əlemi 2024-03-15 19:43:29 +01:00
parent ac3f07f804
commit e8a7e4e31c
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 106 additions and 8 deletions

View file

@ -5,3 +5,7 @@ pub use object::{Object, Link, ObjectOrLink};
pub mod activity; pub mod activity;
pub use activity::Activity; pub use activity::Activity;
pub mod types;
pub use types::Type;

View file

@ -27,7 +27,7 @@ pub trait Link {
pub trait Object { pub trait Object {
fn id(&self) -> Option<&str> { None } fn id(&self) -> Option<&str> { None }
fn object_type(&self) -> Option<&str> { None } fn object_type(&self) -> Option<super::Type> { None }
fn attachment (&self) -> Option<&str> { None } fn attachment (&self) -> Option<&str> { None }
fn attributed_to (&self) -> Option<&str> { None } fn attributed_to (&self) -> Option<&str> { None }
fn audience (&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() self.get("id")?.as_str()
} }
fn object_type(&self) -> Option<&str> { fn object_type(&self) -> Option<super::Type> {
self.get("type")?.as_str() todo!()
} }
// ... // ...

View file

@ -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,
}

View file

@ -1,15 +1,21 @@
pub mod model; pub mod model;
pub mod activitystream; pub mod activitystream;
pub mod activitypub;
pub mod server;
pub mod storage;
use axum::{ use activitystream::{types::{ActivityType, ObjectType}, Object, Type};
routing::get, use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::{get, post}, Json, Router};
Router,
};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// build our application with a single route // 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 // run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
@ -18,3 +24,23 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
} }
async fn inbox(State(ctx) : State<()>, Json(object): Json<serde_json::Value>) -> Result<Json<serde_json::Value>, 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<String>) -> Result<Json<serde_json::Value>, StatusCode> {
todo!()
}
async fn object(State(ctx) : State<()>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
todo!()
}