fix: try 2 to fix sending message error

This commit is contained in:
2026-01-16 21:53:55 +01:00
parent eef169ae02
commit 7cebc490ca

View File

@@ -13,15 +13,10 @@ pub struct Message {
} }
pub async fn send_dm(client: &Client, receiver: PublicKey, message: &str) -> Result<EventId> { pub async fn send_dm(client: &Client, receiver: PublicKey, message: &str) -> Result<EventId> {
println!("[SDK] Sending NIP-17 message to: {}", receiver.to_bech32()?); // In current nostr-sdk versions, send_private_msg sends the GiftWrap to the receiver
// and usually a copy to yourself so you can see your own history.
// send_private_msg in newer nostr-sdk versions handles NIP-17 wrapping.
// Important: It must be broadcasted to relays that both you and the receiver use.
let output = client.send_private_msg(receiver, message, None).await?; let output = client.send_private_msg(receiver, message, None).await?;
// We wait briefly to ensure the relays acknowledged the event
println!("[SDK] Event broadcasted successfully. ID: {}", output.id()); println!("[SDK] Event broadcasted successfully. ID: {}", output.id());
Ok(*output.id()) Ok(*output.id())
} }
@@ -32,17 +27,17 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
let search_start = Timestamp::now() - Duration::from_secs(60 * 60 * 24 * 365); let search_start = Timestamp::now() - Duration::from_secs(60 * 60 * 24 * 365);
// NIP-17 Strategy: // --- STEP 1: The Filter ---
// We fetch ALL GiftWraps (Kind 1059) sent to US. // Important: We ONLY fetch events where WE are the recipient (pubkey == my_pubkey).
// Why? Because those are the only ones we can decrypt!
// This includes: // This includes:
// 1. Messages from others to us. // - Messages FROM contact TO me.
// 2. Copies of messages we sent to others (Self-DMs). // - Messages FROM me TO me (the history copy of what I sent to contact).
let filter = Filter::new() let filter = Filter::new()
.kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage]) .kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage])
.pubkey(my_pubkey) // ONLY messages addressed to me are decryptable! .pubkey(my_pubkey)
.since(search_start); .since(search_start);
// Fetch events from all connected relays
let events = client.fetch_events(filter, Duration::from_secs(10)).await?; let events = client.fetch_events(filter, Duration::from_secs(10)).await?;
let mut seen_ids = HashSet::new(); let mut seen_ids = HashSet::new();
@@ -53,16 +48,16 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
continue; continue;
} }
// === NIP-17 (GiftWrap) Processing ===
if event.kind == Kind::GiftWrap { if event.kind == Kind::GiftWrap {
// Attempt to unwrap (decrypt) the GiftWrap // We can only unwrap if we are the recipient of the GiftWrap
if let Ok(unwrapped) = client.unwrap_gift_wrap(&event).await { if let Ok(unwrapped) = client.unwrap_gift_wrap(&event).await {
let rumor = unwrapped.rumor; let rumor = unwrapped.rumor;
// Is this from the contact to me? // Case A: Incoming (Sender is contact, Receiver is me)
let is_incoming = rumor.pubkey == contact_pubkey; let is_incoming = rumor.pubkey == contact_pubkey;
// Is this a copy of a message I sent to the contact? // Case B: Outgoing (Sender is me, and the rumor has a 'p' tag for the contact)
// We check the internal rumor tags for the recipient's pubkey
let is_outgoing = rumor.pubkey == my_pubkey let is_outgoing = rumor.pubkey == my_pubkey
&& rumor.tags.iter().any(|t| { && rumor.tags.iter().any(|t| {
if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized() if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized()
@@ -85,8 +80,10 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
}); });
} }
} }
} else if event.kind == Kind::EncryptedDirectMessage { }
// Legacy NIP-04 support // === NIP-04 (Legacy) Processing ===
else if event.kind == Kind::EncryptedDirectMessage {
// (Legacy support remains identical)
let is_incoming = event.pubkey == contact_pubkey; let is_incoming = event.pubkey == contact_pubkey;
let has_contact_tag = event.tags.iter().any(|t| { let has_contact_tag = event.tags.iter().any(|t| {
if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized() { if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized() {