From 5cfd16ea35c9d87253af670d19813ebae44c8d3c Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 6 Jun 2024 20:43:03 +0200 Subject: [PATCH] feat: added audience field so that we can see where lemmy posts are coming from --- upub/cli/src/faker.rs | 1 + upub/core/src/model/object.rs | 3 ++ upub/core/src/traits/normalize.rs | 1 + upub/migrations/src/lib.rs | 2 ++ ...001_create_actor_activity_object_tables.rs | 1 + ...20240606_000001_add_audience_to_objects.rs | 35 +++++++++++++++++++ 6 files changed, 43 insertions(+) create mode 100644 upub/migrations/src/m20240606_000001_add_audience_to_objects.rs diff --git a/upub/cli/src/faker.rs b/upub/cli/src/faker.rs index be35635e..bffbe4e4 100644 --- a/upub/cli/src/faker.rs +++ b/upub/cli/src/faker.rs @@ -83,6 +83,7 @@ pub async fn faker(ctx: upub::Context, count: i64) -> Result<(), sea_orm::DbErr> replies: Set(0), likes: Set(0), announces: Set(0), + audience: Set(None), to: Set(Audience(vec![apb::target::PUBLIC.to_string()])), bto: Set(Audience::default()), cc: Set(Audience(vec![])), diff --git a/upub/core/src/model/object.rs b/upub/core/src/model/object.rs index 80848705..c5bb10ea 100644 --- a/upub/core/src/model/object.rs +++ b/upub/core/src/model/object.rs @@ -28,6 +28,8 @@ pub struct Model { pub bcc: Audience, pub published: ChronoDateTimeUtc, pub updated: ChronoDateTimeUtc, + + pub audience: Option, // added with migration m20240606_000001 } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -195,6 +197,7 @@ impl Model { .set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone())) .set_published(Some(self.published)) .set_updated(Some(self.updated)) + .set_audience(apb::Node::maybe_link(self.audience)) .set_to(apb::Node::links(self.to.0.clone())) .set_bto(apb::Node::Empty) .set_cc(apb::Node::links(self.cc.0.clone())) diff --git a/upub/core/src/traits/normalize.rs b/upub/core/src/traits/normalize.rs index 1d39cb2d..57bd472a 100644 --- a/upub/core/src/traits/normalize.rs +++ b/upub/core/src/traits/normalize.rs @@ -231,6 +231,7 @@ impl AP { .map_or(0, |x| x.total_items().unwrap_or(0)) as i32, announces: object.shares().get() .map_or(0, |x| x.total_items().unwrap_or(0)) as i32, + audience: object.audience().id().str(), to: object.to().into(), bto: object.bto().into(), cc: object.cc().into(), diff --git a/upub/migrations/src/lib.rs b/upub/migrations/src/lib.rs index 0465e1b4..fe3fc45a 100644 --- a/upub/migrations/src/lib.rs +++ b/upub/migrations/src/lib.rs @@ -7,6 +7,7 @@ mod m20240524_000004_create_addressing_deliveries; mod m20240524_000005_create_attachments_tags_mentions; mod m20240529_000001_add_relation_unique_index; mod m20240605_000001_add_jobs_table; +mod m20240606_000001_add_audience_to_objects; pub struct Migrator; @@ -21,6 +22,7 @@ impl MigratorTrait for Migrator { Box::new(m20240524_000005_create_attachments_tags_mentions::Migration), Box::new(m20240529_000001_add_relation_unique_index::Migration), Box::new(m20240605_000001_add_jobs_table::Migration), + Box::new(m20240606_000001_add_audience_to_objects::Migration), ] } } diff --git a/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs b/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs index abe21fd2..fe993069 100644 --- a/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs +++ b/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs @@ -65,6 +65,7 @@ pub enum Objects { Bto, Published, Updated, + Audience, // added with migration m20240606_000001 } #[derive(DeriveIden)] diff --git a/upub/migrations/src/m20240606_000001_add_audience_to_objects.rs b/upub/migrations/src/m20240606_000001_add_audience_to_objects.rs new file mode 100644 index 00000000..834aea4d --- /dev/null +++ b/upub/migrations/src/m20240606_000001_add_audience_to_objects.rs @@ -0,0 +1,35 @@ +use sea_orm_migration::prelude::*; + +use crate::m20240524_000001_create_actor_activity_object_tables::Objects; + +#[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(Objects::Table) + .add_column(ColumnDef::new(Objects::Audience).string().null()) + .to_owned() + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Objects::Table) + .drop_column(Objects::Audience) + .to_owned() + ) + .await?; + + Ok(()) + } +}