From 1d5b474c056f028da26ddd8b221bc21e55522e6d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 19 Mar 2024 10:50:14 +0100 Subject: [PATCH] Add `published` field to outgoing activities (ref #4529) --- Cargo.lock | 2 ++ crates/apub/src/activities/mod.rs | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 5ef691e26..0069d3510 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2633,6 +2633,7 @@ version = "0.19.3" dependencies = [ "activitypub_federation", "actix-web", + "anyhow", "chrono", "encoding", "enum-map", @@ -2645,6 +2646,7 @@ dependencies = [ "lemmy_db_views_moderator", "lemmy_utils", "mime", + "moka", "once_cell", "pretty_assertions", "regex", diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index 1472d6866..752568682 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -30,6 +30,7 @@ use activitypub_federation::{ traits::{ActivityHandler, Actor}, }; use anyhow::anyhow; +use chrono::{DateTime, Local, Utc}; use lemmy_api_common::{ context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, @@ -204,8 +205,9 @@ where { info!("Saving outgoing activity to queue {}", activity.id()); + let activity = WithPublished::new(activity); let form = SentActivityForm { - ap_id: activity.id().clone().into(), + ap_id: activity.inner.id().clone().into(), data: serde_json::to_value(activity)?, sensitive, send_inboxes: send_targets @@ -223,6 +225,24 @@ where Ok(()) } +/// Wrapper struct that adds `published` field with timestamp to outgoing activities. Important that +/// the timestamp includes milliseconds and timezone. +#[derive(Serialize)] +struct WithPublished { + published: DateTime, + #[serde(flatten)] + inner: T, +} + +impl WithPublished { + pub fn new(inner: T) -> WithPublished { + Self { + published: Local::now().into(), + inner, + } + } +} + pub async fn handle_outgoing_activities(context: Data) { while let Some(data) = ActivityChannel::retrieve_activity().await { if let Err(e) = match_outgoing_activities(data, &context.reset_request_count()).await {