make time-zone aware (#62)

* make time-zone aware.

* format and revert debug

* better test log

* empty
This commit is contained in:
phiresky 2023-07-26 16:17:39 +02:00 committed by GitHub
parent 61085a643f
commit 32e3cd5574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 28 deletions

View file

@ -65,7 +65,7 @@ Besides we also need a second struct to represent the data which gets stored in
```rust ```rust
# use url::Url; # use url::Url;
# use chrono::NaiveDateTime; # use chrono::{DateTime, Utc};
pub struct DbUser { pub struct DbUser {
pub id: i32, pub id: i32,
@ -79,7 +79,7 @@ pub struct DbUser {
pub local: bool, pub local: bool,
pub public_key: String, pub public_key: String,
pub private_key: Option<String>, pub private_key: Option<String>,
pub last_refreshed_at: NaiveDateTime, pub last_refreshed_at: DateTime<Utc>,
} }
``` ```

View file

@ -19,7 +19,7 @@ pub enum SearchableDbObjects {
Post(DbPost) Post(DbPost)
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)] #[serde(untagged)]
pub enum SearchableObjects { pub enum SearchableObjects {
Person(Person), Person(Person),

View file

@ -7,7 +7,7 @@ use activitypub_federation::{
protocol::{public_key::PublicKey, verification::verify_domains_match}, protocol::{public_key::PublicKey, verification::verify_domains_match},
traits::{ActivityHandler, Actor, Object}, traits::{ActivityHandler, Actor, Object},
}; };
use chrono::{Local, NaiveDateTime}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use url::Url; use url::Url;
@ -21,7 +21,7 @@ pub struct DbUser {
pub public_key: String, pub public_key: String,
// exists only for local users // exists only for local users
pub private_key: Option<String>, pub private_key: Option<String>,
last_refreshed_at: NaiveDateTime, last_refreshed_at: DateTime<Utc>,
pub followers: Vec<Url>, pub followers: Vec<Url>,
pub local: bool, pub local: bool,
} }
@ -45,7 +45,7 @@ impl DbUser {
inbox, inbox,
public_key: keypair.public_key, public_key: keypair.public_key,
private_key: Some(keypair.private_key), private_key: Some(keypair.private_key),
last_refreshed_at: Local::now().naive_local(), last_refreshed_at: Utc::now(),
followers: vec![], followers: vec![],
local: true, local: true,
}) })
@ -69,7 +69,7 @@ impl Object for DbUser {
type Kind = Person; type Kind = Person;
type Error = Error; type Error = Error;
fn last_refreshed_at(&self) -> Option<NaiveDateTime> { fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
Some(self.last_refreshed_at) Some(self.last_refreshed_at)
} }
@ -114,7 +114,7 @@ impl Object for DbUser {
inbox: json.inbox, inbox: json.inbox,
public_key: json.public_key.public_key_pem, public_key: json.public_key.public_key_pem,
private_key: None, private_key: None,
last_refreshed_at: Local::now().naive_local(), last_refreshed_at: Utc::now(),
followers: vec![], followers: vec![],
local: false, local: false,
}) })

View file

@ -14,7 +14,7 @@ use activitypub_federation::{
protocol::{context::WithContext, public_key::PublicKey, verification::verify_domains_match}, protocol::{context::WithContext, public_key::PublicKey, verification::verify_domains_match},
traits::{ActivityHandler, Actor, Object}, traits::{ActivityHandler, Actor, Object},
}; };
use chrono::{Local, NaiveDateTime}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use url::Url; use url::Url;
@ -28,7 +28,7 @@ pub struct DbUser {
public_key: String, public_key: String,
// exists only for local users // exists only for local users
private_key: Option<String>, private_key: Option<String>,
last_refreshed_at: NaiveDateTime, last_refreshed_at: DateTime<Utc>,
pub followers: Vec<Url>, pub followers: Vec<Url>,
pub local: bool, pub local: bool,
} }
@ -54,7 +54,7 @@ impl DbUser {
inbox, inbox,
public_key: keypair.public_key, public_key: keypair.public_key,
private_key: Some(keypair.private_key), private_key: Some(keypair.private_key),
last_refreshed_at: Local::now().naive_local(), last_refreshed_at: Utc::now(),
followers: vec![], followers: vec![],
local: true, local: true,
}) })
@ -124,7 +124,7 @@ impl Object for DbUser {
type Kind = Person; type Kind = Person;
type Error = Error; type Error = Error;
fn last_refreshed_at(&self) -> Option<NaiveDateTime> { fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
Some(self.last_refreshed_at) Some(self.last_refreshed_at)
} }
@ -166,7 +166,7 @@ impl Object for DbUser {
inbox: json.inbox, inbox: json.inbox,
public_key: json.public_key.public_key_pem, public_key: json.public_key.public_key_pem,
private_key: None, private_key: None,
last_refreshed_at: Local::now().naive_local(), last_refreshed_at: Utc::now(),
followers: vec![], followers: vec![],
local: false, local: false,
}; };

View file

@ -1,6 +1,6 @@
use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Object}; use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Object};
use anyhow::anyhow; use anyhow::anyhow;
use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc}; use chrono::{DateTime, Duration as ChronoDuration, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
fmt::{Debug, Display, Formatter}, 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 /// 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` after the last refetch, in debug builds
/// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`. /// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`.
fn should_refetch_object(last_refreshed: NaiveDateTime) -> bool { fn should_refetch_object(last_refreshed: DateTime<Utc>) -> bool {
let update_interval = if cfg!(debug_assertions) { let update_interval = if cfg!(debug_assertions) {
// avoid infinite loop when fetching community outbox // avoid infinite loop when fetching community outbox
ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG) ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG)
} else { } else {
ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS) 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) last_refreshed.lt(&refresh_limit)
} }
@ -259,10 +259,10 @@ pub mod tests {
#[test] #[test]
fn test_should_refetch_object() { 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)); 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)); assert!(should_refetch_object(two_days_ago));
} }
} }

View file

@ -221,7 +221,7 @@ mod tests {
}; };
#[tokio::test] #[tokio::test]
async fn test_webfinger() { async fn test_webfinger() -> Result<(), Error> {
let config = FederationConfig::builder() let config = FederationConfig::builder()
.domain("example.com") .domain("example.com")
.app_data(DbConnection) .app_data(DbConnection)
@ -229,12 +229,10 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let data = config.to_request_data(); let data = config.to_request_data();
let res =
webfinger_resolve_actor::<DbConnection, DbUser>("LemmyDev@mastodon.social", &data) webfinger_resolve_actor::<DbConnection, DbUser>("LemmyDev@mastodon.social", &data).await?;
.await;
assert!(res.is_ok());
// poa.st is as of 2023-07-14 the largest Pleroma instance // poa.st is as of 2023-07-14 the largest Pleroma instance
let res = webfinger_resolve_actor::<DbConnection, DbUser>("graf@poa.st", &data).await; webfinger_resolve_actor::<DbConnection, DbUser>("graf@poa.st", &data).await?;
assert!(res.is_ok()); Ok(())
} }
} }

View file

@ -2,7 +2,7 @@
use crate::{config::Data, protocol::public_key::PublicKey}; use crate::{config::Data, protocol::public_key::PublicKey};
use async_trait::async_trait; use async_trait::async_trait;
use chrono::NaiveDateTime; use chrono::{DateTime, Utc};
use serde::Deserialize; use serde::Deserialize;
use std::{fmt::Debug, ops::Deref}; use std::{fmt::Debug, ops::Deref};
use url::Url; use url::Url;
@ -11,7 +11,7 @@ use url::Url;
/// ///
/// ``` /// ```
/// # use activitystreams_kinds::{object::NoteType, public}; /// # use activitystreams_kinds::{object::NoteType, public};
/// # use chrono::{Local, NaiveDateTime}; /// # use chrono::{Local, DateTime, Utc};
/// # use serde::{Deserialize, Serialize}; /// # use serde::{Deserialize, Serialize};
/// # use url::Url; /// # use url::Url;
/// # use activitypub_federation::protocol::{public_key::PublicKey, helpers::deserialize_one_or_many}; /// # 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 /// The object is refetched if `last_refreshed_at` value is more than 24 hours ago. In debug
/// mode this is reduced to 20 seconds. /// mode this is reduced to 20 seconds.
fn last_refreshed_at(&self) -> Option<NaiveDateTime> { fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
None None
} }