1
0
Fork 0
forked from alemi/upub

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
This commit is contained in:
əlemi 2024-04-15 21:36:31 +02:00
parent f1ff946245
commit ee26596568
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 16 additions and 17 deletions

View file

@ -25,8 +25,13 @@ pub async fn view(
Path(id): Path<String>, Path(id): Path<String>,
AuthIdentity(auth): AuthIdentity, AuthIdentity(auth): AuthIdentity,
) -> Result<JsonLD<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
let aid = if id.starts_with('+') {
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
} else {
ctx.aid(id.clone())
};
match model::addressing::Entity::find_activities() 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()) .filter(auth.filter_condition())
.into_model::<EmbeddedActivity>() .into_model::<EmbeddedActivity>()
.one(ctx.db()) .one(ctx.db())

View file

@ -65,11 +65,6 @@ impl ActivityPubRouter for Router<crate::server::Context> {
} }
} }
#[derive(Debug, serde::Deserialize)]
pub struct RemoteId {
pub id: Option<String>,
}
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
// TODO i don't really like how pleroma/mastodon do it actually, maybe change this? // TODO i don't really like how pleroma/mastodon do it actually, maybe change this?
pub struct Pagination { pub struct Pagination {

View file

@ -28,8 +28,13 @@ pub async fn view(
Path(id): Path<String>, Path(id): Path<String>,
AuthIdentity(auth): AuthIdentity, AuthIdentity(auth): AuthIdentity,
) -> Result<JsonLD<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
let oid = if id.starts_with('+') {
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
} else {
ctx.oid(id.clone())
};
match model::addressing::Entity::find_activities() 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()) .filter(auth.filter_condition())
.into_model::<EmbeddedActivity>() .into_model::<EmbeddedActivity>()
.one(ctx.db()) .one(ctx.db())

View file

@ -4,13 +4,13 @@ pub mod outbox;
pub mod following; pub mod following;
use axum::extract::{Path, Query, State}; use axum::extract::{Path, State};
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
use apb::{PublicKeyMut, ActorMut, DocumentMut, DocumentType, ObjectMut, BaseMut, Node}; use apb::{PublicKeyMut, ActorMut, DocumentMut, DocumentType, ObjectMut, BaseMut, Node};
use crate::{errors::UpubError, model::{self, user}, server::Context, url}; 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 { pub fn ap_user(user: model::user::Model) -> serde_json::Value {
serde_json::Value::new_object() serde_json::Value::new_object()
@ -47,15 +47,9 @@ pub fn ap_user(user: model::user::Model) -> serde_json::Value {
pub async fn view( pub async fn view(
State(ctx) : State<Context>, State(ctx) : State<Context>,
Path(id): Path<String>, Path(id): Path<String>,
Query(rid): Query<RemoteId>,
) -> crate::Result<JsonLD<serde_json::Value>> { ) -> crate::Result<JsonLD<serde_json::Value>> {
// TODO can this be made less convoluted??? let uid = if id.starts_with('+') {
let uid = if id == "+" { format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
if let Some(rid) = rid.id {
rid
} else {
return Err(UpubError::bad_request());
}
} else { } else {
ctx.uid(id.clone()) ctx.uid(id.clone())
}; };