fix: try to fix sending message error
This commit is contained in:
@@ -12,9 +12,16 @@ pub struct Message {
|
||||
pub is_incoming: bool,
|
||||
}
|
||||
|
||||
/// Sends a DM. The SDK handles the NIP-17/NIP-04 logic internally.
|
||||
pub async fn send_dm(client: &Client, receiver: PublicKey, message: &str) -> Result<EventId> {
|
||||
println!("[SDK] Sending NIP-17 message to: {}", receiver.to_bech32()?);
|
||||
|
||||
// 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?;
|
||||
|
||||
// We wait briefly to ensure the relays acknowledged the event
|
||||
println!("[SDK] Event broadcasted successfully. ID: {}", output.id());
|
||||
|
||||
Ok(*output.id())
|
||||
}
|
||||
|
||||
@@ -23,57 +30,40 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
|
||||
let my_pubkey = signer.get_public_key().await?;
|
||||
let contact_pubkey = PublicKey::parse(contact_npub)?;
|
||||
|
||||
// Look back 1 year to ensure we find all history
|
||||
let search_start = Timestamp::now() - Duration::from_secs(60 * 60 * 24 * 365);
|
||||
|
||||
// --- STEP 1: Define Filters ---
|
||||
// Filter A: Events where I am the recipient (#p tag)
|
||||
// This includes incoming messages and my own "self-DM" copies for history.
|
||||
let filter_to_me = Filter::new()
|
||||
// NIP-17 Strategy:
|
||||
// We fetch ALL GiftWraps (Kind 1059) sent to US.
|
||||
// This includes:
|
||||
// 1. Messages from others to us.
|
||||
// 2. Copies of messages we sent to others (Self-DMs).
|
||||
let filter = Filter::new()
|
||||
.kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage])
|
||||
.pubkey(my_pubkey)
|
||||
.pubkey(my_pubkey) // ONLY messages addressed to me are decryptable!
|
||||
.since(search_start);
|
||||
|
||||
// Filter B: Events where I am the author
|
||||
// Necessary to find outgoing NIP-04 messages and certain NIP-17 relay storage patterns.
|
||||
let filter_from_me = Filter::new()
|
||||
.kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage])
|
||||
.author(my_pubkey)
|
||||
.since(search_start);
|
||||
// Fetch events from all connected relays
|
||||
let events = client.fetch_events(filter, Duration::from_secs(10)).await?;
|
||||
|
||||
// --- STEP 2: Fetch Events Sequentially ---
|
||||
// SDK 0.44.1 fetch_events only accepts a single Filter.
|
||||
// We execute both requests and combine the results.
|
||||
let events_to_me = client
|
||||
.fetch_events(filter_to_me, Duration::from_secs(10))
|
||||
.await?;
|
||||
|
||||
let events_from_me = client
|
||||
.fetch_events(filter_from_me, Duration::from_secs(10))
|
||||
.await?;
|
||||
|
||||
// Combine both result sets into a single vector
|
||||
let mut all_events = events_to_me.to_vec();
|
||||
all_events.extend(events_from_me.to_vec());
|
||||
|
||||
// --- STEP 3: Deduplicate and Process ---
|
||||
let mut seen_ids = HashSet::new();
|
||||
let mut messages: Vec<Message> = Vec::new();
|
||||
|
||||
for event in all_events {
|
||||
// Skip duplicates from multiple relays or overlapping filters
|
||||
for event in events.to_vec() {
|
||||
if !seen_ids.insert(event.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// === NIP-17 (Modern) Processing ===
|
||||
if event.kind == Kind::GiftWrap {
|
||||
// Attempt to unwrap (decrypt) the GiftWrap
|
||||
if let Ok(unwrapped) = client.unwrap_gift_wrap(&event).await {
|
||||
let rumor = unwrapped.rumor;
|
||||
let is_from_contact = rumor.pubkey == contact_pubkey;
|
||||
|
||||
// Outgoing logic: Rumor is from me, and rumor p-tag is the contact
|
||||
let is_from_me = rumor.pubkey == my_pubkey
|
||||
// Is this from the contact to me?
|
||||
let is_incoming = rumor.pubkey == contact_pubkey;
|
||||
|
||||
// Is this a copy of a message I sent to the contact?
|
||||
// We check the internal rumor tags for the recipient's pubkey
|
||||
let is_outgoing = rumor.pubkey == my_pubkey
|
||||
&& rumor.tags.iter().any(|t| {
|
||||
if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized()
|
||||
{
|
||||
@@ -83,7 +73,7 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
|
||||
}
|
||||
});
|
||||
|
||||
if (is_from_contact || is_from_me)
|
||||
if (is_incoming || is_outgoing)
|
||||
&& (rumor.kind == Kind::from(14) || rumor.kind == Kind::TextNote)
|
||||
{
|
||||
messages.push(Message {
|
||||
@@ -91,13 +81,12 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
|
||||
sender: rumor.pubkey,
|
||||
content: rumor.content.clone(),
|
||||
created_at: rumor.created_at,
|
||||
is_incoming: is_from_contact,
|
||||
is_incoming,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// === NIP-04 (Legacy) Processing ===
|
||||
else if event.kind == Kind::EncryptedDirectMessage {
|
||||
} else if event.kind == Kind::EncryptedDirectMessage {
|
||||
// Legacy NIP-04 support
|
||||
let is_incoming = event.pubkey == contact_pubkey;
|
||||
let has_contact_tag = event.tags.iter().any(|t| {
|
||||
if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized() {
|
||||
@@ -127,7 +116,6 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
|
||||
}
|
||||
}
|
||||
|
||||
// Chronological sorting (oldest first)
|
||||
messages.sort_by(|a, b| a.created_at.cmp(&b.created_at));
|
||||
Ok(messages)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user