From ab5d0cb61aa74e46e7c1991a770d9813a73308cc Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 19 Apr 2024 04:42:43 +0200 Subject: [PATCH] fix: replies only in object view, page uris --- src/model/object.rs | 9 +------ src/routes/activitypub/object/mod.rs | 38 ++++++++++++++++++++-------- src/server/context.rs | 11 +++++--- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/model/object.rs b/src/model/object.rs index b288767..eeceba9 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -1,4 +1,4 @@ -use apb::{BaseMut, CollectionMut, ObjectMut}; +use apb::{BaseMut, ObjectMut}; use sea_orm::entity::prelude::*; use crate::routes::activitypub::jsonld::LD; @@ -59,13 +59,6 @@ impl Model { .set_content(self.content.as_deref()) .set_context(apb::Node::maybe_link(self.context.clone())) .set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone())) - .set_replies(apb::Node::object( - serde_json::Value::new_object() - .set_id(Some(&format!("{}/replies", self.id))) - .set_collection_type(Some(apb::CollectionType::OrderedCollection)) - .set_first(apb::Node::link(format!("{}/replies/page", self.id))) - .set_total_items(Some(self.comments as u64)) - )) .set_published(Some(self.published)) .set_to(apb::Node::links(self.to.0.clone())) .set_bto(apb::Node::Empty) diff --git a/src/routes/activitypub/object/mod.rs b/src/routes/activitypub/object/mod.rs index 9477b7c..d83ddf6 100644 --- a/src/routes/activitypub/object/mod.rs +++ b/src/routes/activitypub/object/mod.rs @@ -1,5 +1,6 @@ pub mod replies; +use apb::{BaseMut, CollectionMut, ObjectMut}; use axum::extract::{Path, Query, State}; use sea_orm::{ColumnTrait, QueryFilter}; @@ -18,19 +19,36 @@ pub async fn view( } else { ctx.oid(id.clone()) }; - match model::addressing::Entity::find_activities() + + let result = model::addressing::Entity::find_activities() .filter(model::object::Column::Id.eq(&oid)) .filter(auth.filter_condition()) .into_model::() .one(ctx.db()) - .await? - { - Some(EmbeddedActivity { activity: _, object: Some(object) }) => Ok(JsonLD(object.ap().ld_context())), - Some(EmbeddedActivity { activity: _, object: None }) => Err(UpubError::not_found()), - None => if auth.is_local() && query.fetch && !ctx.is_local(&oid) { - Ok(JsonLD(ctx.fetch_object(&oid).await?.ap().ld_context())) - } else { - Err(UpubError::not_found()) + .await?; + + let object = match result { + Some(EmbeddedActivity { activity: _, object: Some(obj) }) => obj, + _ => { + if auth.is_local() && query.fetch && !ctx.is_local(&oid) { + ctx.fetch_object(&oid).await? + } else { + return Err(UpubError::not_found()) + } }, - } + }; + + let replies = + serde_json::Value::new_object() + .set_id(Some(&crate::url!(ctx, "/objects/{id}/replies"))) + .set_collection_type(Some(apb::CollectionType::OrderedCollection)) + .set_first(apb::Node::link(crate::url!(ctx, "/objects/{id}/replies/page"))) + .set_total_items(Some(object.comments as u64)); + + + Ok(JsonLD( + object.ap() + .set_replies(apb::Node::object(replies)) + .ld_context() + )) } diff --git a/src/server/context.rs b/src/server/context.rs index c14ccce..5c664d0 100644 --- a/src/server/context.rs +++ b/src/server/context.rs @@ -105,11 +105,14 @@ impl Context { } /// get bare id, usually an uuid but unspecified - pub fn id(&self, id: String) -> String { - if id.starts_with(&self.0.domain) { - id.split('/').last().unwrap_or("").to_string() + pub fn id(&self, uri: &str) -> String { + if uri.starts_with(&self.0.domain) { + uri.split('/').last().unwrap_or("").to_string() } else { - id + uri + .replace("https://", "+") + .replace("http://", "+") + .replace('/', "@") } }