diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index 9de4bc11..267dbe20 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -5,7 +5,7 @@ use crate::{activitystream::Base, model::activity, server::Context}; pub async fn view(State(ctx) : State, Path(id): Path) -> Result, StatusCode> { - match activity::Entity::find_by_id(ctx.activity_uri(id)).one(ctx.db()).await { + match activity::Entity::find_by_id(ctx.aid(id)).one(ctx.db()).await { Ok(Some(activity)) => Ok(Json(activity.underlying_json_object())), Ok(None) => Err(StatusCode::NOT_FOUND), Err(e) => { diff --git a/src/activitypub/object.rs b/src/activitypub/object.rs index 83adc293..11f8f21c 100644 --- a/src/activitypub/object.rs +++ b/src/activitypub/object.rs @@ -5,7 +5,7 @@ use crate::{activitystream::Base, model::object, server::Context}; pub async fn view(State(ctx) : State, Path(id): Path) -> Result, StatusCode> { - match object::Entity::find_by_id(ctx.object_uri(id)).one(ctx.db()).await { + match object::Entity::find_by_id(ctx.oid(id)).one(ctx.db()).await { Ok(Some(object)) => Ok(Json(object.underlying_json_object())), Ok(None) => Err(StatusCode::NOT_FOUND), Err(e) => { diff --git a/src/activitypub/user.rs b/src/activitypub/user.rs index ad9dff39..e582cb54 100644 --- a/src/activitypub/user.rs +++ b/src/activitypub/user.rs @@ -3,14 +3,14 @@ use std::sync::Arc; use axum::{extract::{Path, Query, State}, http::StatusCode, Json}; use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, Order, QueryFilter, QueryOrder, QuerySelect}; -use crate::{activitystream::{self, object::{activity::{Activity, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, ObjectType}, Base, BaseMut, BaseType, Node}, model::{self, activity, object, user}, server::Context}; +use crate::{activitystream::{self, object::{activity::{Activity, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, ObjectType}, Base, BaseMut, BaseType, Node}, model::{self, activity, object, user}, server::Context, url}; pub async fn list(State(_db) : State>) -> Result, StatusCode> { todo!() } pub async fn view(State(ctx) : State, Path(id): Path) -> Result, StatusCode> { - match user::Entity::find_by_id(ctx.user_uri(id)).one(ctx.db()).await { + match user::Entity::find_by_id(ctx.uid(id)).one(ctx.db()).await { Ok(Some(user)) => Ok(Json(user.underlying_json_object())), Ok(None) => Err(StatusCode::NOT_FOUND), Err(e) => { @@ -29,7 +29,7 @@ pub async fn outbox( // find requested recent post, to filter based on its date (use now() as fallback) let before = if let Some(before) = page.max_id { - match model::activity::Entity::find_by_id(ctx.activity_uri(before)) + match model::activity::Entity::find_by_id(ctx.aid(before)) .one(ctx.db()).await { Ok(None) => return Err(StatusCode::NOT_FOUND), @@ -58,8 +58,8 @@ pub async fn outbox( obj // TODO set id, calculate uri from given args .set_collection_type(Some(CollectionType::OrderedCollectionPage)) - .set_part_of(Node::link(&format!("http://localhost:3000/users/{id}/outbox"))) - .set_next(Node::link(&format!("http://localhost:3000/users/{id}/outbox?page=true&max_id={next}"))) + .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)); Ok(Json(obj)) }, @@ -68,9 +68,9 @@ pub async fn outbox( } else { let mut obj = crate::activitystream::object(); obj - .set_id(Some(&format!("http://localhost:3000/users/{id}/outbox"))) + .set_id(Some(&url!(ctx, "/users/{id}/outbox"))) .set_collection_type(Some(CollectionType::OrderedCollection)) - .set_first(Node::link(&format!("http://localhost:3000/users/{id}/outbox?page=true"))); + .set_first(Node::link(&url!(ctx, "/users/{id}/outbox?page=true"))); Ok(Json(obj.underlying_json_object())) } } diff --git a/src/server.rs b/src/server.rs index e860c9e5..be7c2473 100644 --- a/src/server.rs +++ b/src/server.rs @@ -11,6 +11,13 @@ struct ContextInner { domain: String, } +#[macro_export] +macro_rules! url { + ($ctx:expr, $($args: tt)*) => { + format!("{}{}", $ctx.base(), format!($($args)*)) + }; +} + impl Context { pub fn new(db: DatabaseConnection, mut domain: String) -> Self { if !domain.starts_with("http") { @@ -26,24 +33,34 @@ impl Context { &self.0.db } + pub fn base(&self) -> &str { + &self.0.domain + } + pub fn uri(&self, entity: &str, id: String) -> String { if id.starts_with("http") { id } else { format!("{}/{}/{}", self.0.domain, entity, id) } } - pub fn user_uri(&self, id: String) -> String { + // TODO maybe redo these with enums? idk + + /// get full user id uri + pub fn uid(&self, id: String) -> String { self.uri("users", id) } - pub fn object_uri(&self, id: String) -> String { + /// get full object id uri + pub fn oid(&self, id: String) -> String { self.uri("objects", id) } - pub fn activity_uri(&self, id: String) -> String { + /// get full activity id uri + pub fn aid(&self, id: String) -> String { self.uri("activities", id) } + /// get bare uri, usually an uuid but unspecified pub fn id(&self, id: String) -> String { if id.starts_with(&self.0.domain) { let mut out = id.replace(&self.0.domain, "");