fix: try to fix sending message error
This commit is contained in:
@@ -12,9 +12,16 @@ pub struct Message {
|
|||||||
pub is_incoming: bool,
|
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> {
|
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?;
|
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())
|
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 my_pubkey = signer.get_public_key().await?;
|
||||||
let contact_pubkey = PublicKey::parse(contact_npub)?;
|
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);
|
let search_start = Timestamp::now() - Duration::from_secs(60 * 60 * 24 * 365);
|
||||||
|
|
||||||
// --- STEP 1: Define Filters ---
|
// NIP-17 Strategy:
|
||||||
// Filter A: Events where I am the recipient (#p tag)
|
// We fetch ALL GiftWraps (Kind 1059) sent to US.
|
||||||
// This includes incoming messages and my own "self-DM" copies for history.
|
// This includes:
|
||||||
let filter_to_me = Filter::new()
|
// 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])
|
.kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage])
|
||||||
.pubkey(my_pubkey)
|
.pubkey(my_pubkey) // ONLY messages addressed to me are decryptable!
|
||||||
.since(search_start);
|
.since(search_start);
|
||||||
|
|
||||||
// Filter B: Events where I am the author
|
// Fetch events from all connected relays
|
||||||
// Necessary to find outgoing NIP-04 messages and certain NIP-17 relay storage patterns.
|
let events = client.fetch_events(filter, Duration::from_secs(10)).await?;
|
||||||
let filter_from_me = Filter::new()
|
|
||||||
.kinds([Kind::GiftWrap, Kind::EncryptedDirectMessage])
|
|
||||||
.author(my_pubkey)
|
|
||||||
.since(search_start);
|
|
||||||
|
|
||||||
// --- 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 seen_ids = HashSet::new();
|
||||||
let mut messages: Vec<Message> = Vec::new();
|
let mut messages: Vec<Message> = Vec::new();
|
||||||
|
|
||||||
for event in all_events {
|
for event in events.to_vec() {
|
||||||
// Skip duplicates from multiple relays or overlapping filters
|
|
||||||
if !seen_ids.insert(event.id) {
|
if !seen_ids.insert(event.id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === NIP-17 (Modern) Processing ===
|
|
||||||
if event.kind == Kind::GiftWrap {
|
if event.kind == Kind::GiftWrap {
|
||||||
|
// Attempt to unwrap (decrypt) 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;
|
||||||
let is_from_contact = rumor.pubkey == contact_pubkey;
|
|
||||||
|
|
||||||
// Outgoing logic: Rumor is from me, and rumor p-tag is the contact
|
// Is this from the contact to me?
|
||||||
let is_from_me = rumor.pubkey == my_pubkey
|
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| {
|
&& 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()
|
||||||
{
|
{
|
||||||
@@ -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)
|
&& (rumor.kind == Kind::from(14) || rumor.kind == Kind::TextNote)
|
||||||
{
|
{
|
||||||
messages.push(Message {
|
messages.push(Message {
|
||||||
@@ -91,13 +81,12 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result<Vec<
|
|||||||
sender: rumor.pubkey,
|
sender: rumor.pubkey,
|
||||||
content: rumor.content.clone(),
|
content: rumor.content.clone(),
|
||||||
created_at: rumor.created_at,
|
created_at: rumor.created_at,
|
||||||
is_incoming: is_from_contact,
|
is_incoming,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if event.kind == Kind::EncryptedDirectMessage {
|
||||||
// === NIP-04 (Legacy) Processing ===
|
// Legacy NIP-04 support
|
||||||
else if event.kind == Kind::EncryptedDirectMessage {
|
|
||||||
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() {
|
||||||
@@ -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));
|
messages.sort_by(|a, b| a.created_at.cmp(&b.created_at));
|
||||||
Ok(messages)
|
Ok(messages)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user