chore: move "local-or-remote" logic insite trait
This commit is contained in:
parent
129213204a
commit
64a05aae0c
3 changed files with 27 additions and 26 deletions
|
@ -201,7 +201,9 @@ impl Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::ext::IntoActivityPub for Model {
|
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()
|
apb::new()
|
||||||
.set_id(Some(self.id.clone()))
|
.set_id(Some(self.id.clone()))
|
||||||
.set_actor_type(Some(self.actor_type))
|
.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_updated(if self.updated != self.published { Some(self.updated) } else { None })
|
||||||
.set_preferred_username(Some(self.preferred_username))
|
.set_preferred_username(Some(self.preferred_username))
|
||||||
.set_statuses_count(Some(self.statuses_count as u64))
|
.set_statuses_count(Some(self.statuses_count as u64))
|
||||||
.set_followers_count(Some(self.followers_count as u64))
|
// local users may want to hide these! default to hidden, and downstream we can opt-in to
|
||||||
.set_following_count(Some(self.following_count as u64))
|
// showing them. for remote users we assume the number is already "protected" by remote
|
||||||
.set_inbox(apb::Node::maybe_link(self.inbox))
|
// instance so we just show it
|
||||||
.set_outbox(apb::Node::maybe_link(self.outbox))
|
.set_followers_count(if is_local { None } else { Some(self.followers_count as u64) })
|
||||||
.set_following(apb::Node::maybe_link(self.following))
|
.set_following_count(if is_local { None } else { Some(self.following_count as u64) })
|
||||||
.set_followers(apb::Node::maybe_link(self.followers))
|
.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(
|
.set_public_key(apb::Node::object(
|
||||||
apb::new()
|
apb::new()
|
||||||
.set_id(Some(format!("{}#main-key", self.id)))
|
.set_id(Some(format!("{}#main-key", self.id)))
|
||||||
|
@ -241,7 +246,8 @@ impl crate::ext::IntoActivityPub for Model {
|
||||||
))
|
))
|
||||||
.set_endpoints(apb::Node::object(
|
.set_endpoints(apb::Node::object(
|
||||||
apb::new()
|
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_also_known_as(apb::Node::links(self.also_known_as.0))
|
||||||
.set_moved_to(apb::Node::maybe_link(self.moved_to))
|
.set_moved_to(apb::Node::maybe_link(self.moved_to))
|
||||||
|
|
|
@ -161,9 +161,8 @@ impl Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::ext::IntoActivityPub for Model {
|
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()
|
apb::new()
|
||||||
.set_id(Some(self.id))
|
|
||||||
.set_object_type(Some(self.object_type))
|
.set_object_type(Some(self.object_type))
|
||||||
.set_attributed_to(apb::Node::maybe_link(self.attributed_to))
|
.set_attributed_to(apb::Node::maybe_link(self.attributed_to))
|
||||||
.set_name(self.name)
|
.set_name(self.name)
|
||||||
|
@ -175,7 +174,7 @@ impl crate::ext::IntoActivityPub for Model {
|
||||||
.set_url(apb::Node::link(x))
|
.set_url(apb::Node::link(x))
|
||||||
)))
|
)))
|
||||||
.set_context(apb::Node::maybe_link(self.context.clone()))
|
.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_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone()))
|
||||||
.set_quote_url(apb::Node::maybe_link(self.quote.clone()))
|
.set_quote_url(apb::Node::maybe_link(self.quote.clone()))
|
||||||
.set_published(Some(self.published))
|
.set_published(Some(self.published))
|
||||||
|
@ -199,9 +198,12 @@ impl crate::ext::IntoActivityPub for Model {
|
||||||
))
|
))
|
||||||
.set_replies(apb::Node::object(
|
.set_replies(apb::Node::object(
|
||||||
apb::new()
|
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_collection_type(Some(apb::CollectionType::OrderedCollection))
|
||||||
.set_total_items(Some(self.replies as u64))
|
.set_total_items(Some(self.replies as u64))
|
||||||
))
|
))
|
||||||
|
.set_id(Some(self.id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub mod notifications;
|
||||||
|
|
||||||
use axum::extract::{Path, Query, State};
|
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 upub::{ext::AnyQuery, model, traits::Fetcher, Context};
|
||||||
|
|
||||||
use crate::{builders::JsonLD, ApiError, AuthIdentity};
|
use crate::{builders::JsonLD, ApiError, AuthIdentity};
|
||||||
|
@ -56,32 +56,25 @@ pub async fn view(
|
||||||
{
|
{
|
||||||
// local user
|
// local user
|
||||||
Some((user_model, Some(cfg))) => {
|
Some((user_model, Some(cfg))) => {
|
||||||
|
let (followers, following) = (user_model.followers_count, user_model.following_count);
|
||||||
let mut user = ctx.ap(user_model)
|
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_following_me(following_me)
|
||||||
.set_followed_by_me(followed_by_me)
|
.set_followed_by_me(followed_by_me)
|
||||||
.set_manually_approves_followers(Some(!cfg.accept_follow_requests))
|
.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")))
|
|
||||||
));
|
|
||||||
|
|
||||||
if auth.is(&uid) {
|
if auth.is(&uid) {
|
||||||
user = user.set_notifications(Node::link(upub::url!(ctx, "/actors/{id}/notifications")));
|
user = user.set_notifications(Node::link(upub::url!(ctx, "/actors/{id}/notifications")));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !auth.is(&uid) && !cfg.show_followers_count {
|
if auth.is(&uid) || cfg.show_followers_count {
|
||||||
user = user.set_followers_count(None);
|
user = user.set_followers_count(Some(u64::try_from(followers).unwrap_or(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !auth.is(&uid) && !cfg.show_following_count {
|
if auth.is(&uid) || cfg.show_following_count {
|
||||||
user = user.set_following_count(None);
|
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 {
|
if let Some(ref fe) = ctx.cfg().instance.frontend {
|
||||||
user = user.set_url(Node::link(format!("{fe}/actors/{id}")));
|
user = user.set_url(Node::link(format!("{fe}/actors/{id}")));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue