From c94bfdcbe8e6bfc150d0f5e730c97da28b7ec06e Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 26 May 2024 18:41:56 +0200 Subject: [PATCH] feat: naive attempt to resolve followers/following --- src/model/actor.rs | 8 +++++++ src/model/relation.rs | 56 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/model/actor.rs b/src/model/actor.rs index 59a1732..b2d3b77 100644 --- a/src/model/actor.rs +++ b/src/model/actor.rs @@ -61,6 +61,8 @@ pub enum Relation { Mentions, #[sea_orm(has_many = "super::object::Entity")] Objects, + #[sea_orm(has_many = "super::relation::Entity")] + Relations, #[sea_orm(has_many = "super::session::Entity")] Sessions, } @@ -125,6 +127,12 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Relations.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::Sessions.def() diff --git a/src/model/relation.rs b/src/model/relation.rs index 2a91741..6ca98b4 100644 --- a/src/model/relation.rs +++ b/src/model/relation.rs @@ -1,4 +1,4 @@ -use sea_orm::entity::prelude::*; +use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "relations")] @@ -47,4 +47,58 @@ pub enum Relation { ActorsFollowing, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::ActorsFollowing.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ActivitiesFollow.def() + } +} + impl ActiveModelBehavior for ActiveModel {} + +impl Entity { + 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() + .join( + sea_orm::JoinType::InnerJoin, + Entity::belongs_to(super::actor::Entity) + .from(Column::Follower) + .to(super::actor::Column::Internal) + .into() + ) + .filter(Column::Following.eq(internal_id)) + .select_only() + .select_column(super::actor::Column::Id) + .into_tuple::() + .all(db) + .await?; + + Ok(out) + } + + 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() + .join( + sea_orm::JoinType::InnerJoin, + Entity::belongs_to(super::actor::Entity) + .from(Column::Following) + .to(super::actor::Column::Internal) + .into() + ) + .filter(Column::Follower.eq(internal_id)) + .select_only() + .select_column(super::actor::Column::Id) + .into_tuple::() + .all(db) + .await?; + + Ok(out) + } +}