fix: add updated field on objects and set it

This commit is contained in:
əlemi 2024-05-02 15:15:16 +02:00
parent 30219c7fe6
commit ee8ab38570
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 49 additions and 0 deletions

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::Updated).date_time().null())
.to_owned()
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Objects::Table)
.drop_column(Objects::Updated)
.to_owned()
)
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Objects {
Table,
Updated,
}

View file

@ -13,6 +13,7 @@ mod m20240418_000001_add_statuses_and_reply_to;
mod m20240421_000001_add_attachments; 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;
pub struct Migrator; pub struct Migrator;
@ -33,6 +34,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240421_000001_add_attachments::Migration), Box::new(m20240421_000001_add_attachments::Migration),
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),
] ]
} }
} }

View file

@ -25,6 +25,7 @@ pub struct Model {
pub to: Audience, pub to: Audience,
pub bto: Audience, pub bto: Audience,
pub published: ChronoDateTimeUtc, pub published: ChronoDateTimeUtc,
pub updated: Option<ChronoDateTimeUtc>,
pub sensitive: bool, pub sensitive: bool,
} }
@ -41,6 +42,7 @@ impl Model {
context: object.context().id(), context: object.context().id(),
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(),
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()
@ -67,6 +69,7 @@ impl Model {
.set_context(apb::Node::maybe_link(self.context.clone())) .set_context(apb::Node::maybe_link(self.context.clone()))
.set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone())) .set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone()))
.set_published(Some(self.published)) .set_published(Some(self.published))
.set_updated(self.updated)
.set_to(apb::Node::links(self.to.0.clone())) .set_to(apb::Node::links(self.to.0.clone()))
.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()))

View file

@ -252,6 +252,7 @@ impl apb::server::Inbox for Context {
// back up to serde_json::Value because impl Object != impl Actor // back up to serde_json::Value because impl Object != impl Actor
let actor_model = model::user::Model::new(&object_node)?; let actor_model = model::user::Model::new(&object_node)?;
let mut update_model = actor_model.into_active_model(); let mut update_model = actor_model.into_active_model();
update_model.updated = sea_orm::Set(chrono::Utc::now());
update_model.reset(model::user::Column::Name); update_model.reset(model::user::Column::Name);
update_model.reset(model::user::Column::Summary); update_model.reset(model::user::Column::Summary);
update_model.reset(model::user::Column::Image); update_model.reset(model::user::Column::Image);
@ -262,6 +263,7 @@ impl apb::server::Inbox for Context {
Some(apb::ObjectType::Note) => { Some(apb::ObjectType::Note) => {
let object_model = model::object::Model::new(&object_node)?; let object_model = model::object::Model::new(&object_node)?;
let mut update_model = object_model.into_active_model(); let mut update_model = object_model.into_active_model();
update_model.updated = sea_orm::Set(Some(chrono::Utc::now()));
update_model.reset(model::object::Column::Name); update_model.reset(model::object::Column::Name);
update_model.reset(model::object::Column::Summary); update_model.reset(model::object::Column::Summary);
update_model.reset(model::object::Column::Content); update_model.reset(model::object::Column::Content);

View file

@ -327,6 +327,7 @@ impl apb::server::Outbox for Context {
if actor_model.icon.is_none() { actor_model.icon = old_actor_model.icon } if actor_model.icon.is_none() { actor_model.icon = old_actor_model.icon }
let mut update_model = actor_model.into_active_model(); let mut update_model = actor_model.into_active_model();
update_model.updated = sea_orm::Set(chrono::Utc::now());
update_model.reset(model::user::Column::Name); update_model.reset(model::user::Column::Name);
update_model.reset(model::user::Column::Summary); update_model.reset(model::user::Column::Summary);
update_model.reset(model::user::Column::Image); update_model.reset(model::user::Column::Image);
@ -357,6 +358,7 @@ impl apb::server::Outbox for Context {
if object_model.content.is_none() { object_model.content = old_object_model.content } if object_model.content.is_none() { object_model.content = old_object_model.content }
let mut update_model = object_model.into_active_model(); let mut update_model = object_model.into_active_model();
update_model.updated = sea_orm::Set(Some(chrono::Utc::now()));
update_model.reset(model::object::Column::Name); update_model.reset(model::object::Column::Name);
update_model.reset(model::object::Column::Summary); update_model.reset(model::object::Column::Summary);
update_model.reset(model::object::Column::Content); update_model.reset(model::object::Column::Content);