diff --git a/Cargo.toml b/Cargo.toml index 52273e0..39ea8ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mostro" -version = "0.7.9" +version = "0.8.0" edition = "2021" license = "MIT" authors = ["Francisco Calderón "] @@ -38,5 +38,4 @@ mostro-core = "0.2.7" tokio-cron-scheduler = "*" tracing = "0.1.37" tracing-subscriber = "0.3.16" -lazy_static = "1.4.0" config = "0.13.3" diff --git a/src/app.rs b/src/app.rs index b76067a..219b2c8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -17,15 +17,17 @@ use crate::app::cancel::cancel_action; use crate::app::dispute::dispute_action; use crate::app::fiat_sent::fiat_sent_action; use crate::app::order::order_action; -use crate::app::rate_user::update_user_reputation_action; +use crate::app::rate_user::{send_user_rates, update_user_reputation_action}; use crate::app::release::release_action; use crate::app::take_buy::take_buy_action; use crate::app::take_sell::take_sell_action; use crate::lightning::LndConnector; +use crate::CLEAR_USER_VEC; use anyhow::Result; use mostro_core::{Action, Message}; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; +use std::sync::atomic::Ordering; pub async fn run( my_keys: Keys, @@ -36,6 +38,15 @@ pub async fn run( loop { let mut notifications = client.notifications(); + let mut rate_list: Vec = vec![]; + + // Check if we can send user rates updates + if CLEAR_USER_VEC.load(Ordering::Relaxed) { + send_user_rates(&rate_list, &client).await?; + CLEAR_USER_VEC.store(false, Ordering::Relaxed); + rate_list.clear(); + } + while let Ok(notification) = notifications.recv().await { if let RelayPoolNotification::Event(_, event) = notification { if let Kind::EncryptedDirectMessage = event.kind { @@ -85,7 +96,12 @@ pub async fn run( Action::PayInvoice => todo!(), Action::RateUser => { update_user_reputation_action( - msg, &event, &my_keys, &client, &pool, + msg, + &event, + &my_keys, + &client, + &pool, + &mut rate_list, ) .await?; } diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index a14d8d8..accd195 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -140,6 +140,7 @@ pub async fn update_user_reputation_action( my_keys: &Keys, client: &Client, pool: &Pool, + rate_list: &mut Vec, ) -> Result<()> { let order_id = msg.order_id.unwrap(); let order = match Order::by_id(pool, order_id).await? { @@ -245,6 +246,7 @@ pub async fn update_user_reputation_action( order.id, my_keys, pool, + rate_list, ) .await?; @@ -256,3 +258,18 @@ pub async fn update_user_reputation_action( Ok(()) } + +pub async fn send_user_rates(rate_list: &[Event], client: &Client) -> Result<()> { + for ev in rate_list.iter() { + // Send event to relay + match client.send_event(ev.clone()).await { + Ok(id) => { + info!("Updated rate event with id {:?}", id) + } + Err(e) => { + info!("Error on updating rate event {:?}", e.to_string()) + } + } + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 556f290..00d8979 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,18 +16,12 @@ use nostr_sdk::prelude::*; use scheduler::start_scheduler; use settings::Settings; use settings::{init_default_dir, init_global_settings}; +use std::sync::atomic::AtomicBool; use std::{env::args, path::PathBuf, sync::OnceLock}; -use tokio::sync::Mutex; +static CLEAR_USER_VEC: AtomicBool = AtomicBool::new(false); static MOSTRO_CONFIG: OnceLock = OnceLock::new(); -#[macro_use] -extern crate lazy_static; - -lazy_static! { - static ref RATE_EVENT_LIST: Mutex> = Mutex::new(vec![]); -} - #[tokio::main] async fn main() -> Result<()> { pretty_env_logger::init(); diff --git a/src/scheduler.rs b/src/scheduler.rs index 8b6017e..257a776 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -2,7 +2,8 @@ use crate::db::*; use crate::lightning::LndConnector; use crate::settings::Settings; use crate::util::update_order_event; -use crate::RATE_EVENT_LIST; +use crate::CLEAR_USER_VEC; +use std::sync::atomic::Ordering; use anyhow::Result; use mostro_core::Status; @@ -165,24 +166,10 @@ pub async fn cron_scheduler(sched: &JobScheduler) -> Result<(), anyhow::Error> { let job_update_rate_events = Job::new_async("0 0 * * * *", move |uuid, mut l| { Box::pin(async move { - // Connect to relays - let client = crate::util::connect_nostr().await.unwrap(); + info!("I run async every hour - update rate event of users"); - info!("I run async every hour - update rate event of users",); - - for ev in RATE_EVENT_LIST.lock().await.iter() { - // Send event to relay - match client.send_event(ev.clone()).await { - Ok(id) => { - info!("Updated rate event with id {:?}", id) - } - Err(e) => { - info!("Error on updating rate event {:?}", e.to_string()) - } - } - } - // Clear list - RATE_EVENT_LIST.lock().await.clear(); + // Clear list after sending + CLEAR_USER_VEC.store(true, Ordering::Relaxed); let next_tick = l.next_tick_for_job(uuid).await; match next_tick { diff --git a/src/util.rs b/src/util.rs index 498c4a8..096db5e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,7 +4,7 @@ use crate::lightning::LndConnector; use crate::messages; use crate::models::Yadio; use crate::settings::Settings; -use crate::{db, flow, RATE_EVENT_LIST}; +use crate::{db, flow}; use anyhow::{Context, Result}; use log::{error, info}; @@ -188,6 +188,7 @@ pub async fn update_user_rating_event( order_id: Uuid, keys: &Keys, pool: &SqlitePool, + rate_list: &mut Vec, ) -> Result<()> { // let reputation = reput // nip33 kind and d tag @@ -204,7 +205,7 @@ pub async fn update_user_rating_event( } // Add event message to global list - RATE_EVENT_LIST.lock().await.push(event); + rate_list.push(event); Ok(()) }