Skip to content

Commit

Permalink
Publish new dispute nip33 event
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Oct 25, 2023
1 parent 196dd9a commit 5130eeb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ uuid = { version = "1.3.0", features = [
"serde",
] }
reqwest = { version = "0.11", features = ["json"] }
mostro-core = "0.3.7"
#tokio-cron-scheduler = "*"
mostro-core = { path = "../mostro-core" }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
1 change: 1 addition & 0 deletions migrations/20230928145530_disputes.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CREATE TABLE IF NOT EXISTS disputes (
id char(36) primary key not null,
order_id char(36) unique not null,
status varchar(10) not null,
solver_pubkey char(64),
Expand Down
21 changes: 13 additions & 8 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::db::{add_dispute, update_order_buyer_dispute, update_order_seller_dispute};
use crate::nip33::new_event;
use crate::util::send_dm;

use anyhow::Result;
use log::error;
use mostro_core::dispute::{Dispute, Status};
use log::info;
use mostro_core::dispute::Dispute;
use mostro_core::order::Order;
use mostro_core::{Action, Message};
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -65,13 +67,7 @@ pub async fn dispute_action(
if !update_buyer_dispute && !update_seller_dispute {
return Ok(());
};
let dispute = Dispute {
order_id,
status: Status::Pending,
solver_pubkey: None,
created_at: 0,
taken_at: 0,
};
let dispute = Dispute::new(order.id);
add_dispute(&dispute, pool).await?;

// We create a Message for the initiator
Expand All @@ -91,6 +87,15 @@ pub async fn dispute_action(
let message = message.as_json()?;
let counterpart_pubkey = XOnlyPublicKey::from_bech32(counterpart)?;
send_dm(client, my_keys, &counterpart_pubkey, message).await?;
// nip33 kind with dispute id as identifier
let event = new_event(
my_keys,
"".to_string(),
dispute.id.to_string(),
([]).to_vec(),
)?;
info!("Dispute event to be published: {event:#?}");
client.send_event(event).await?;

Ok(())
}
4 changes: 3 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result
let dispute = sqlx::query_as::<_, Dispute>(
r#"
INSERT INTO disputes (
id,
order_id,
status,
solver_pubkey,
created_at,
taken_at
) VALUES (?1, ?2, ?3, ?4, ?5)
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
RETURNING *
"#,
)
.bind(dispute.id)
.bind(dispute.order_id)
.bind(&dispute.status.to_string())
.bind(&dispute.solver_pubkey)
Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ pub async fn publish_order(
None,
None,
None,
Some(order.created_at),
order.created_at,
);
let order_string = order.as_json().unwrap();
info!("serialized order: {order_string}");
// nip33 kind with order fields as tags and order id as identifier
let event = new_event(keys, order_string, order_id.to_string(), tags)?;
info!("Event to be published: {event:#?}");
info!("Order event to be published: {event:#?}");
let event_id = event.id.to_string();
info!("Publishing Event Id: {event_id} for Order Id: {order_id}");
// We update the order id with the new event_id
Expand Down Expand Up @@ -217,7 +217,7 @@ pub async fn update_order_event(
None,
None,
None,
Some(order.created_at),
order.created_at,
);
let order_content = publish_order.as_json()?;
let mut order = order.clone();
Expand Down

0 comments on commit 5130eeb

Please sign in to comment.