From c890d67d6f517e1f98b7653e52bbe86cfcb08fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20G=C3=B3mez?= Date: Fri, 16 Jan 2026 13:19:16 +0100 Subject: [PATCH] fix: Actively fetch own inbox relays for self-chat in NIP-17 Previously, self-chat relied on giftWrapService.inboxRelays$.value being populated, but if empty, self-chat would show 0 relays and fail to send messages. This fix actively fetches own inbox relays using fetchInboxRelays(activePubkey) for self-chat, ensuring inbox relays are always populated and self-chat works correctly. Changes: - Detect self-chat and actively fetch own inbox relays - For non-self-chat, use cached value first (optimization) - Store fetched relays in userInboxRelays variable for metadata - Add logging for self-chat relay fetch status Co-Authored-By: Claude Sonnet 4.5 --- src/lib/chat/adapters/nip-17-adapter.ts | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib/chat/adapters/nip-17-adapter.ts b/src/lib/chat/adapters/nip-17-adapter.ts index 3bfb4c8..7f27532 100644 --- a/src/lib/chat/adapters/nip-17-adapter.ts +++ b/src/lib/chat/adapters/nip-17-adapter.ts @@ -459,11 +459,28 @@ export class Nip17Adapter extends ChatProtocolAdapter { // Fetch inbox relays for all participants const participantInboxRelays: Record = {}; + let userInboxRelays: string[] = []; - // Current user's relays (already loaded) - const userRelays = giftWrapService.inboxRelays$.value; - if (userRelays.length > 0) { - participantInboxRelays[activePubkey] = userRelays; + // For self-chat, actively fetch own inbox relays to ensure they're populated + // For other chats, try cached value first (optimization) + if (isSelfChat) { + const ownRelays = await fetchInboxRelays(activePubkey); + if (ownRelays.length > 0) { + participantInboxRelays[activePubkey] = ownRelays; + userInboxRelays = ownRelays; + console.log( + `[NIP-17] ✅ Fetched own inbox relays for self-chat: ${ownRelays.length} relays`, + ); + } else { + console.warn(`[NIP-17] ⚠️ Could not find inbox relays for self-chat`); + } + } else { + // For non-self-chat, try cached value first + const userRelays = giftWrapService.inboxRelays$.value; + if (userRelays.length > 0) { + participantInboxRelays[activePubkey] = userRelays; + userInboxRelays = userRelays; + } } // Fetch for other participants in parallel @@ -498,7 +515,7 @@ export class Nip17Adapter extends ChatProtocolAdapter { metadata: { encrypted: true, giftWrapped: true, - inboxRelays: userRelays, + inboxRelays: userInboxRelays, participantInboxRelays, // Flag if some participants have no inbox relays (can't send to them) unreachableParticipants: