From 7fa15a57a8dbf5f598ac5764fafeeec77ff88059 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 12 May 2024 01:58:51 +0200 Subject: [PATCH] feat: add url field to objects so that we can configure a frontend url and have posts redirect there when remote instances press "external source" --- src/cli/faker.rs | 1 + .../m20240512_000001_add_url_to_objects.rs | 40 +++++++++++++++++++ src/migrations/mod.rs | 2 + src/model/object.rs | 3 ++ src/server/outbox.rs | 13 +++++- 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/migrations/m20240512_000001_add_url_to_objects.rs diff --git a/src/cli/faker.rs b/src/cli/faker.rs index 4d9da60..5d8899e 100644 --- a/src/cli/faker.rs +++ b/src/cli/faker.rs @@ -83,6 +83,7 @@ pub async fn faker(ctx: crate::server::Context, count: u64) -> Result<(), sea_or bto: Set(Audience::default()), cc: Set(Audience(vec![])), bcc: Set(Audience::default()), + url: Set(None), sensitive: Set(false), }).exec(db).await?; diff --git a/src/migrations/m20240512_000001_add_url_to_objects.rs b/src/migrations/m20240512_000001_add_url_to_objects.rs new file mode 100644 index 0000000..373549a --- /dev/null +++ b/src/migrations/m20240512_000001_add_url_to_objects.rs @@ -0,0 +1,40 @@ +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(Objects::Table) + .add_column(ColumnDef::new(Objects::Url).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::Url) + .to_owned() + ) + .await?; + + Ok(()) + } +} + +#[derive(DeriveIden)] +enum Objects { + Table, + Url, +} + diff --git a/src/migrations/mod.rs b/src/migrations/mod.rs index c5b4dae..8468333 100644 --- a/src/migrations/mod.rs +++ b/src/migrations/mod.rs @@ -14,6 +14,7 @@ mod m20240421_000001_add_attachments; mod m20240424_000001_add_sensitive_field; mod m20240429_000001_add_relays_table; mod m20240502_000001_add_object_updated; +mod m20240512_000001_add_url_to_objects; pub struct Migrator; @@ -35,6 +36,7 @@ impl MigratorTrait for Migrator { Box::new(m20240424_000001_add_sensitive_field::Migration), Box::new(m20240429_000001_add_relays_table::Migration), Box::new(m20240502_000001_add_object_updated::Migration), + Box::new(m20240512_000001_add_url_to_objects::Migration), ] } } diff --git a/src/model/object.rs b/src/model/object.rs index af2a32b..61d2009 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -24,6 +24,7 @@ pub struct Model { pub bcc: Audience, pub to: Audience, pub bto: Audience, + pub url: Option, pub published: ChronoDateTimeUtc, pub updated: Option, @@ -43,6 +44,7 @@ impl Model { in_reply_to: object.in_reply_to().id(), published: object.published().ok_or(super::FieldError("published"))?, updated: object.updated(), + url: object.url().id(), comments: object.replies().get() .map_or(0, |x| x.total_items().unwrap_or(0)) as i64, likes: object.likes().get() @@ -74,6 +76,7 @@ impl Model { .set_bto(apb::Node::Empty) .set_cc(apb::Node::links(self.cc.0.clone())) .set_bcc(apb::Node::Empty) + .set_url(apb::Node::maybe_link(self.url)) .set_sensitive(Some(self.sensitive)) .set_shares(apb::Node::object( serde_json::Value::new_object() diff --git a/src/server/outbox.rs b/src/server/outbox.rs index 4440817..3766e75 100644 --- a/src/server/outbox.rs +++ b/src/server/outbox.rs @@ -14,7 +14,8 @@ impl apb::server::Outbox for Context { type Activity = serde_json::Value; async fn create_note(&self, uid: String, object: serde_json::Value) -> crate::Result { - let oid = self.oid(uuid::Uuid::new_v4().to_string()); + let raw_oid = uuid::Uuid::new_v4().to_string(); + let oid = self.oid(raw_oid.clone()); let aid = self.aid(uuid::Uuid::new_v4().to_string()); let activity_targets = object.addressed(); let mut object_model = model::object::Model::new( @@ -35,6 +36,10 @@ impl apb::server::Outbox for Context { } let reply_to = object_model.in_reply_to.clone(); + if let Some(fe_url) = &self.cfg().instance.frontend { + object_model.url = Some(format!("{fe_url}/objects/{raw_oid}")); + } + let activity_model = model::activity::Model { id: aid.clone(), activity_type: apb::ActivityType::Create, @@ -75,7 +80,8 @@ impl apb::server::Outbox for Context { return Err(UpubError::bad_request()); }; - let oid = self.oid(uuid::Uuid::new_v4().to_string()); + let raw_oid = uuid::Uuid::new_v4().to_string(); + let oid = self.oid(raw_oid.clone()); let aid = self.aid(uuid::Uuid::new_v4().to_string()); let activity_targets = activity.addressed(); let mut object_model = model::object::Model::new( @@ -106,6 +112,9 @@ impl apb::server::Outbox for Context { (_, Some(_)) => {}, // leave it as set by user } let reply_to = object_model.in_reply_to.clone(); + if let Some(fe_url) = &self.cfg().instance.frontend { + object_model.url = Some(format!("{fe_url}/objects/{raw_oid}")); + } model::object::Entity::insert(object_model.into_active_model()) .exec(self.db()).await?;