Fiks: bevar originale timestamps ved warmup i stedet for ctx.timestamp

load_messages satte alle meldinger til nåtidspunkt. Parser nå PG-timestamp
til SpacetimeDB Timestamp slik at created_at bevares korrekt.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
vegard 2026-03-16 18:17:27 +01:00
parent 718f202014
commit b6fd6b5ef0

View file

@ -435,6 +435,9 @@ pub fn load_messages(
continue; continue;
} }
let created_at = parse_timestamp(item["created_at"].as_str().unwrap_or_default())
.unwrap_or(ctx.timestamp);
ctx.db.chat_message().insert(ChatMessage { ctx.db.chat_message().insert(ChatMessage {
id, id,
channel_id: item["channel_id"].as_str().unwrap_or_default().to_string(), channel_id: item["channel_id"].as_str().unwrap_or_default().to_string(),
@ -444,7 +447,7 @@ pub fn load_messages(
body: item["body"].as_str().unwrap_or_default().to_string(), body: item["body"].as_str().unwrap_or_default().to_string(),
message_type: item["message_type"].as_str().unwrap_or("text").to_string(), message_type: item["message_type"].as_str().unwrap_or("text").to_string(),
reply_to: item["reply_to"].as_str().unwrap_or_default().to_string(), reply_to: item["reply_to"].as_str().unwrap_or_default().to_string(),
created_at: ctx.timestamp, created_at,
metadata: item["metadata"].as_str().unwrap_or_default().to_string(), metadata: item["metadata"].as_str().unwrap_or_default().to_string(),
edited_at: item["edited_at"].as_str().unwrap_or_default().to_string(), edited_at: item["edited_at"].as_str().unwrap_or_default().to_string(),
}); });
@ -533,6 +536,21 @@ pub fn mark_synced(ctx: &ReducerContext, ids: Vec<u64>) -> Result<(), String> {
// === Hjelpefunksjoner === // === Hjelpefunksjoner ===
/// Parse en PG-tekststreng ("2026-03-15 23:57:11.677139+00") til SpacetimeDB Timestamp.
fn parse_timestamp(s: &str) -> Option<Timestamp> {
if s.is_empty() { return None; }
// Prøv standard ISO-parse først, deretter PG-formater
let dt = s.parse::<chrono::DateTime<chrono::FixedOffset>>()
.map(|d| d.to_utc())
.or_else(|_| {
chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S%.f")
.or_else(|_| chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S"))
.map(|naive| naive.and_utc())
})
.ok()?;
Some(Timestamp::from_micros_since_unix_epoch(dt.timestamp_micros()))
}
/// Formater en SpacetimeDB Timestamp til ISO 8601-streng. /// Formater en SpacetimeDB Timestamp til ISO 8601-streng.
fn format_timestamp(ts: Timestamp) -> String { fn format_timestamp(ts: Timestamp) -> String {
let duration = ts.to_duration_since_unix_epoch().unwrap_or_default(); let duration = ts.to_duration_since_unix_epoch().unwrap_or_default();