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"
This commit is contained in:
əlemi 2024-05-12 01:58:51 +02:00
parent aa19211b8e
commit 7fa15a57a8
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 57 additions and 2 deletions

View file

@ -83,6 +83,7 @@ pub async fn faker(ctx: crate::server::Context, count: u64) -> Result<(), sea_or
bto: Set(Audience::default()), bto: Set(Audience::default()),
cc: Set(Audience(vec![])), cc: Set(Audience(vec![])),
bcc: Set(Audience::default()), bcc: Set(Audience::default()),
url: Set(None),
sensitive: Set(false), sensitive: Set(false),
}).exec(db).await?; }).exec(db).await?;

View file

@ -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,
}

View file

@ -14,6 +14,7 @@ mod m20240421_000001_add_attachments;
mod m20240424_000001_add_sensitive_field; mod m20240424_000001_add_sensitive_field;
mod m20240429_000001_add_relays_table; mod m20240429_000001_add_relays_table;
mod m20240502_000001_add_object_updated; mod m20240502_000001_add_object_updated;
mod m20240512_000001_add_url_to_objects;
pub struct Migrator; pub struct Migrator;
@ -35,6 +36,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240424_000001_add_sensitive_field::Migration), Box::new(m20240424_000001_add_sensitive_field::Migration),
Box::new(m20240429_000001_add_relays_table::Migration), Box::new(m20240429_000001_add_relays_table::Migration),
Box::new(m20240502_000001_add_object_updated::Migration), Box::new(m20240502_000001_add_object_updated::Migration),
Box::new(m20240512_000001_add_url_to_objects::Migration),
] ]
} }
} }

View file

@ -24,6 +24,7 @@ pub struct Model {
pub bcc: Audience, pub bcc: Audience,
pub to: Audience, pub to: Audience,
pub bto: Audience, pub bto: Audience,
pub url: Option<String>,
pub published: ChronoDateTimeUtc, pub published: ChronoDateTimeUtc,
pub updated: Option<ChronoDateTimeUtc>, pub updated: Option<ChronoDateTimeUtc>,
@ -43,6 +44,7 @@ impl Model {
in_reply_to: object.in_reply_to().id(), in_reply_to: object.in_reply_to().id(),
published: object.published().ok_or(super::FieldError("published"))?, published: object.published().ok_or(super::FieldError("published"))?,
updated: object.updated(), updated: object.updated(),
url: object.url().id(),
comments: object.replies().get() comments: object.replies().get()
.map_or(0, |x| x.total_items().unwrap_or(0)) as i64, .map_or(0, |x| x.total_items().unwrap_or(0)) as i64,
likes: object.likes().get() likes: object.likes().get()
@ -74,6 +76,7 @@ impl Model {
.set_bto(apb::Node::Empty) .set_bto(apb::Node::Empty)
.set_cc(apb::Node::links(self.cc.0.clone())) .set_cc(apb::Node::links(self.cc.0.clone()))
.set_bcc(apb::Node::Empty) .set_bcc(apb::Node::Empty)
.set_url(apb::Node::maybe_link(self.url))
.set_sensitive(Some(self.sensitive)) .set_sensitive(Some(self.sensitive))
.set_shares(apb::Node::object( .set_shares(apb::Node::object(
serde_json::Value::new_object() serde_json::Value::new_object()

View file

@ -14,7 +14,8 @@ impl apb::server::Outbox for Context {
type Activity = serde_json::Value; type Activity = serde_json::Value;
async fn create_note(&self, uid: String, object: serde_json::Value) -> crate::Result<String> { async fn create_note(&self, uid: String, object: serde_json::Value) -> crate::Result<String> {
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 aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = object.addressed(); let activity_targets = object.addressed();
let mut object_model = model::object::Model::new( 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(); 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 { let activity_model = model::activity::Model {
id: aid.clone(), id: aid.clone(),
activity_type: apb::ActivityType::Create, activity_type: apb::ActivityType::Create,
@ -75,7 +80,8 @@ impl apb::server::Outbox for Context {
return Err(UpubError::bad_request()); 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 aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = activity.addressed(); let activity_targets = activity.addressed();
let mut object_model = model::object::Model::new( 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 (_, Some(_)) => {}, // leave it as set by user
} }
let reply_to = object_model.in_reply_to.clone(); 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()) model::object::Entity::insert(object_model.into_active_model())
.exec(self.db()).await?; .exec(self.db()).await?;