Skip to content

Commit

Permalink
Follows differ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Aug 23, 2024
1 parent 9ab8dbc commit 0fa5fca
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 27 deletions.
20 changes: 18 additions & 2 deletions src/domain/follow_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub enum ChangeType {
Followed,
Unfollowed,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct FollowChange {
pub change_type: ChangeType,
Expand Down Expand Up @@ -75,3 +75,19 @@ impl fmt::Display for FollowChange {
)
}
}

impl fmt::Debug for FollowChange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{}{} at {}",
&self.follower.to_hex()[..3],
match self.change_type {
ChangeType::Followed => "--->",
ChangeType::Unfollowed => "-x->",
},
&self.followee.to_hex()[..3],
self.at
)
}
}
11 changes: 7 additions & 4 deletions src/follow_change_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::domain::follow_change::FollowChange;
use crate::google_publisher::GooglePublisher;
use crate::refresh_friendly_id::refresh_friendly_id;
use crate::repo::Repo;
use crate::repo::{Repo, RepoTrait};
use crate::worker_pool::{WorkerTask, WorkerTaskItem};
use nostr_sdk::prelude::*;
use std::error::Error;
Expand All @@ -10,6 +10,7 @@ use tokio::time::sleep;
use tokio_util::sync::CancellationToken;
use tracing::debug;

/// Fetches friendly ids and then sends follow change to google pubsub
pub struct FollowChangeHandler {
repo: Arc<Repo>,
google_publisher: GooglePublisher,
Expand Down Expand Up @@ -45,7 +46,7 @@ impl WorkerTask<FollowChange> for FollowChangeHandler {
} = worker_task_item;

// Fetch friendly IDs for the pubkeys or get it from DB if it takes more
// than timeout_secs
// than timeout_secs. Whatever if found through the network is cached.
let (friendly_follower, friendly_followee) = tokio::select!(
result = fetch_friendly_ids(
&self.repo,
Expand All @@ -70,6 +71,7 @@ impl WorkerTask<FollowChange> for FollowChangeHandler {
}
}

/// Get pubkey info from Nostr metadata or nip05 servers
async fn fetch_friendly_ids(
repo: &Arc<Repo>,
nostr_client: &Client,
Expand All @@ -83,13 +85,14 @@ async fn fetch_friendly_ids(
(friendly_follower, friendly_followee)
}

/// Waits some seconds (to give some time for the pubkey info to be found from
/// nostr metadata or nip05 servers) and then just fetches whatever is found in
/// the DB
async fn get_friendly_ids_from_db(
repo: &Arc<Repo>,
follow_change: &FollowChange,
timeout_secs: u64,
) -> (String, String) {
// Wait some seconds to give some time to the friendly follower and
// followee to be found from nostr metadata or nip05 servers
sleep(std::time::Duration::from_secs(timeout_secs)).await;

let (friendly_follower, friendly_followee) = tokio::join!(
Expand Down
Loading

0 comments on commit 0fa5fca

Please sign in to comment.