mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-06-16 17:48:34 +02:00
fix(nip-22): use eventLoader for provided event to fix EventStore lookup
CRITICAL FIX: The adapter was failing to find provided events (the comment you click "Chat" on) even when they were already in EventStore. Problem: Line 147 was using the buggy `fetchEvent()` method which calls `firstValueFrom(eventStore.event(id))` - this returns the initial `undefined` value instead of waiting for the cached event to emit. Root cause example: User clicks "Chat" on a kind 1111 comment that's already in EventStore. The adapter calls fetchEvent() to get it, but firstValueFrom returns undefined immediately, throwing "Event not found" error. Solution: - Replace fetchEvent() call with eventLoader for provided event lookup - Remove unused fetchEvent() method (29 lines deleted) - Remove unused getDefaultRelays() method (12 lines deleted) - Add debug logging to track event resolution Benefits: - Events in EventStore are now properly found when opening chat - Consistent use of eventLoader for all event fetching - Cleaner code (48 fewer lines) - Better error messages with event IDs for debugging This completes the EventStore caching fix - now both provided events AND root events properly check the cache before making network requests. All 1114 tests passing.
This commit is contained in:
@@ -143,12 +143,29 @@ export class Nip22Adapter extends ChatProtocolAdapter {
|
||||
const pointer = identifier.value;
|
||||
const relayHints = identifier.relays || [];
|
||||
|
||||
// 1. Fetch the provided event
|
||||
const providedEvent = await this.fetchEvent(pointer, relayHints);
|
||||
// 1. Fetch the provided event using eventLoader (properly checks EventStore cache)
|
||||
console.log(
|
||||
`[NIP-22] Resolving conversation from event ${pointer.id.slice(0, 8)}`,
|
||||
);
|
||||
|
||||
const providedEvent = await firstValueFrom(
|
||||
eventLoader(
|
||||
{ id: pointer.id, kind: pointer.kind, relays: relayHints },
|
||||
pointer.author,
|
||||
),
|
||||
);
|
||||
|
||||
if (!providedEvent) {
|
||||
console.warn(
|
||||
`[NIP-22] Provided event ${pointer.id.slice(0, 8)} not found`,
|
||||
);
|
||||
throw new Error("Event not found");
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[NIP-22] Found provided event ${pointer.id.slice(0, 8)} (kind ${providedEvent.kind})`,
|
||||
);
|
||||
|
||||
// 2. Determine root event based on kind
|
||||
let rootEvent: NostrEvent;
|
||||
let rootId: string;
|
||||
@@ -839,39 +856,6 @@ export class Nip22Adapter extends ChatProtocolAdapter {
|
||||
return getOutboxes(relayList).slice(0, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Fetch an event by pointer
|
||||
*/
|
||||
private async fetchEvent(
|
||||
pointer: { id: string; kind?: number },
|
||||
relayHints: string[] = [],
|
||||
): Promise<NostrEvent | null> {
|
||||
// Check EventStore first
|
||||
const cached = await firstValueFrom(eventStore.event(pointer.id), {
|
||||
defaultValue: undefined,
|
||||
});
|
||||
if (cached) return cached;
|
||||
|
||||
// Not in store - fetch from relays
|
||||
const relays =
|
||||
relayHints.length > 0 ? relayHints : await this.getDefaultRelays();
|
||||
|
||||
const filter: Filter = {
|
||||
ids: [pointer.id],
|
||||
limit: 1,
|
||||
};
|
||||
|
||||
if (pointer.kind !== undefined) {
|
||||
filter.kinds = [pointer.kind];
|
||||
}
|
||||
|
||||
const events = await firstValueFrom(
|
||||
pool.request(relays, [filter], { eventStore }).pipe(toArray()),
|
||||
);
|
||||
|
||||
return events[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Fetch event by EventPointer using eventLoader
|
||||
* EventLoader properly checks EventStore cache first, then uses smart relay selection
|
||||
@@ -958,19 +942,6 @@ export class Nip22Adapter extends ChatProtocolAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Get default relays
|
||||
*/
|
||||
private async getDefaultRelays(): Promise<string[]> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
if (activePubkey) {
|
||||
const outbox = await this.getOutboxRelays(activePubkey);
|
||||
if (outbox.length > 0) return outbox.slice(0, 5);
|
||||
}
|
||||
|
||||
return AGGREGATOR_RELAYS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert root event to Message object
|
||||
* Root event is rendered as a card using KindRenderer for better UX
|
||||
|
||||
Reference in New Issue
Block a user