Skip to content

Commit

Permalink
Use UTC everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Aug 23, 2024
1 parent 62a2489 commit be2a51f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/domain/follow.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use chrono::{DateTime, FixedOffset};
use chrono::{DateTime, Utc};
use nostr_sdk::prelude::*;

#[derive(Debug, Clone)]
pub struct Follow {
pub followee: PublicKey,
pub follower: PublicKey,
pub updated_at: DateTime<FixedOffset>,
pub updated_at: DateTime<Utc>,
#[allow(unused)]
pub created_at: DateTime<FixedOffset>,
pub created_at: DateTime<Utc>,
}
17 changes: 7 additions & 10 deletions src/follows_differ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
domain::{follow::Follow, follow_change::FollowChange},
worker_pool::{WorkerTask, WorkerTaskItem},
};
use chrono::{DateTime, FixedOffset};
use chrono::{DateTime, Utc};
use nostr_sdk::prelude::*;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -35,12 +35,6 @@ where
}
}

fn convert_timestamp(&self, timestamp: Timestamp) -> Result<DateTime<FixedOffset>> {
DateTime::from_timestamp(timestamp.as_u64() as i64, 0)
.map(|dt| dt.fixed_offset())
.ok_or_else(|| "Failed to convert timestamp to datetime".into())
}

/// Initializes a structure that holds the differences between the stored
/// follows and the latest contact list.
async fn initialize_follows_diff(
Expand Down Expand Up @@ -84,12 +78,11 @@ where
}
}
}

async fn process_follows_diff(
&self,
follows_diff: HashMap<PublicKey, FollowsDiff>,
follower: &PublicKey,
event_created_at: DateTime<FixedOffset>,
event_created_at: DateTime<Utc>,
) -> Result<(usize, usize, usize)> {
let mut followed_counter = 0;
let mut unfollowed_counter = 0;
Expand Down Expand Up @@ -179,7 +172,7 @@ where
let WorkerTaskItem { item: event } = worker_task_item;
let follower = event.pubkey;

let event_created_at = self.convert_timestamp(event.created_at)?;
let event_created_at = convert_timestamp(event.created_at.as_u64())?;

// Get the stored follows and the latest update time from the database
let (mut follows_diff, maybe_latest_stored_updated_at) =
Expand Down Expand Up @@ -218,6 +211,10 @@ where
}
}

fn convert_timestamp(timestamp: u64) -> Result<DateTime<Utc>> {
DateTime::<Utc>::from_timestamp(timestamp as i64, 0).ok_or("Invalid timestamp".into())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 4 additions & 4 deletions src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::panic;

use crate::domain::follow::Follow;
use chrono::{DateTime, FixedOffset};
use chrono::{DateTime, Utc};
use neo4rs::{query, Graph};
use nostr_sdk::prelude::PublicKey;
use thiserror::Error;
Expand Down Expand Up @@ -107,7 +107,7 @@ impl RepoTrait for Repo {
"#;

let query = query(statement)
.param("updated_at", follow.updated_at)
.param("updated_at", follow.updated_at.naive_utc())
.param("followee_val", follow.followee.to_hex())
.param("follower_val", follow.follower.to_hex());

Expand Down Expand Up @@ -163,10 +163,10 @@ impl RepoTrait for Repo {
.get::<String>("follower")
.map_err(RepoError::Deserialization)?;
let updated_at = row
.get::<DateTime<FixedOffset>>("updated_at")
.get::<DateTime<Utc>>("updated_at")
.map_err(RepoError::Deserialization)?;
let created_at = row
.get::<DateTime<FixedOffset>>("created_at")
.get::<DateTime<Utc>>("created_at")
.map_err(RepoError::Deserialization)?;

follows.push(Follow {
Expand Down

0 comments on commit be2a51f

Please sign in to comment.