1
0
Fork 0
forked from alemi/upub

fix: new follows update the relation, fix counters

This commit is contained in:
əlemi 2024-06-08 17:22:12 +02:00
parent fb0242221b
commit 743080395b
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,5 +1,5 @@
use apb::{target::Addressed, Activity, Base, Object}; use apb::{target::Addressed, Activity, Base, Object};
use sea_orm::{sea_query::Expr, ActiveValue::{NotSet, Set}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter}; use sea_orm::{sea_query::Expr, ActiveValue::{NotSet, Set}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter, QuerySelect, SelectColumns};
use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}}; use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@ -121,15 +121,37 @@ pub async fn follow(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
.ok_or(ProcessorError::Incomplete)?; .ok_or(ProcessorError::Incomplete)?;
let usr = ctx.fetch_user(activity.object().id()?, tx).await?; let usr = ctx.fetch_user(activity.object().id()?, tx).await?;
let activity_model = ctx.insert_activity(activity, tx).await?; let activity_model = ctx.insert_activity(activity, tx).await?;
let relation_model = crate::model::relation::ActiveModel {
internal: NotSet, if let Some(relation) = crate::model::relation::Entity::find()
accept: Set(None), .filter(crate::model::relation::Column::Follower.eq(source_actor_internal))
activity: Set(activity_model.internal), .filter(crate::model::relation::Column::Following.eq(usr.internal))
follower: Set(source_actor_internal), .select_only()
following: Set(usr.internal), .select_column(crate::model::relation::Column::Internal)
}; .into_tuple::<i64>()
crate::model::relation::Entity::insert(relation_model) .one(tx)
.exec(tx).await?; .await?
{
// already requested, update pending row
crate::model::relation::Entity::update_many()
.col_expr(crate::model::relation::Column::Activity, Expr::value(Some(activity_model.internal)))
.filter(crate::model::relation::Column::Internal.eq(relation))
.exec(tx)
.await?;
} else {
// new follow request, make new row
let relation_model = crate::model::relation::ActiveModel {
internal: NotSet,
accept: Set(None),
activity: Set(activity_model.internal),
follower: Set(source_actor_internal),
following: Set(usr.internal),
};
crate::model::relation::Entity::insert(relation_model)
.exec(tx).await?;
}
tracing::info!("{} wants to follow {}", activity_model.actor, usr.id); tracing::info!("{} wants to follow {}", activity_model.actor, usr.id);
Ok(()) Ok(())
} }
@ -147,9 +169,18 @@ pub async fn accept(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
let activity_model = ctx.insert_activity(activity, tx).await?; let activity_model = ctx.insert_activity(activity, tx).await?;
let follower = crate::model::actor::Entity::ap_to_internal(&follow_activity.actor, tx)
.await?
.ok_or(ProcessorError::Incomplete)?;
let following = crate::model::actor::Entity::ap_to_internal(&activity_model.actor, tx)
.await?
.ok_or(ProcessorError::Incomplete)?;
crate::model::relation::Entity::update_many() crate::model::relation::Entity::update_many()
.col_expr(crate::model::relation::Column::Accept, Expr::value(Some(activity_model.internal))) .col_expr(crate::model::relation::Column::Accept, Expr::value(Some(activity_model.internal)))
.filter(crate::model::relation::Column::Activity.eq(follow_activity.internal)) .col_expr(crate::model::relation::Column::Activity, Expr::value(Some(follow_activity.internal)))
.filter(crate::model::relation::Column::Follower.eq(follower))
.filter(crate::model::relation::Column::Following.eq(following))
.exec(tx).await?; .exec(tx).await?;
crate::model::actor::Entity::update_many() crate::model::actor::Entity::update_many()
@ -157,7 +188,7 @@ pub async fn accept(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
crate::model::actor::Column::FollowingCount, crate::model::actor::Column::FollowingCount,
Expr::col(crate::model::actor::Column::FollowingCount).add(1) Expr::col(crate::model::actor::Column::FollowingCount).add(1)
) )
.filter(crate::model::actor::Column::Id.eq(&follow_activity.actor)) .filter(crate::model::actor::Column::Internal.eq(follower))
.exec(tx) .exec(tx)
.await?; .await?;
crate::model::actor::Entity::update_many() crate::model::actor::Entity::update_many()
@ -165,7 +196,7 @@ pub async fn accept(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
crate::model::actor::Column::FollowersCount, crate::model::actor::Column::FollowersCount,
Expr::col(crate::model::actor::Column::FollowersCount).add(1) Expr::col(crate::model::actor::Column::FollowersCount).add(1)
) )
.filter(crate::model::actor::Column::Id.eq(&follow_activity.actor)) .filter(crate::model::actor::Column::Internal.eq(following))
.exec(tx) .exec(tx)
.await?; .await?;