diff --git a/src/routes/activitypub/activity.rs b/src/routes/activitypub/activity.rs index dbd853f3..864f86a3 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 382ba113..85153c96 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 95304943..ee484bd4 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 bf1b7658..c50ee54d 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) } }