diff --git a/src/routes/activitypub/user/mod.rs b/src/routes/activitypub/user/mod.rs index ca1af867..54387d9e 100644 --- a/src/routes/activitypub/user/mod.rs +++ b/src/routes/activitypub/user/mod.rs @@ -5,9 +5,9 @@ pub mod outbox; pub mod following; use axum::extract::{Path, Query, State}; -use sea_orm::EntityTrait; +use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, SelectColumns}; -use apb::{ActorMut, Node, ObjectMut}; +use apb::{ActorMut, CollectionMut, Node, Object, ObjectMut}; use crate::{errors::UpubError, model::{self, user}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url}; use super::{jsonld::LD, JsonLD, TryFetch}; @@ -32,18 +32,48 @@ pub async fn view( .one(ctx.db()).await? { // local user - Some((user, Some(cfg))) => { - let mut user = user.ap() + Some((user_model, Some(cfg))) => { + let mut user = user_model.ap() .set_inbox(Node::link(url!(ctx, "/users/{id}/inbox"))) .set_outbox(Node::link(url!(ctx, "/users/{id}/outbox"))) .set_following(Node::link(url!(ctx, "/users/{id}/following"))) .set_followers(Node::link(url!(ctx, "/users/{id}/followers"))); - if !cfg.show_followers_count { + // TODO maybe this thing could be made as a single join, to avoid triple db roundtrip for + // each fetch made by local users? it's indexed and fast but still... + if let Some(my_id) = auth.my_id() { + if !auth.is(&uid) { + let followed_by_me = model::relation::Entity::find() + .filter(model::relation::Column::Follower.eq(my_id)) + .filter(model::relation::Column::Following.eq(&uid)) + .select_column(model::relation::Column::Follower) + .into_tuple::() + .all(ctx.db()) + .await?; + + user + .audience() + .update(|x| x.set_ordered_items(apb::Node::links(followed_by_me))); + + let following_me = model::relation::Entity::find() + .filter(model::relation::Column::Following.eq(my_id)) + .filter(model::relation::Column::Follower.eq(&uid)) + .select_column(model::relation::Column::Following) + .into_tuple::() + .all(ctx.db()) + .await?; + + user + .generator() + .update(|x| x.set_ordered_items(apb::Node::links(following_me))); + } + } + + if !auth.is(&uid) && !cfg.show_followers_count { user = user.set_audience(apb::Node::Empty); } - if !cfg.show_following_count { + if !auth.is(&uid) && !cfg.show_following_count { user = user.set_generator(apb::Node::Empty); }