Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor AI - test on a file #389

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Replace todo!() with proper error handling

The PayInvoice action will cause a runtime panic. This should be properly handled or marked as unimplemented.

-        Action::PayInvoice => todo!(),
+        Action::PayInvoice => {
+            tracing::warn!("PayInvoice action not implemented yet");
+            Ok(())
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Action::PayInvoice => todo!(),
Action::PayInvoice => {
tracing::warn!("PayInvoice action not implemented yet");
Ok(())
}


// 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(())
}
Comment on lines +78 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider proper error handling for unknown actions

The catch-all pattern silently logs unknown actions at info level. Consider whether this should be a warning or error level log, and whether it should return an error result.

         _ => {
-            tracing::info!("Received message with action {:?}", action);
+            tracing::warn!("Received unknown action: {:?}", action);
+            Err(anyhow::anyhow!("Unsupported action type"))
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_ => {
tracing::info!("Received message with action {:?}", action);
Ok(())
}
_ => {
tracing::warn!("Received unknown action: {:?}", action);
Err(anyhow::anyhow!("Unsupported action type"))
}

}
}

/// 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
4 changes: 2 additions & 2 deletions src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ pub async fn admin_take_dispute_action(
// to them know who will assist them on the dispute
let solver_pubkey = Peer::new(event.sender.to_hex());
let msg_to_buyer = Message::new_order(
request_id,
None,
Some(order.id),
Action::AdminTookDispute,
Some(Content::Peer(solver_pubkey.clone())),
);

let msg_to_seller = Message::new_order(
request_id,
None,
Some(order.id),
Action::AdminTookDispute,
Some(Content::Peer(solver_pubkey)),
Expand Down
2 changes: 1 addition & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn fiat_sent_action(

// We a message to the seller
send_new_order_msg(
request_id,
None,
Some(order.id),
Action::FiatSentOk,
Some(Content::Peer(peer)),
Expand Down
Loading