From 64a05aae0c4a18394ce24976f420935a9b7fa8a0 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 26 Dec 2024 20:40:49 +0100 Subject: [PATCH] chore: move "local-or-remote" logic insite trait --- upub/core/src/model/actor.rs | 22 ++++++++++++++-------- upub/core/src/model/object.rs | 8 +++++--- upub/routes/src/activitypub/actor/mod.rs | 23 ++++++++--------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/upub/core/src/model/actor.rs b/upub/core/src/model/actor.rs index 76a4a07..ba13a4c 100644 --- a/upub/core/src/model/actor.rs +++ b/upub/core/src/model/actor.rs @@ -201,7 +201,9 @@ impl Entity { } impl crate::ext::IntoActivityPub for Model { - fn into_activity_pub_json(self, _ctx: &crate::Context) -> serde_json::Value { + fn into_activity_pub_json(self, ctx: &crate::Context) -> serde_json::Value { + let is_local = ctx.is_local(&self.id); + let id = ctx.id(&self.id); apb::new() .set_id(Some(self.id.clone())) .set_actor_type(Some(self.actor_type)) @@ -227,12 +229,15 @@ impl crate::ext::IntoActivityPub for Model { .set_updated(if self.updated != self.published { Some(self.updated) } else { None }) .set_preferred_username(Some(self.preferred_username)) .set_statuses_count(Some(self.statuses_count as u64)) - .set_followers_count(Some(self.followers_count as u64)) - .set_following_count(Some(self.following_count as u64)) - .set_inbox(apb::Node::maybe_link(self.inbox)) - .set_outbox(apb::Node::maybe_link(self.outbox)) - .set_following(apb::Node::maybe_link(self.following)) - .set_followers(apb::Node::maybe_link(self.followers)) + // local users may want to hide these! default to hidden, and downstream we can opt-in to + // showing them. for remote users we assume the number is already "protected" by remote + // instance so we just show it + .set_followers_count(if is_local { None } else { Some(self.followers_count as u64) }) + .set_following_count(if is_local { None } else { Some(self.following_count as u64) }) + .set_inbox(if is_local { apb::Node::link(crate::url!(ctx, "/actors/{id}/inbox")) } else { apb::Node::maybe_link(self.inbox) }) + .set_outbox(if is_local { apb::Node::link(crate::url!(ctx, "/actors/{id}/outbox")) } else { apb::Node::maybe_link(self.outbox) }) + .set_following(if is_local { apb::Node::link(crate::url!(ctx, "/actors{id}/following")) } else { apb::Node::maybe_link(self.following) }) + .set_followers(if is_local { apb::Node::link(crate::url!(ctx, "/actors/{id}/followers")) } else { apb::Node::maybe_link(self.followers) }) .set_public_key(apb::Node::object( apb::new() .set_id(Some(format!("{}#main-key", self.id))) @@ -241,7 +246,8 @@ impl crate::ext::IntoActivityPub for Model { )) .set_endpoints(apb::Node::object( apb::new() - .set_shared_inbox(self.shared_inbox) + .set_shared_inbox(if is_local { Some(crate::url!(ctx, "/inbox")) } else { self.shared_inbox }) + .set_proxy_url(if is_local { Some(crate::url!(ctx, "/fetch")) } else { None }) )) .set_also_known_as(apb::Node::links(self.also_known_as.0)) .set_moved_to(apb::Node::maybe_link(self.moved_to)) diff --git a/upub/core/src/model/object.rs b/upub/core/src/model/object.rs index 3e2dc5a..977bc89 100644 --- a/upub/core/src/model/object.rs +++ b/upub/core/src/model/object.rs @@ -161,9 +161,8 @@ impl Entity { } impl crate::ext::IntoActivityPub for Model { - fn into_activity_pub_json(self, _ctx: &crate::Context) -> serde_json::Value { + fn into_activity_pub_json(self, ctx: &crate::Context) -> serde_json::Value { apb::new() - .set_id(Some(self.id)) .set_object_type(Some(self.object_type)) .set_attributed_to(apb::Node::maybe_link(self.attributed_to)) .set_name(self.name) @@ -175,7 +174,7 @@ impl crate::ext::IntoActivityPub for Model { .set_url(apb::Node::link(x)) ))) .set_context(apb::Node::maybe_link(self.context.clone())) - .set_conversation(apb::Node::maybe_link(self.context.clone())) // duplicate context for mastodon + .set_conversation(apb::Node::maybe_link(self.context)) // duplicate context for mastodon .set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone())) .set_quote_url(apb::Node::maybe_link(self.quote.clone())) .set_published(Some(self.published)) @@ -199,9 +198,12 @@ impl crate::ext::IntoActivityPub for Model { )) .set_replies(apb::Node::object( apb::new() + .set_id(if ctx.is_local(&self.id) { Some(format!("{}/replies", self.id)) } else { None }) + .set_first( if ctx.is_local(&self.id) { apb::Node::link(format!("{}/replies/page", self.id)) } else { apb::Node::Empty }) .set_collection_type(Some(apb::CollectionType::OrderedCollection)) .set_total_items(Some(self.replies as u64)) )) + .set_id(Some(self.id)) } } diff --git a/upub/routes/src/activitypub/actor/mod.rs b/upub/routes/src/activitypub/actor/mod.rs index b6285c8..8294067 100644 --- a/upub/routes/src/activitypub/actor/mod.rs +++ b/upub/routes/src/activitypub/actor/mod.rs @@ -7,7 +7,7 @@ pub mod notifications; use axum::extract::{Path, Query, State}; -use apb::{LD, ActorMut, EndpointsMut, Node, ObjectMut}; +use apb::{LD, ActorMut, Node, ObjectMut}; use upub::{ext::AnyQuery, model, traits::Fetcher, Context}; use crate::{builders::JsonLD, ApiError, AuthIdentity}; @@ -56,32 +56,25 @@ pub async fn view( { // local user Some((user_model, Some(cfg))) => { + let (followers, following) = (user_model.followers_count, user_model.following_count); let mut user = ctx.ap(user_model) - .set_inbox(Node::link(upub::url!(ctx, "/actors/{id}/inbox"))) - .set_outbox(Node::link(upub::url!(ctx, "/actors/{id}/outbox"))) - .set_following(Node::link(upub::url!(ctx, "/actors/{id}/following"))) - .set_followers(Node::link(upub::url!(ctx, "/actors/{id}/followers"))) .set_following_me(following_me) .set_followed_by_me(followed_by_me) - .set_manually_approves_followers(Some(!cfg.accept_follow_requests)) - .set_endpoints(Node::object( - apb::new() - .set_shared_inbox(Some(upub::url!(ctx, "/inbox"))) - .set_proxy_url(Some(upub::url!(ctx, "/fetch"))) - )); + .set_manually_approves_followers(Some(!cfg.accept_follow_requests)); if auth.is(&uid) { user = user.set_notifications(Node::link(upub::url!(ctx, "/actors/{id}/notifications"))); } - if !auth.is(&uid) && !cfg.show_followers_count { - user = user.set_followers_count(None); + if auth.is(&uid) || cfg.show_followers_count { + user = user.set_followers_count(Some(u64::try_from(followers).unwrap_or(0))); } - if !auth.is(&uid) && !cfg.show_following_count { - user = user.set_following_count(None); + if auth.is(&uid) || cfg.show_following_count { + user = user.set_following_count(Some(u64::try_from(following).unwrap_or(0))); } + // TODO this is known "magically" !! very tight coupling ouchhh if let Some(ref fe) = ctx.cfg().instance.frontend { user = user.set_url(Node::link(format!("{fe}/actors/{id}"))); }