From 178ad1b75dd41e23cb8ddf1b151842e951434eef Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Mar 2024 09:19:31 +0100 Subject: [PATCH] fix: dedicated methods for uris so i dont always wonder if its plural or not --- src/activitypub/activity.rs | 3 +-- src/activitypub/object.rs | 2 +- src/activitypub/user.rs | 4 ++-- src/server.rs | 13 +++++++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index 207e1faa..9de4bc11 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -5,8 +5,7 @@ use crate::{activitystream::Base, model::activity, server::Context}; pub async fn view(State(ctx) : State, Path(id): Path) -> Result, StatusCode> { - let uri = format!("http://localhost:3000/activities/{id}"); - match activity::Entity::find_by_id(uri).one(ctx.db()).await { + match activity::Entity::find_by_id(ctx.activity_uri(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 1fe23445..83adc293 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.uri("objects", id)).one(ctx.db()).await { + match object::Entity::find_by_id(ctx.object_uri(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 966c8063..ad9dff39 100644 --- a/src/activitypub/user.rs +++ b/src/activitypub/user.rs @@ -10,7 +10,7 @@ pub async fn list(State(_db) : State>) -> Result, Path(id): Path) -> Result, StatusCode> { - match user::Entity::find_by_id(ctx.uri("users", id)).one(ctx.db()).await { + match user::Entity::find_by_id(ctx.user_uri(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.uri("activities", before)) + match model::activity::Entity::find_by_id(ctx.activity_uri(before)) .one(ctx.db()).await { Ok(None) => return Err(StatusCode::NOT_FOUND), diff --git a/src/server.rs b/src/server.rs index 0af3d13e..e860c9e5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,6 +10,7 @@ struct ContextInner { db: DatabaseConnection, domain: String, } + impl Context { pub fn new(db: DatabaseConnection, mut domain: String) -> Self { if !domain.starts_with("http") { @@ -31,6 +32,18 @@ impl Context { } } + pub fn user_uri(&self, id: String) -> String { + self.uri("users", id) + } + + pub fn object_uri(&self, id: String) -> String { + self.uri("objects", id) + } + + pub fn activity_uri(&self, id: String) -> String { + self.uri("activities", id) + } + pub fn id(&self, id: String) -> String { if id.starts_with(&self.0.domain) { let mut out = id.replace(&self.0.domain, "");