From a6c3ef9a3e817247d4a8ab4a55b757588dbf3b1a Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 30 Apr 2024 14:22:44 +0200 Subject: [PATCH] feat: increment statuses count be it when posting, receiving a Create or fetching an object, increment relevant user statuses count --- src/server/fetcher.rs | 5 +++++ src/server/inbox.rs | 9 +++++++++ src/server/outbox.rs | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/server/fetcher.rs b/src/server/fetcher.rs index 382b5e6..8d8e8ff 100644 --- a/src/server/fetcher.rs +++ b/src/server/fetcher.rs @@ -166,6 +166,11 @@ async fn fetch_object_inner(ctx: &Context, id: &str, depth: usize) -> crate::Res if let Err(e) = ctx.fetch_user(&attributed_to).await { tracing::warn!("could not get actor of fetched object: {e}"); } + model::user::Entity::update_many() + .col_expr(model::user::Column::StatusesCount, Expr::col(model::user::Column::StatusesCount).add(1)) + .filter(model::user::Column::Id.eq(&attributed_to)) + .exec(ctx.db()) + .await?; } let addressed = object.addressed(); diff --git a/src/server/inbox.rs b/src/server/inbox.rs index 055e246..8b6eb90 100644 --- a/src/server/inbox.rs +++ b/src/server/inbox.rs @@ -22,6 +22,7 @@ impl apb::server::Inbox for Context { }; let mut object_model = model::object::Model::new(&object_node)?; let oid = object_model.id.clone(); + let uid = object_model.attributed_to.clone(); // make sure we're allowed to edit this object if let Some(object_author) = &object_model.attributed_to { if server != Context::server(object_author) { @@ -58,6 +59,14 @@ impl apb::server::Inbox for Context { .exec(self.db()) .await?; } + // TODO can we even receive anonymous objects? + if let Some(object_author) = uid { + model::user::Entity::update_many() + .col_expr(model::user::Column::StatusesCount, Expr::col(model::user::Column::StatusesCount).add(1)) + .filter(model::user::Column::Id.eq(&object_author)) + .exec(self.db()) + .await?; + } let expanded_addressing = self.expand_addressing(activity.addressed()).await?; self.address_to(Some(&aid), Some(&oid), &expanded_addressing).await?; tracing::info!("{} posted {}", aid, oid); diff --git a/src/server/outbox.rs b/src/server/outbox.rs index 85c8594..d7ba823 100644 --- a/src/server/outbox.rs +++ b/src/server/outbox.rs @@ -47,6 +47,11 @@ impl apb::server::Outbox for Context { .exec(self.db()).await?; model::activity::Entity::insert(activity_model.into_active_model()) .exec(self.db()).await?; + model::user::Entity::update_many() + .col_expr(model::user::Column::StatusesCount, Expr::col(model::user::Column::StatusesCount).add(1)) + .filter(model::user::Column::Id.eq(&uid)) + .exec(self.db()) + .await?; self.dispatch(&uid, activity_targets, &aid, Some(&oid)).await?; @@ -90,6 +95,11 @@ impl apb::server::Outbox for Context { .exec(self.db()).await?; model::activity::Entity::insert(activity_model.into_active_model()) .exec(self.db()).await?; + model::user::Entity::update_many() + .col_expr(model::user::Column::StatusesCount, Expr::col(model::user::Column::StatusesCount).add(1)) + .filter(model::user::Column::Id.eq(&uid)) + .exec(self.db()) + .await?; self.dispatch(&uid, activity_targets, &aid, Some(&oid)).await?;