From 32e3cd55741cebc6fc0105dd1f105dc4598824e1 Mon Sep 17 00:00:00 2001 From: phiresky Date: Wed, 26 Jul 2023 16:17:39 +0200 Subject: [PATCH] make time-zone aware (#62) * make time-zone aware. * format and revert debug * better test log * empty --- docs/03_federating_users.md | 4 ++-- docs/10_fetching_objects_with_unknown_type.md | 2 +- examples/live_federation/objects/person.rs | 10 +++++----- examples/local_federation/objects/person.rs | 10 +++++----- src/fetch/object_id.rs | 10 +++++----- src/fetch/webfinger.rs | 12 +++++------- src/traits.rs | 6 +++--- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/docs/03_federating_users.md b/docs/03_federating_users.md index a45eac7..7882af1 100644 --- a/docs/03_federating_users.md +++ b/docs/03_federating_users.md @@ -65,7 +65,7 @@ Besides we also need a second struct to represent the data which gets stored in ```rust # use url::Url; -# use chrono::NaiveDateTime; +# use chrono::{DateTime, Utc}; pub struct DbUser { pub id: i32, @@ -79,7 +79,7 @@ pub struct DbUser { pub local: bool, pub public_key: String, pub private_key: Option, - pub last_refreshed_at: NaiveDateTime, + pub last_refreshed_at: DateTime, } ``` diff --git a/docs/10_fetching_objects_with_unknown_type.md b/docs/10_fetching_objects_with_unknown_type.md index f231200..96392d4 100644 --- a/docs/10_fetching_objects_with_unknown_type.md +++ b/docs/10_fetching_objects_with_unknown_type.md @@ -19,7 +19,7 @@ pub enum SearchableDbObjects { Post(DbPost) } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Debug)] #[serde(untagged)] pub enum SearchableObjects { Person(Person), diff --git a/examples/live_federation/objects/person.rs b/examples/live_federation/objects/person.rs index 7d30320..d9439ea 100644 --- a/examples/live_federation/objects/person.rs +++ b/examples/live_federation/objects/person.rs @@ -7,7 +7,7 @@ use activitypub_federation::{ protocol::{public_key::PublicKey, verification::verify_domains_match}, traits::{ActivityHandler, Actor, Object}, }; -use chrono::{Local, NaiveDateTime}; +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; use url::Url; @@ -21,7 +21,7 @@ pub struct DbUser { pub public_key: String, // exists only for local users pub private_key: Option, - last_refreshed_at: NaiveDateTime, + last_refreshed_at: DateTime, pub followers: Vec, pub local: bool, } @@ -45,7 +45,7 @@ impl DbUser { inbox, public_key: keypair.public_key, private_key: Some(keypair.private_key), - last_refreshed_at: Local::now().naive_local(), + last_refreshed_at: Utc::now(), followers: vec![], local: true, }) @@ -69,7 +69,7 @@ impl Object for DbUser { type Kind = Person; type Error = Error; - fn last_refreshed_at(&self) -> Option { + fn last_refreshed_at(&self) -> Option> { Some(self.last_refreshed_at) } @@ -114,7 +114,7 @@ impl Object for DbUser { inbox: json.inbox, public_key: json.public_key.public_key_pem, private_key: None, - last_refreshed_at: Local::now().naive_local(), + last_refreshed_at: Utc::now(), followers: vec![], local: false, }) diff --git a/examples/local_federation/objects/person.rs b/examples/local_federation/objects/person.rs index 5579af0..efdcc70 100644 --- a/examples/local_federation/objects/person.rs +++ b/examples/local_federation/objects/person.rs @@ -14,7 +14,7 @@ use activitypub_federation::{ protocol::{context::WithContext, public_key::PublicKey, verification::verify_domains_match}, traits::{ActivityHandler, Actor, Object}, }; -use chrono::{Local, NaiveDateTime}; +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; use url::Url; @@ -28,7 +28,7 @@ pub struct DbUser { public_key: String, // exists only for local users private_key: Option, - last_refreshed_at: NaiveDateTime, + last_refreshed_at: DateTime, pub followers: Vec, pub local: bool, } @@ -54,7 +54,7 @@ impl DbUser { inbox, public_key: keypair.public_key, private_key: Some(keypair.private_key), - last_refreshed_at: Local::now().naive_local(), + last_refreshed_at: Utc::now(), followers: vec![], local: true, }) @@ -124,7 +124,7 @@ impl Object for DbUser { type Kind = Person; type Error = Error; - fn last_refreshed_at(&self) -> Option { + fn last_refreshed_at(&self) -> Option> { Some(self.last_refreshed_at) } @@ -166,7 +166,7 @@ impl Object for DbUser { inbox: json.inbox, public_key: json.public_key.public_key_pem, private_key: None, - last_refreshed_at: Local::now().naive_local(), + last_refreshed_at: Utc::now(), followers: vec![], local: false, }; diff --git a/src/fetch/object_id.rs b/src/fetch/object_id.rs index 08c1d07..29d5507 100644 --- a/src/fetch/object_id.rs +++ b/src/fetch/object_id.rs @@ -1,6 +1,6 @@ use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Object}; use anyhow::anyhow; -use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc}; +use chrono::{DateTime, Duration as ChronoDuration, Utc}; use serde::{Deserialize, Serialize}; use std::{ fmt::{Debug, Display, Formatter}, @@ -180,14 +180,14 @@ static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 20; /// Determines when a remote actor should be refetched from its instance. In release builds, this is /// `ACTOR_REFETCH_INTERVAL_SECONDS` after the last refetch, in debug builds /// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`. -fn should_refetch_object(last_refreshed: NaiveDateTime) -> bool { +fn should_refetch_object(last_refreshed: DateTime) -> bool { let update_interval = if cfg!(debug_assertions) { // avoid infinite loop when fetching community outbox ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG) } else { ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS) }; - let refresh_limit = Utc::now().naive_utc() - update_interval; + let refresh_limit = Utc::now() - update_interval; last_refreshed.lt(&refresh_limit) } @@ -259,10 +259,10 @@ pub mod tests { #[test] fn test_should_refetch_object() { - let one_second_ago = Utc::now().naive_utc() - ChronoDuration::seconds(1); + let one_second_ago = Utc::now() - ChronoDuration::seconds(1); assert!(!should_refetch_object(one_second_ago)); - let two_days_ago = Utc::now().naive_utc() - ChronoDuration::days(2); + let two_days_ago = Utc::now() - ChronoDuration::days(2); assert!(should_refetch_object(two_days_ago)); } } diff --git a/src/fetch/webfinger.rs b/src/fetch/webfinger.rs index 5a8a7ce..fc2bd8d 100644 --- a/src/fetch/webfinger.rs +++ b/src/fetch/webfinger.rs @@ -221,7 +221,7 @@ mod tests { }; #[tokio::test] - async fn test_webfinger() { + async fn test_webfinger() -> Result<(), Error> { let config = FederationConfig::builder() .domain("example.com") .app_data(DbConnection) @@ -229,12 +229,10 @@ mod tests { .await .unwrap(); let data = config.to_request_data(); - let res = - webfinger_resolve_actor::("LemmyDev@mastodon.social", &data) - .await; - assert!(res.is_ok()); + + webfinger_resolve_actor::("LemmyDev@mastodon.social", &data).await?; // poa.st is as of 2023-07-14 the largest Pleroma instance - let res = webfinger_resolve_actor::("graf@poa.st", &data).await; - assert!(res.is_ok()); + webfinger_resolve_actor::("graf@poa.st", &data).await?; + Ok(()) } } diff --git a/src/traits.rs b/src/traits.rs index c6b329f..e4ed6d0 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -2,7 +2,7 @@ use crate::{config::Data, protocol::public_key::PublicKey}; use async_trait::async_trait; -use chrono::NaiveDateTime; +use chrono::{DateTime, Utc}; use serde::Deserialize; use std::{fmt::Debug, ops::Deref}; use url::Url; @@ -11,7 +11,7 @@ use url::Url; /// /// ``` /// # use activitystreams_kinds::{object::NoteType, public}; -/// # use chrono::{Local, NaiveDateTime}; +/// # use chrono::{Local, DateTime, Utc}; /// # use serde::{Deserialize, Serialize}; /// # use url::Url; /// # use activitypub_federation::protocol::{public_key::PublicKey, helpers::deserialize_one_or_many}; @@ -112,7 +112,7 @@ pub trait Object: Sized + Debug { /// /// The object is refetched if `last_refreshed_at` value is more than 24 hours ago. In debug /// mode this is reduced to 20 seconds. - fn last_refreshed_at(&self) -> Option { + fn last_refreshed_at(&self) -> Option> { None }