chore: move "local-or-remote" logic insite trait

This commit is contained in:
əlemi 2024-12-26 20:40:49 +01:00
parent 129213204a
commit 64a05aae0c
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 27 additions and 26 deletions

View file

@ -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))

View file

@ -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))
}
}

View file

@ -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}")));
}