2024-03-20 05:44:50 +01:00
|
|
|
pub mod user;
|
|
|
|
pub mod object;
|
|
|
|
pub mod activity;
|
2024-03-21 03:07:35 +01:00
|
|
|
pub mod jsonld;
|
|
|
|
pub use jsonld::JsonLD;
|
2024-03-20 05:44:50 +01:00
|
|
|
|
|
|
|
use axum::{extract::State, http::StatusCode, Json};
|
2024-03-20 08:56:35 +01:00
|
|
|
use sea_orm::{EntityTrait, IntoActiveModel};
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
use crate::{activitystream::{object::{activity::{Activity, ActivityType}, actor::{ActorMut, ActorType}, ObjectMut, ObjectType}, Base, BaseMut, BaseType, Node}, model, server::Context, url};
|
|
|
|
|
|
|
|
use self::jsonld::LD;
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-03-21 00:04:44 +01:00
|
|
|
pub const PUBLIC_TARGET : &str = "https://www.w3.org/ns/activitystreams#Public";
|
|
|
|
|
|
|
|
pub fn split_id(id: &str) -> (String, String) {
|
|
|
|
let clean = id
|
|
|
|
.replace("http://", "")
|
|
|
|
.replace("https://", "");
|
|
|
|
let mut splits = clean.split('/');
|
|
|
|
let first = splits.next().unwrap_or("");
|
|
|
|
let last = splits.last().unwrap_or(first);
|
|
|
|
(first.to_string(), last.to_string())
|
|
|
|
}
|
|
|
|
|
2024-03-21 01:11:20 +01:00
|
|
|
pub fn domain(domain: &str) -> String {
|
|
|
|
domain
|
|
|
|
.replace("http://", "")
|
|
|
|
.replace("https://", "")
|
|
|
|
.replace('/', "")
|
|
|
|
}
|
|
|
|
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-03-20 07:55:24 +01:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
// TODO i don't really like how pleroma/mastodon do it actually, maybe change this?
|
|
|
|
pub struct Page {
|
|
|
|
pub page: Option<bool>,
|
|
|
|
pub max_id: Option<String>,
|
2024-03-20 05:44:50 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 11:24:54 +01:00
|
|
|
pub async fn view(State(ctx): State<Context>) -> Result<Json<serde_json::Value>, StatusCode> {
|
2024-03-21 00:04:01 +01:00
|
|
|
Ok(Json(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 00:04:01 +01:00
|
|
|
.set_actor_type(Some(ActorType::Application))
|
|
|
|
.set_id(Some(&url!(ctx, "")))
|
|
|
|
.set_name(Some("μpub"))
|
|
|
|
.set_summary(Some("micro social network, federated"))
|
|
|
|
.set_inbox(Node::link(url!(ctx, "/inbox")))
|
|
|
|
.set_outbox(Node::link(url!(ctx, "/outbox")))
|
|
|
|
))
|
2024-03-20 11:24:54 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 03:07:35 +01:00
|
|
|
pub async fn inbox(State(ctx) : State<Context>, Json(object): Json<serde_json::Value>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-20 05:44:50 +01:00
|
|
|
match object.base_type() {
|
|
|
|
None => { Err(StatusCode::BAD_REQUEST) },
|
|
|
|
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => { todo!() },
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => { todo!() },
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => {
|
|
|
|
let Ok(activity_entity) = model::activity::Model::new(&object) else {
|
|
|
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
|
|
|
};
|
|
|
|
let Node::Object(obj) = object.object() else {
|
|
|
|
// TODO we could process non-embedded activities or arrays but im lazy rn
|
|
|
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
|
|
|
};
|
|
|
|
let Ok(obj_entity) = model::object::Model::new(&*obj) else {
|
|
|
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
|
|
|
};
|
|
|
|
model::object::Entity::insert(obj_entity.into_active_model())
|
2024-03-20 08:56:35 +01:00
|
|
|
.exec(ctx.db())
|
2024-03-20 05:44:50 +01:00
|
|
|
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
model::activity::Entity::insert(activity_entity.into_active_model())
|
2024-03-20 08:56:35 +01:00
|
|
|
.exec(ctx.db())
|
2024-03-20 05:44:50 +01:00
|
|
|
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
2024-03-21 03:07:35 +01:00
|
|
|
Ok(JsonLD(serde_json::Value::Null)) // TODO hmmmmmmmmmmm not the best value to return....
|
2024-03-20 05:44:50 +01:00
|
|
|
},
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(_x))) => { Err(StatusCode::NOT_IMPLEMENTED) },
|
|
|
|
Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:56:35 +01:00
|
|
|
pub async fn outbox(State(_db): State<Context>) -> Result<Json<serde_json::Value>, StatusCode> {
|
2024-03-20 05:44:50 +01:00
|
|
|
todo!()
|
|
|
|
}
|