Skip to content

Commit

Permalink
playing a bit on a file with cursor AI
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Nov 9, 2024
1 parent 1183033 commit 2097ef0
Showing 1 changed file with 64 additions and 115 deletions.
179 changes: 64 additions & 115 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,61 @@ use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::error;
use tracing::info;


/// Helper function to log warning messages for action errors
fn warning_msg(action: &Action, e: anyhow::Error) {
tracing::warn!("Error in {} with context {}", action, e);
}

/// Handles the processing of a single message action
async fn handle_message_action(
action: &Action,
msg: Message,
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
rate_list: Arc<Mutex<Vec<Event>>>,
) -> Result<()> {
match action {
// Order-related actions
Action::NewOrder => order_action(msg, event, my_keys, pool).await,
Action::TakeSell => take_sell_action(msg, event, my_keys, pool).await,
Action::TakeBuy => take_buy_action(msg, event, my_keys, pool).await,

// Payment-related actions
Action::FiatSent => fiat_sent_action(msg, event, my_keys, pool).await,
Action::Release => release_action(msg, event, my_keys, pool, ln_client).await,
Action::AddInvoice => add_invoice_action(msg, event, my_keys, pool).await,
Action::PayInvoice => todo!(),

// Dispute and rating actions
Action::Dispute => dispute_action(msg, event, my_keys, pool).await,
Action::RateUser => update_user_reputation_action(msg, event, my_keys, pool, rate_list).await,
Action::Cancel => cancel_action(msg, event, my_keys, pool, ln_client).await,

// Admin actions
Action::AdminCancel => admin_cancel_action(msg, event, my_keys, pool, ln_client).await,
Action::AdminSettle => admin_settle_action(msg, event, my_keys, pool, ln_client).await,
Action::AdminAddSolver => admin_add_solver_action(msg, event, my_keys, pool).await,
Action::AdminTakeDispute => admin_take_dispute_action(msg, event, pool).await,

_ => {
tracing::info!("Received message with action {:?}", action);
Ok(())
}
}
}

/// Main event loop that processes incoming Nostr events.
///
/// # Arguments
/// * `my_keys` - The node's keypair
/// * `client` - Nostr client instance
/// * `ln_client` - Lightning network connector
/// * `pool` - SQLite connection pool
/// * `rate_list` - Shared list of rating events
pub async fn run(
my_keys: Keys,
client: &Client,
Expand All @@ -59,7 +107,7 @@ pub async fn run(
// Verify pow
if !event.check_pow(pow) {
// Discard
info!("Not POW verified event!");
tracing::info!("Not POW verified event!");
continue;
}
if let Kind::GiftWrap = event.kind {
Expand All @@ -83,122 +131,23 @@ pub async fn run(
Ok(msg) => {
if msg.get_inner_message_kind().verify() {
if let Some(action) = msg.inner_action() {
match action {
Action::NewOrder => {
if let Err(e) =
order_action(msg, &event, &my_keys, &pool).await
{
warning_msg(&action, e)
}
}
Action::TakeSell => {
if let Err(e) =
take_sell_action(msg, &event, &my_keys, &pool).await
{
warning_msg(&action, e)
}
}
Action::TakeBuy => {
if let Err(e) =
take_buy_action(msg, &event, &my_keys, &pool).await
{
warning_msg(&action, e)
}
}
Action::FiatSent => {
if let Err(e) =
fiat_sent_action(msg, &event, &my_keys, &pool).await
{
warning_msg(&action, e)
}
}
Action::Release => {
if let Err(e) = release_action(
msg, &event, &my_keys, &pool, ln_client,
)
.await
{
warning_msg(&action, e)
}
}
Action::Cancel => {
if let Err(e) = cancel_action(
msg, &event, &my_keys, &pool, ln_client,
)
.await
{
warning_msg(&action, e)
}
}
Action::AddInvoice => {
if let Err(e) =
add_invoice_action(msg, &event, &my_keys, &pool)
.await
{
warning_msg(&action, e)
}
}
Action::PayInvoice => todo!(),
Action::RateUser => {
if let Err(e) = update_user_reputation_action(
msg,
&event,
&my_keys,
&pool,
rate_list.clone(),
)
.await
{
warning_msg(&action, e)
}
}
Action::Dispute => {
if let Err(e) =
dispute_action(msg, &event, &my_keys, &pool).await
{
warning_msg(&action, e)
}
}
Action::AdminCancel => {
if let Err(e) = admin_cancel_action(
msg, &event, &my_keys, &pool, ln_client,
)
.await
{
warning_msg(&action, e)
}
}
Action::AdminSettle => {
if let Err(e) = admin_settle_action(
msg, &event, &my_keys, &pool, ln_client,
)
.await
{
warning_msg(&action, e)
}
}
Action::AdminAddSolver => {
if let Err(e) = admin_add_solver_action(
msg, &event, &my_keys, &pool,
)
.await
{
warning_msg(&action, e)
}
}
Action::AdminTakeDispute => {
if let Err(e) =
admin_take_dispute_action(msg, &event, &pool).await
{
warning_msg(&action, e)
}
}
_ => info!("Received message with action {:?}", action),
if let Err(e) = handle_message_action(
&action,
msg,
&event,
&my_keys,
&pool,
ln_client,
rate_list.clone(),
)
.await
{
warning_msg(&action, e)
}
}
}
}
Err(e) => error!("Failed to parse message from JSON: {:?}", e),
Err(e) => tracing::warn!("Failed to parse event message from JSON: {:?}", e),
}
}
}
Expand Down

0 comments on commit 2097ef0

Please sign in to comment.