mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-12 16:37:06 +02:00
This commit implements a production-ready solution for NIP-17 gift wrap sending and persistence, fixing the issue where self-chat messages would appear optimistically but disappear on page reload. **Root Causes Identified:** 1. **publishEvent ignored relay hints from actions** - ActionRunner calls publishMethod(event, relays) with two parameters - Our publishEvent only accepted one parameter (event) - Gift wrap actions passed inbox relays as second parameter → ignored - Gift wraps published to wrong relays or failed with no relay list 2. **Encrypted content not persisted during send** - Gift wraps created with EncryptedContentSymbol (in-memory only) - When received back from relay, new instance had no symbol - isGiftWrapUnlocked() returned false → marked as "pending" - Messages didn't appear in UI until manually decrypted 3. **Optimistic updates created duplicate risk** - Synthetic rumor created with timestamp T1, ID calculated - Real rumor created with timestamp T2, different ID - Potential for duplicates in UI on relay echo **Changes:** - src/services/hub.ts: * Modified publishEvent signature to accept optional relayHints parameter * Use relay hints when provided, fallback to outbox relay lookup * Added encrypted content persistence to Dexie for kind 1059 events * Persists decrypted content using EncryptedContentSymbol during publish * Fixed TypeScript null handling for getOutboxRelays return type * Added detailed console logging for relay routing debugging - src/lib/chat/adapters/nip-17-adapter.ts: * Removed optimistic update code (lines 651-725) * Removed synthetic rumor creation and ID calculation * Removed immediate decryptedRumors$ update on send * Simplified sendMessage flow to rely on natural relay echo (~200-500ms) * Kept relay alignment debug logging for self-chat diagnostics - src/services/gift-wrap.ts: * Added refreshPersistedIds() method for debugging * Allows manual reload of persisted IDs from Dexie if needed **Expected Behavior After Fix:** 1. User sends self-chat message 2. SendWrappedMessage queries own inbox relays (kind 10050) 3. publishEvent receives relay hints, uses them for publishing 4. Gift wrap encrypted content persisted to Dexie during publish 5. Gift wrap sent to inbox relays (same relays we're subscribed to) 6. Gift wrap received back from relay (~200ms) 7. persistedIds check recognizes it as already unlocked 8. Message appears in UI and persists across reloads **Testing Required:** Manual testing checklist (see plan in /claudedocs/): - Self-chat: send message, verify appears and persists on reload - 1-on-1 chat: verify messages persist across reloads - Group chat: verify multi-recipient messages work - Reply functionality: verify reply threads persist - Error cases: verify clear error messages for missing inbox relays Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>