33//! and publish dispute events to the network.
44
55use crate :: app:: context:: AppContext ;
6- use crate :: config:: settings:: Settings ;
76use crate :: db:: find_dispute_by_order_id;
87use crate :: nip33:: { create_platform_tag_values, new_dispute_event} ;
9- use crate :: util:: { enqueue_order_msg, get_nostr_client , get_order} ;
8+ use crate :: util:: { enqueue_order_msg, get_order} ;
109use mostro_core:: prelude:: * ;
1110use nostr:: nips:: nip59:: UnwrappedGift ;
1211use nostr_sdk:: prelude:: * ;
13- use sqlx :: { Pool , Sqlite } ;
12+
1413use sqlx_crud:: traits:: Crud ;
1514use std:: borrow:: Cow ;
1615use uuid:: Uuid ;
@@ -20,6 +19,7 @@ use uuid::Uuid;
2019/// Creates and publishes a NIP-33 replaceable event containing dispute details,
2120/// including status, initiator (`buyer` or `seller`), and application metadata.
2221async fn publish_dispute_event (
22+ ctx : & AppContext ,
2323 dispute : & Dispute ,
2424 my_keys : & Keys ,
2525 is_buyer_dispute : bool ,
@@ -45,7 +45,7 @@ async fn publish_dispute_event(
4545 // Application identifier tag
4646 Tag :: custom(
4747 TagKind :: Custom ( Cow :: Borrowed ( "y" ) ) ,
48- create_platform_tag_values( Settings :: get_mostro ( ) . name. as_deref( ) ) ,
48+ create_platform_tag_values( ctx . settings ( ) . mostro . name. as_deref( ) ) ,
4949 ) ,
5050 // Event type tag
5151 Tag :: custom(
@@ -61,23 +61,18 @@ async fn publish_dispute_event(
6161
6262 tracing:: info!( "Publishing dispute event: {:#?}" , event) ;
6363
64- // Get nostr client and publish the event
65- match get_nostr_client ( ) {
66- Ok ( client) => match client. send_event ( & event) . await {
67- Ok ( _) => {
68- tracing:: info!(
69- "Successfully published dispute event for dispute ID: {}" ,
70- dispute. id
71- ) ;
72- Ok ( ( ) )
73- }
74- Err ( e) => {
75- tracing:: error!( "Failed to send dispute event: {}" , e) ;
76- Err ( MostroInternalErr ( ServiceError :: NostrError ( e. to_string ( ) ) ) )
77- }
78- } ,
64+ // Get nostr client from context and publish the event
65+ let client = ctx. nostr_client ( ) ;
66+ match client. send_event ( & event) . await {
67+ Ok ( _) => {
68+ tracing:: info!(
69+ "Successfully published dispute event for dispute ID: {}" ,
70+ dispute. id
71+ ) ;
72+ Ok ( ( ) )
73+ }
7974 Err ( e) => {
80- tracing:: error!( "Failed to get Nostr client : {}" , e) ;
75+ tracing:: error!( "Failed to send dispute event : {}" , e) ;
8176 Err ( MostroInternalErr ( ServiceError :: NostrError ( e. to_string ( ) ) ) )
8277 }
8378 }
@@ -102,9 +97,9 @@ fn get_counterpart_info(sender: &str, buyer: &str, seller: &str) -> Result<bool,
10297/// Checks that:
10398/// - The order exists
10499/// - The order status allows disputes (Active or FiatSent)
105- async fn get_valid_order ( pool : & Pool < Sqlite > , msg : & Message ) -> Result < Order , MostroError > {
100+ async fn get_valid_order ( ctx : & AppContext , msg : & Message ) -> Result < Order , MostroError > {
106101 // Try to fetch the order from the database
107- let order = get_order ( msg, pool) . await ?;
102+ let order = get_order ( msg, ctx . pool ( ) ) . await ?;
108103
109104 // Check if the order status is Active or FiatSent
110105 if order. check_status ( Status :: Active ) . is_err ( ) && order. check_status ( Status :: FiatSent ) . is_err ( )
@@ -172,7 +167,7 @@ pub async fn dispute_action(
172167 return Err ( MostroInternalErr ( ServiceError :: DisputeAlreadyExists ) ) ;
173168 }
174169 // Get and validate order
175- let mut order = get_valid_order ( pool , & msg) . await ?;
170+ let mut order = get_valid_order ( ctx , & msg) . await ?;
176171 // Get seller and buyer pubkeys
177172 let ( seller, buyer) = match ( & order. seller_pubkey , & order. buyer_pubkey ) {
178173 ( Some ( seller) , Some ( buyer) ) => ( seller. to_owned ( ) , buyer. to_owned ( ) ) ,
@@ -236,7 +231,7 @@ pub async fn dispute_action(
236231 . await ?;
237232
238233 // Publish dispute event to network
239- publish_dispute_event ( & dispute, my_keys, is_buyer_dispute)
234+ publish_dispute_event ( ctx , & dispute, my_keys, is_buyer_dispute)
240235 . await
241236 . map_err ( |_| MostroInternalErr ( ServiceError :: DisputeEventError ) ) ?;
242237
@@ -256,12 +251,13 @@ pub async fn dispute_action(
256251/// * `my_keys` - Mostro's keys for signing the dispute event
257252/// * `context` - Description of the resolution context for logging (e.g., "cooperative cancel")
258253pub async fn close_dispute_after_user_resolution (
259- pool : & Pool < Sqlite > ,
254+ ctx : & AppContext ,
260255 order : & Order ,
261256 new_status : DisputeStatus ,
262257 my_keys : & Keys ,
263258 context : & str ,
264259) {
260+ let pool = ctx. pool ( ) ;
265261 if let Ok ( mut dispute) = find_dispute_by_order_id ( pool, order. id ) . await {
266262 let dispute_id = dispute. id ;
267263 dispute. status = new_status. to_string ( ) ;
@@ -310,7 +306,7 @@ pub async fn close_dispute_after_user_resolution(
310306 ) ,
311307 Tag :: custom(
312308 TagKind :: Custom ( Cow :: Borrowed ( "y" ) ) ,
313- create_platform_tag_values( Settings :: get_mostro ( ) . name. as_deref( ) ) ,
309+ create_platform_tag_values( ctx . settings ( ) . mostro . name. as_deref( ) ) ,
314310 ) ,
315311 Tag :: custom(
316312 TagKind :: Custom ( Cow :: Borrowed ( "z" ) ) ,
@@ -319,17 +315,13 @@ pub async fn close_dispute_after_user_resolution(
319315 ] ) ;
320316
321317 match new_dispute_event ( my_keys, "" , dispute_id. to_string ( ) , tags) {
322- Ok ( event) => match get_nostr_client ( ) {
323- Ok ( client) => {
324- tracing:: info!( "Publishing dispute close event: {:#?}" , event) ;
325- if let Err ( e) = client. send_event ( & event) . await {
326- tracing:: error!( "Failed to publish dispute close event: {}" , e) ;
327- }
318+ Ok ( event) => {
319+ let client = ctx. nostr_client ( ) ;
320+ tracing:: info!( "Publishing dispute close event: {:#?}" , event) ;
321+ if let Err ( e) = client. send_event ( & event) . await {
322+ tracing:: error!( "Failed to publish dispute close event: {}" , e) ;
328323 }
329- Err ( e) => {
330- tracing:: error!( "Failed to get Nostr client for dispute event: {}" , e) ;
331- }
332- } ,
324+ }
333325 Err ( e) => {
334326 tracing:: error!(
335327 "Failed to create dispute close event for dispute {}: {}" ,
0 commit comments