From f487ac06e92363b15674331c5d1796f430c8004a Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 29 May 2024 19:25:43 +0200 Subject: [PATCH] fix: is_following helper, fixed fe follow fields --- src/model/relation.rs | 12 ++++++++++++ src/routes/activitypub/user/mod.rs | 30 ++++++------------------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/model/relation.rs b/src/model/relation.rs index 6ca98b4f..1c4deab8 100644 --- a/src/model/relation.rs +++ b/src/model/relation.rs @@ -62,6 +62,7 @@ impl Related for Entity { impl ActiveModelBehavior for ActiveModel {} impl Entity { + // TODO this is 2 queries!!! can it be optimized down to 1? pub async fn followers(uid: &str, db: &DatabaseConnection) -> crate::Result> { let internal_id = super::actor::Entity::ap_to_internal(uid, db).await?; let out = Entity::find() @@ -82,6 +83,7 @@ impl Entity { Ok(out) } + // TODO this is 2 queries!!! can it be optimized down to 1? pub async fn following(uid: &str, db: &DatabaseConnection) -> crate::Result> { let internal_id = super::actor::Entity::ap_to_internal(uid, db).await?; let out = Entity::find() @@ -101,4 +103,14 @@ impl Entity { Ok(out) } + + // TODO this is 3 queries!!! can it be optimized down to 1? + pub fn is_following(follower: i64, following: i64) -> sea_orm::Selector> { + Entity::find() + .filter(Column::Follower.eq(follower)) + .filter(Column::Following.eq(following)) + .select_only() + .select_column(Column::Internal) + .into_tuple::() + } } diff --git a/src/routes/activitypub/user/mod.rs b/src/routes/activitypub/user/mod.rs index a8076bfd..7dbd9d7b 100644 --- a/src/routes/activitypub/user/mod.rs +++ b/src/routes/activitypub/user/mod.rs @@ -5,10 +5,9 @@ pub mod outbox; pub mod following; use axum::extract::{Path, Query, State}; -use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect, SelectColumns}; use apb::{ActorMut, EndpointsMut, Node}; -use crate::{errors::UpubError, model, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url}; +use crate::{errors::UpubError, model, server::{auth::AuthIdentity, builders::AnyQuery, fetcher::Fetcher, Context}, url}; use super::{jsonld::LD, JsonLD, TryFetch}; @@ -30,34 +29,17 @@ pub async fn view( ctx.fetch_user(&uid).await?; } } + let internal_uid = model::actor::Entity::ap_to_internal(&uid, ctx.db()).await?; let (followed_by_me, following_me) = match auth.my_id() { None => (None, None), Some(my_id) => { // TODO these two queries are fast because of indexes but still are 2 subqueries for each - // user GET, not even parallelized... should really add these as joins on the main query, so + // user GET, not even parallelized... should maybe add these as joins on the main query? so // that it's one roundtrip only - let followed_by_me = model::relation::Entity::find() - .filter(model::relation::Column::Follower.eq(my_id)) - .filter(model::relation::Column::Following.eq(&uid)) - .select_only() - .select_column(model::relation::Column::Follower) - .into_tuple::() - .one(ctx.db()) - .await? - .map(|_| true); - - let following_me = model::relation::Entity::find() - .filter(model::relation::Column::Following.eq(my_id)) - .filter(model::relation::Column::Follower.eq(&uid)) - .select_only() - .select_column(model::relation::Column::Follower) - .into_tuple::() - .one(ctx.db()) - .await? - .map(|_| true); - - (followed_by_me, following_me) + let followed_by_me = model::relation::Entity::is_following(my_id, internal_uid).any(ctx.db()).await?; + let following_me = model::relation::Entity::is_following(internal_uid, my_id).any(ctx.db()).await?; + (Some(followed_by_me), Some(following_me)) }, };