From d36ac82dd987ed2ad6fc5931b3b5a5cce92c51f9 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 23 Mar 2024 16:44:27 +0100 Subject: [PATCH] feat: add followers and followers count to users redo migrations (: honestly if you're expecting to keep a long-lasting db at this stage of development i have bad news for you --- src/migrations/m20240316_000001_create_table.rs | 4 ++++ src/model/user.rs | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/migrations/m20240316_000001_create_table.rs b/src/migrations/m20240316_000001_create_table.rs index 62c68951..9007f65f 100644 --- a/src/migrations/m20240316_000001_create_table.rs +++ b/src/migrations/m20240316_000001_create_table.rs @@ -28,6 +28,8 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Users::Outbox).string().null()) .col(ColumnDef::new(Users::Following).string().null()) .col(ColumnDef::new(Users::Followers).string().null()) + .col(ColumnDef::new(Users::FollowingCount).integer().not_null().default(0)) + .col(ColumnDef::new(Users::FollowersCount).integer().not_null().default(0)) .col(ColumnDef::new(Users::PublicKey).string().not_null()) .col(ColumnDef::new(Users::PrivateKey).string().null()) .col(ColumnDef::new(Users::Created).date_time().not_null()) @@ -120,7 +122,9 @@ enum Users { SharedInbox, Outbox, Following, + FollowingCount, Followers, + FollowersCount, PublicKey, PrivateKey, Created, diff --git a/src/model/user.rs b/src/model/user.rs index 2495ce4d..2d899024 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; use crate::activitystream::key::PublicKey as _; -use crate::{activitypub, activitystream::object::actor::{Actor, ActorType}}; +use crate::{activitypub, activitystream::object::{collection::Collection, actor::{Actor, ActorType}}}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "users")] @@ -24,6 +24,9 @@ pub struct Model { pub following: Option, pub followers: Option, + pub following_count: i64, + pub followers_count: i64, + pub public_key: String, pub private_key: Option, @@ -53,6 +56,8 @@ impl Model { following: object.following().id().map(|x| x.to_string()), created: object.published().unwrap_or(chrono::Utc::now()), updated: chrono::Utc::now(), + following_count: object.following().get().map(|f| f.total_items().unwrap_or(0)).unwrap_or(0) as i64, + followers_count: object.followers().get().map(|f| f.total_items().unwrap_or(0)).unwrap_or(0) as i64, public_key: object.public_key().get().ok_or(super::FieldError("publicKey"))?.public_key_pem().to_string(), private_key: None, // there's no way to transport privkey over AP json, must come from DB })