@@ -12,13 +12,17 @@ use std::thread::sleep;
1212use std:: time:: Duration ;
1313use std:: { fs, path:: Path } ;
1414
15- pub async fn send_admin_gift_wrap_dm (
15+ async fn send_gift_wrap_dm_internal (
1616 client : & Client ,
17- admin_keys : & Keys ,
17+ sender_keys : & Keys ,
1818 receiver_pubkey : & PublicKey ,
1919 message : & str ,
20+ is_admin : bool ,
2021) -> Result < ( ) > {
21- let pow: u8 = var ( "POW" ) . unwrap_or ( '0' . to_string ( ) ) . parse ( ) . unwrap ( ) ;
22+ let pow: u8 = var ( "POW" )
23+ . unwrap_or_else ( |_| "0" . to_string ( ) )
24+ . parse ( )
25+ . unwrap_or ( 0 ) ;
2226
2327 // Create Message struct for consistency with Mostro protocol
2428 let dm_message = Message :: new_dm (
@@ -34,48 +38,34 @@ pub async fn send_admin_gift_wrap_dm(
3438 // Create the rumor with JSON content
3539 let rumor = EventBuilder :: text_note ( content)
3640 . pow ( pow)
37- . build ( admin_keys . public_key ( ) ) ;
41+ . build ( sender_keys . public_key ( ) ) ;
3842
39- // Create gift wrap using admin_keys as the signing key
40- let event = EventBuilder :: gift_wrap ( admin_keys , receiver_pubkey, rumor, Tags :: new ( ) ) . await ?;
43+ // Create gift wrap using sender_keys as the signing key
44+ let event = EventBuilder :: gift_wrap ( sender_keys , receiver_pubkey, rumor, Tags :: new ( ) ) . await ?;
4145
42- info ! ( "Sending admin gift wrap event: {event:#?}" ) ;
46+ let sender_type = if is_admin { "admin" } else { "user" } ;
47+ info ! ( "Sending {} gift wrap event to {}" , sender_type, receiver_pubkey) ;
4348 client. send_event ( & event) . await ?;
4449
4550 Ok ( ( ) )
4651}
4752
53+ pub async fn send_admin_gift_wrap_dm (
54+ client : & Client ,
55+ admin_keys : & Keys ,
56+ receiver_pubkey : & PublicKey ,
57+ message : & str ,
58+ ) -> Result < ( ) > {
59+ send_gift_wrap_dm_internal ( client, admin_keys, receiver_pubkey, message, true ) . await
60+ }
61+
4862pub async fn send_gift_wrap_dm (
4963 client : & Client ,
5064 trade_keys : & Keys ,
5165 receiver_pubkey : & PublicKey ,
5266 message : & str ,
5367) -> Result < ( ) > {
54- let pow: u8 = var ( "POW" ) . unwrap_or ( '0' . to_string ( ) ) . parse ( ) . unwrap ( ) ;
55-
56- // Create Message struct for consistency with Mostro protocol
57- let dm_message = Message :: new_dm (
58- None ,
59- None ,
60- Action :: SendDm ,
61- Some ( Payload :: TextMessage ( message. to_string ( ) ) ) ,
62- ) ;
63-
64- // Serialize as JSON with the expected format (Message, Option<Signature>)
65- let content = serde_json:: to_string ( & ( dm_message, None :: < String > ) ) ?;
66-
67- // Create the rumor with JSON content
68- let rumor = EventBuilder :: text_note ( content)
69- . pow ( pow)
70- . build ( trade_keys. public_key ( ) ) ;
71-
72- // Create gift wrap using trade_keys as ephemeral key
73- let event = EventBuilder :: gift_wrap ( trade_keys, receiver_pubkey, rumor, Tags :: new ( ) ) . await ?;
74-
75- info ! ( "Sending gift wrap event: {event:#?}" ) ;
76- client. send_event ( & event) . await ?;
77-
78- Ok ( ( ) )
68+ send_gift_wrap_dm_internal ( client, trade_keys, receiver_pubkey, message, false ) . await
7969}
8070
8171pub async fn send_dm (
@@ -211,15 +201,24 @@ pub async fn get_direct_messages_from_trade_keys(
211201 since : i64 ,
212202 mostro_pubkey : & PublicKey ,
213203) -> Vec < ( Message , u64 , PublicKey ) > {
204+ if trade_keys_hex. is_empty ( ) {
205+ return Vec :: new ( ) ;
206+ }
207+
214208 let fake_since = 2880 ;
215209 let fake_since_time = chrono:: Utc :: now ( )
216210 . checked_sub_signed ( chrono:: Duration :: minutes ( fake_since) )
217211 . unwrap ( )
218212 . timestamp ( ) as u64 ;
219213 let fake_timestamp = Timestamp :: from ( fake_since_time) ;
214+
215+ let since_time = chrono:: Utc :: now ( )
216+ . checked_sub_signed ( chrono:: Duration :: minutes ( since) )
217+ . unwrap ( )
218+ . timestamp ( ) as u64 ;
220219
221220 let mut all_direct_messages: Vec < ( Message , u64 , PublicKey ) > = Vec :: new ( ) ;
222- let mut id_list = Vec :: < EventId > :: new ( ) ;
221+ let mut id_set = std :: collections :: HashSet :: < EventId > :: new ( ) ;
223222
224223 for trade_key_hex in trade_keys_hex {
225224 if let Ok ( trade_keys) = Keys :: parse ( & trade_key_hex) {
@@ -233,48 +232,45 @@ pub async fn get_direct_messages_from_trade_keys(
233232
234233 if let Ok ( events) = client. fetch_events ( filters, Duration :: from_secs ( 15 ) ) . await {
235234 for dm in events. iter ( ) {
236- if !id_list. contains ( & dm. id ) {
237- id_list. push ( dm. id ) ;
238-
239- let unwrapped_gift = match nip59:: extract_rumor ( & trade_keys, dm) . await {
240- Ok ( u) => u,
241- Err ( _) => {
242- println ! ( "Error unwrapping gift for trade key: {}" , trade_keys. public_key( ) ) ;
243- continue ;
244- }
245- } ;
246-
247- // Filter: only process messages NOT from Mostro (user-to-user messages)
248- if unwrapped_gift. rumor . pubkey == * mostro_pubkey {
249- continue ; // Skip Mostro messages
250- }
235+ if !id_set. insert ( dm. id ) {
236+ continue ; // Already processed
237+ }
251238
252- let since_time = chrono:: Utc :: now ( )
253- . checked_sub_signed ( chrono:: Duration :: minutes ( since) )
254- . unwrap ( )
255- . timestamp ( ) as u64 ;
256-
257- if unwrapped_gift. rumor . created_at . as_u64 ( ) < since_time {
239+ let unwrapped_gift = match nip59:: extract_rumor ( & trade_keys, dm) . await {
240+ Ok ( u) => u,
241+ Err ( _) => {
242+ error ! ( "Error unwrapping gift for trade key: {}" , trade_keys. public_key( ) ) ;
258243 continue ;
259244 }
245+ } ;
246+
247+ // Filter: only process messages NOT from Mostro (user-to-user messages)
248+ if unwrapped_gift. rumor . pubkey == * mostro_pubkey {
249+ continue ; // Skip Mostro messages
250+ }
260251
261- // Parse JSON content (all messages should be JSON now)
262- let ( message, _) : ( Message , Option < String > ) = match serde_json:: from_str ( & unwrapped_gift. rumor . content ) {
263- Ok ( parsed) => parsed,
264- Err ( _) => {
265- println ! ( "Error parsing JSON content from: {}" , unwrapped_gift. rumor. pubkey) ;
266- continue ;
267- }
268- } ;
269-
270- all_direct_messages. push ( (
271- message,
272- unwrapped_gift. rumor . created_at . as_u64 ( ) ,
273- unwrapped_gift. rumor . pubkey
274- ) ) ;
252+ if unwrapped_gift. rumor . created_at . as_u64 ( ) < since_time {
253+ continue ;
275254 }
255+
256+ // Parse JSON content (all messages should be JSON now)
257+ let ( message, _) : ( Message , Option < String > ) = match serde_json:: from_str ( & unwrapped_gift. rumor . content ) {
258+ Ok ( parsed) => parsed,
259+ Err ( _) => {
260+ error ! ( "Error parsing JSON content from: {}" , unwrapped_gift. rumor. pubkey) ;
261+ continue ;
262+ }
263+ } ;
264+
265+ all_direct_messages. push ( (
266+ message,
267+ unwrapped_gift. rumor . created_at . as_u64 ( ) ,
268+ unwrapped_gift. rumor . pubkey
269+ ) ) ;
276270 }
277271 }
272+ } else {
273+ error ! ( "Failed to parse trade key: {}" , trade_key_hex) ;
278274 }
279275 }
280276
0 commit comments