Normalize the legacy session projector's seconds/milliseconds mix-up in the returned read model only. Stored KG and provenance bytes stay intact.
(bytes: &[u8])
| 506 | /// Normalize the legacy session projector's seconds/milliseconds mix-up in |
| 507 | /// the returned read model only. Stored KG and provenance bytes stay intact. |
| 508 | pub(crate) fn from_json(bytes: &[u8]) -> Result<Self, serde_json::Error> { |
| 509 | let mut node: Self = serde_json::from_slice(bytes)?; |
| 510 | if node.source == "session_ledger" && node.has_type(crate::kg::ontology::Class::SessionTurn) |
| 511 | { |
| 512 | if let Some(created_at) = node.metadata.as_mut().and_then(|m| m.get_mut("created_at")) { |
| 513 | if let Some(legacy) = created_at |
| 514 | .as_str() |
| 515 | .and_then(|value| { |
| 516 | chrono::DateTime::parse_from_rfc3339(value) |
| 517 | // Chrono emits expanded years for the erroneous |
| 518 | // date, but its strict RFC-3339 parser rejects them. |
| 519 | .or_else(|_| { |
| 520 | chrono::DateTime::parse_from_str(value, "%Y-%m-%dT%H:%M:%S%.f%:z") |
| 521 | }) |
| 522 | .ok() |
| 523 | }) |
| 524 | .filter(|value| { |
| 525 | value.timestamp().unsigned_abs() |
| 526 | >= crate::types::timestamp::MILLISECOND_EPOCH_THRESHOLD |
| 527 | }) |
| 528 | { |
| 529 | if let Some(corrected) = |
| 530 | crate::types::timestamp::epoch_datetime(legacy.timestamp()) |
| 531 | { |
| 532 | *created_at = serde_json::json!(corrected.to_rfc3339()); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | Ok(node) |
| 538 | } |
| 539 | |
| 540 | pub fn has_type(&self, class: crate::kg::ontology::Class) -> bool { |
| 541 | self.types.iter().any(|iri| iri == class.iri()) |