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 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gómez
2026-01-16 13:19:16 +01:00
parent 0293d2dde6
commit c890d67d6f

View File

@@ -459,11 +459,28 @@ export class Nip17Adapter extends ChatProtocolAdapter {
// Fetch inbox relays for all participants
const participantInboxRelays: Record<string, string[]> = {};
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: