From ee26596568468312a7597dea2c2f4b315f0598b8 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 15 Apr 2024 21:36:31 +0200 Subject: [PATCH] feat: allow to resolve local views of remote things basically any id prefixed with + will be: * prefixed with 'https://' * have all '@' replaced with '/' * not be normalized with local domain patterns thus allowing to look up kind of any url in our db this is kinda reinventing the wheel, but i really don't want to have local-only ids and would much rather have a local-only way to display them, because at least everyone can understand it and look up anything remote easily --- src/routes/activitypub/activity.rs | 7 ++++++- src/routes/activitypub/mod.rs | 5 ----- src/routes/activitypub/object.rs | 7 ++++++- src/routes/activitypub/user/mod.rs | 14 ++++---------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/routes/activitypub/activity.rs b/src/routes/activitypub/activity.rs index ebdb4fc..59e8ba5 100644 --- a/src/routes/activitypub/activity.rs +++ b/src/routes/activitypub/activity.rs @@ -25,8 +25,13 @@ pub async fn view( Path(id): Path, AuthIdentity(auth): AuthIdentity, ) -> Result, StatusCode> { + let aid = if id.starts_with('+') { + format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) + } else { + ctx.aid(id.clone()) + }; match model::addressing::Entity::find_activities() - .filter(model::activity::Column::Id.eq(ctx.aid(id))) + .filter(model::activity::Column::Id.eq(aid)) .filter(auth.filter_condition()) .into_model::() .one(ctx.db()) diff --git a/src/routes/activitypub/mod.rs b/src/routes/activitypub/mod.rs index 995cefc..c4cd3a9 100644 --- a/src/routes/activitypub/mod.rs +++ b/src/routes/activitypub/mod.rs @@ -65,11 +65,6 @@ impl ActivityPubRouter for Router { } } -#[derive(Debug, serde::Deserialize)] -pub struct RemoteId { - pub id: Option, -} - #[derive(Debug, serde::Deserialize)] // TODO i don't really like how pleroma/mastodon do it actually, maybe change this? pub struct Pagination { diff --git a/src/routes/activitypub/object.rs b/src/routes/activitypub/object.rs index 1471e50..716d461 100644 --- a/src/routes/activitypub/object.rs +++ b/src/routes/activitypub/object.rs @@ -28,8 +28,13 @@ pub async fn view( Path(id): Path, AuthIdentity(auth): AuthIdentity, ) -> Result, StatusCode> { + let oid = if id.starts_with('+') { + format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) + } else { + ctx.oid(id.clone()) + }; match model::addressing::Entity::find_activities() - .filter(model::object::Column::Id.eq(ctx.oid(id))) + .filter(model::object::Column::Id.eq(oid)) .filter(auth.filter_condition()) .into_model::() .one(ctx.db()) diff --git a/src/routes/activitypub/user/mod.rs b/src/routes/activitypub/user/mod.rs index c6dfbd4..2d20958 100644 --- a/src/routes/activitypub/user/mod.rs +++ b/src/routes/activitypub/user/mod.rs @@ -4,13 +4,13 @@ pub mod outbox; pub mod following; -use axum::extract::{Path, Query, State}; +use axum::extract::{Path, State}; use sea_orm::EntityTrait; use apb::{PublicKeyMut, ActorMut, DocumentMut, DocumentType, ObjectMut, BaseMut, Node}; use crate::{errors::UpubError, model::{self, user}, server::Context, url}; -use super::{jsonld::LD, JsonLD, RemoteId}; +use super::{jsonld::LD, JsonLD}; pub fn ap_user(user: model::user::Model) -> serde_json::Value { serde_json::Value::new_object() @@ -47,15 +47,9 @@ pub fn ap_user(user: model::user::Model) -> serde_json::Value { pub async fn view( State(ctx) : State, Path(id): Path, - Query(rid): Query, ) -> crate::Result> { - // TODO can this be made less convoluted??? - let uid = if id == "+" { - if let Some(rid) = rid.id { - rid - } else { - return Err(UpubError::bad_request()); - } + let uid = if id.starts_with('+') { + format!("https://{}", id.replacen('+', "", 1).replace('@', "/")) } else { ctx.uid(id.clone()) };