From a897edd310e9bce3ebc77bf06a1f9375b3d96d24 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 18 Apr 2024 04:09:13 +0200 Subject: [PATCH] feat: add statuses count and in_reply_to fields migration + models update --- ...240418_000001_add_statuses_and_reply_to.rs | 72 +++++++++++++++++++ src/migrations/mod.rs | 2 + src/model/faker.rs | 2 + src/model/object.rs | 2 + src/model/user.rs | 3 +- 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/migrations/m20240418_000001_add_statuses_and_reply_to.rs diff --git a/src/migrations/m20240418_000001_add_statuses_and_reply_to.rs b/src/migrations/m20240418_000001_add_statuses_and_reply_to.rs new file mode 100644 index 0000000..effb34f --- /dev/null +++ b/src/migrations/m20240418_000001_add_statuses_and_reply_to.rs @@ -0,0 +1,72 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Users::Table) + .add_column( + ColumnDef::new(Users::StatusesCount) + .integer() + .not_null() + .default(0) + ) + .to_owned() + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(Objects::Table) + .add_column( + ColumnDef::new(Objects::InReplyTo) + .string() + .null() + ) + .to_owned() + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Users::Table) + .drop_column(Users::StatusesCount) + .to_owned() + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(Objects::Table) + .drop_column(Objects::InReplyTo) + .to_owned() + ) + .await?; + + Ok(()) + } +} + +#[derive(DeriveIden)] +enum Users { + Table, + StatusesCount, +} + +#[derive(DeriveIden)] +enum Objects { + Table, + InReplyTo, +} diff --git a/src/migrations/mod.rs b/src/migrations/mod.rs index 97bbc48..19d6301 100644 --- a/src/migrations/mod.rs +++ b/src/migrations/mod.rs @@ -9,6 +9,7 @@ mod m20240323_000002_add_simple_credentials; mod m20240324_000001_add_addressing; mod m20240325_000001_add_deliveries; mod m20240325_000002_add_system_key; +mod m20240418_000001_add_statuses_and_reply_to; pub struct Migrator; @@ -25,6 +26,7 @@ impl MigratorTrait for Migrator { Box::new(m20240324_000001_add_addressing::Migration), Box::new(m20240325_000001_add_deliveries::Migration), Box::new(m20240325_000002_add_system_key::Migration), + Box::new(m20240418_000001_add_statuses_and_reply_to::Migration), ] } } diff --git a/src/model/faker.rs b/src/model/faker.rs index cdb3252..0bb57c7 100644 --- a/src/model/faker.rs +++ b/src/model/faker.rs @@ -17,6 +17,7 @@ pub async fn faker(db: &sea_orm::DatabaseConnection, domain: String, count: u64) following_count: 0, followers: None, followers_count: 0, + statuses_count: count as i64, icon: Some("https://cdn.alemi.dev/social/circle-square.png".to_string()), image: Some("https://cdn.alemi.dev/social/someriver-xs.jpg".to_string()), inbox: None, @@ -69,6 +70,7 @@ pub async fn faker(db: &sea_orm::DatabaseConnection, domain: String, count: u64) attributed_to: Set(Some(format!("{domain}/users/test"))), summary: Set(None), context: Set(Some(context.clone())), + in_reply_to: Set(None), content: Set(Some(format!("[{i}] Tic(k). Quasiparticle of intensive multiplicity. Tics (or ticks) are intrinsically several components of autonomously numbering anorganic populations, propagating by contagion between segmentary divisions in the order of nature. Ticks - as nonqualitative differentially-decomposable counting marks - each designate a multitude comprehended as a singular variation in tic(k)-density."))), published: Set(chrono::Utc::now() - std::time::Duration::from_secs(60*i)), comments: Set(0), diff --git a/src/model/object.rs b/src/model/object.rs index 182dd93..09ba2ef 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -16,6 +16,7 @@ pub struct Model { pub shares: i64, pub comments: i64, pub context: Option, + pub in_reply_to: Option, pub cc: Audience, pub bcc: Audience, pub to: Audience, @@ -33,6 +34,7 @@ impl Model { summary: object.summary().map(|x| x.to_string()), content: object.content().map(|x| x.to_string()), context: object.context().id(), + in_reply_to: object.in_reply_to().id(), published: object.published().ok_or(super::FieldError("published"))?, comments: 0, likes: 0, diff --git a/src/model/user.rs b/src/model/user.rs index 695737a..be48ac5 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -24,7 +24,7 @@ pub struct Model { pub following_count: i64, pub followers_count: i64, - // pub statuses_count: i64, + pub statuses_count: i64, pub public_key: String, pub private_key: Option, @@ -57,6 +57,7 @@ impl Model { 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, + statuses_count: object.outbox().get().map(|o| o.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 })