From 3df01b5b0aed8f1ffc3e4cdeb596abb10043d089 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 30 Apr 2024 01:48:30 +0200 Subject: [PATCH] chore: helper to reconstruct ids --- src/routes/activitypub/activity.rs | 6 +----- src/routes/activitypub/context.rs | 9 +++------ src/routes/activitypub/object/replies.rs | 18 ++++++------------ src/server/context.rs | 9 ++++++++- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/routes/activitypub/activity.rs b/src/routes/activitypub/activity.rs index dbd853f..864f86a 100644 --- a/src/routes/activitypub/activity.rs +++ b/src/routes/activitypub/activity.rs @@ -10,11 +10,7 @@ pub async fn view( AuthIdentity(auth): AuthIdentity, Query(query): Query, ) -> crate::Result> { - let aid = if id.starts_with('+') { - format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) - } else { - ctx.aid(id.clone()) - }; + let aid = ctx.uri("activities", id); if auth.is_local() && query.fetch && !ctx.is_local(&aid) { ctx.fetch_activity(&aid).await?; } diff --git a/src/routes/activitypub/context.rs b/src/routes/activitypub/context.rs index 382ba11..85153c9 100644 --- a/src/routes/activitypub/context.rs +++ b/src/routes/activitypub/context.rs @@ -8,11 +8,8 @@ pub async fn get( Path(id): Path, AuthIdentity(auth): AuthIdentity, ) -> crate::Result> { - let context = if id.starts_with('+') { - format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) - } else { - url!(ctx, "/context/{id}") - }; + let local_context_id = url!(ctx, "/context/{id}"); + let context = ctx.uri("context", id); let count = model::addressing::Entity::find_addressed() .filter(auth.filter_condition()) @@ -20,7 +17,7 @@ pub async fn get( .count(ctx.db()) .await?; - crate::server::builders::collection(&url!(ctx, "/context/{id}"), Some(count)) + crate::server::builders::collection(&local_context_id, Some(count)) } pub async fn page( diff --git a/src/routes/activitypub/object/replies.rs b/src/routes/activitypub/object/replies.rs index 9530494..ee484bd 100644 --- a/src/routes/activitypub/object/replies.rs +++ b/src/routes/activitypub/object/replies.rs @@ -8,11 +8,8 @@ pub async fn get( Path(id): Path, AuthIdentity(auth): AuthIdentity, ) -> crate::Result> { - let oid = if id.starts_with('+') { - format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) - } else { - ctx.oid(id.clone()) - }; + let replies_id = url!(ctx, "/objects/{id}/replies"); + let oid = ctx.uri("objects", id); let count = model::addressing::Entity::find_addressed() .filter(auth.filter_condition()) @@ -20,7 +17,7 @@ pub async fn get( .count(ctx.db()) .await?; - crate::server::builders::collection(&url!(ctx, "/objects/{id}/replies"), Some(count)) + crate::server::builders::collection(&replies_id, Some(count)) } pub async fn page( @@ -29,14 +26,11 @@ pub async fn page( Query(page): Query, AuthIdentity(auth): AuthIdentity, ) -> crate::Result> { - let oid = if id.starts_with('+') { - format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) - } else { - ctx.oid(id.clone()) - }; + let page_id = url!(ctx, "/objects/{id}/replies/page"); + let oid = ctx.uri("objects", id); crate::server::builders::paginate( - url!(ctx, "/objects/{id}/replies/page"), + page_id, Condition::all() .add(auth.filter_condition()) .add(model::object::Column::InReplyTo.eq(oid)), diff --git a/src/server/context.rs b/src/server/context.rs index bf1b765..c50ee54 100644 --- a/src/server/context.rs +++ b/src/server/context.rs @@ -97,7 +97,14 @@ impl Context { } pub fn uri(&self, entity: &str, id: String) -> String { - if id.starts_with("http") { id } else { + if id.starts_with("http") { // ready-to-use id + id + } else if id.starts_with('+') { // compacted id + id + .replacen('+', "https://", 1) + .replace('@', "/") + .replace("//", "/@") // oops my method sucks!! TODO + } else { // bare local id format!("{}{}/{}/{}", self.0.protocol, self.0.domain, entity, id) } }