feat: naive attempt to resolve followers/following

This commit is contained in:
əlemi 2024-05-26 18:41:56 +02:00
parent bcfd71eb06
commit c94bfdcbe8
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 63 additions and 1 deletions

View file

@ -61,6 +61,8 @@ pub enum Relation {
Mentions, Mentions,
#[sea_orm(has_many = "super::object::Entity")] #[sea_orm(has_many = "super::object::Entity")]
Objects, Objects,
#[sea_orm(has_many = "super::relation::Entity")]
Relations,
#[sea_orm(has_many = "super::session::Entity")] #[sea_orm(has_many = "super::session::Entity")]
Sessions, Sessions,
} }
@ -125,6 +127,12 @@ impl Related<super::object::Entity> for Entity {
} }
} }
impl Related<super::relation::Entity> for Entity {
fn to() -> RelationDef {
Relation::Relations.def()
}
}
impl Related<super::session::Entity> for Entity { impl Related<super::session::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::Sessions.def() Relation::Sessions.def()

View file

@ -1,4 +1,4 @@
use sea_orm::entity::prelude::*; use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "relations")] #[sea_orm(table_name = "relations")]
@ -47,4 +47,58 @@ pub enum Relation {
ActorsFollowing, ActorsFollowing,
} }
impl Related<super::actor::Entity> for Entity {
fn to() -> RelationDef {
Relation::ActorsFollowing.def()
}
}
impl Related<super::activity::Entity> for Entity {
fn to() -> RelationDef {
Relation::ActivitiesFollow.def()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}
impl Entity {
pub async fn followers(uid: &str, db: &DatabaseConnection) -> crate::Result<Vec<String>> {
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::<String>()
.all(db)
.await?;
Ok(out)
}
pub async fn following(uid: &str, db: &DatabaseConnection) -> crate::Result<Vec<String>> {
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::<String>()
.all(db)
.await?;
Ok(out)
}
}