From 41c0091e985fe935f4c82d8006f8bf2ba877af5a Mon Sep 17 00:00:00 2001 From: nullishamy Date: Thu, 1 May 2025 23:23:36 +0100 Subject: [PATCH] fix: datetime parsing --- ferri-main/src/types/get.rs | 25 ++++++++++++++++++++----- ferri-main/src/types/mod.rs | 16 +++++++++++++++- ferri-server/src/endpoints/inbox.rs | 2 ++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/ferri-main/src/types/get.rs b/ferri-main/src/types/get.rs index d4197e6..09ff5fe 100644 --- a/ferri-main/src/types/get.rs +++ b/ferri-main/src/types/get.rs @@ -1,14 +1,28 @@ use crate::types::{DbError, ObjectUri, ObjectUuid, db}; use chrono::{DateTime, NaiveDateTime, Utc}; use sqlx::SqliteConnection; -use tracing::info; +use tracing::{info, error}; const SQLITE_TIME_FMT: &str = "%Y-%m-%d %H:%M:%S"; fn parse_ts(ts: String) -> Option> { - NaiveDateTime::parse_from_str(&ts, SQLITE_TIME_FMT) - .ok() - .map(|nt| nt.and_utc()) + // Depending on how the TS is queried it may be naive (so get it back to utc) + // or it may have a timezone associated with it + let dt = NaiveDateTime::parse_from_str(&ts, SQLITE_TIME_FMT) + .map(|ndt| { + ndt.and_utc() + }) + .or_else(|_| { + DateTime::parse_from_rfc3339(&ts) + .map(|dt| dt.to_utc()) + }); + + if let Err(err) = dt { + error!("could not parse datetime {} ({}), db weirdness", ts, err); + return None + } + + Some(dt.unwrap()) } pub async fn user_by_id(id: ObjectUuid, conn: &mut SqliteConnection) -> Result { @@ -59,9 +73,10 @@ pub async fn user_by_id(id: ObjectUuid, conn: &mut SqliteConnection) -> Result), } +impl Default for ObjectContext { + fn default() -> Self { + ObjectContext::Str(String::new()) + } +} + #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] pub struct ObjectUri(pub String); @@ -48,6 +54,7 @@ impl ObjectUuid { #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] pub struct Object { #[serde(rename = "@context")] + #[serde(default)] pub context: ObjectContext, pub id: ObjectUri, } @@ -103,6 +110,8 @@ pub mod ap { pub struct MinimalActivity { #[serde(flatten)] pub obj: Object, + + #[serde(rename = "type")] pub ty: ActivityType, } @@ -123,6 +132,7 @@ pub mod ap { #[serde(flatten)] pub obj: Object, + #[serde(rename = "type")] pub ty: ActivityType, pub object: Post, @@ -139,6 +149,7 @@ pub mod ap { #[serde(flatten)] pub obj: Object, + #[serde(rename = "type")] pub ty: ActivityType, pub object: String, @@ -149,7 +160,8 @@ pub mod ap { pub struct AcceptActivity { #[serde(flatten)] pub obj: Object, - + + #[serde(rename = "type")] pub ty: ActivityType, pub object: String, @@ -161,6 +173,7 @@ pub mod ap { #[serde(flatten)] pub obj: Object, + #[serde(rename = "type")] pub ty: ActivityType, pub actor: String, @@ -175,6 +188,7 @@ pub mod ap { #[serde(flatten)] pub obj: Object, + #[serde(rename = "type")] pub ty: ActivityType, #[serde(rename = "published")] diff --git a/ferri-server/src/endpoints/inbox.rs b/ferri-server/src/endpoints/inbox.rs index 8145340..2dabede 100644 --- a/ferri-server/src/endpoints/inbox.rs +++ b/ferri-server/src/endpoints/inbox.rs @@ -327,6 +327,8 @@ async fn handle_boost_activity<'a>( ); let user_id = actor_user.id(); + info!("inserting post with id {} uri {}", base_id, uri); + sqlx::query!( " INSERT INTO post (id, uri, user_id, content, created_at, boosted_post_id)