aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/nip17.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-03-01 18:51:16 -0800
committerAdam Soltys <[email protected]>2026-03-01 18:51:16 -0800
commitc7e2383736d3ef99f069226bbafad9fbddfeb030 (patch)
treee71947a1e627cd1a4802808661ed88866a234f81 /src/lib/nip17.ts
parente3b7208ae23d7b56126b0b32b66dfa48140dc1f2 (diff)
downloadcoinos-ui-c7e2383736d3ef99f069226bbafad9fbddfeb030.tar.gz
coinos-ui-c7e2383736d3ef99f069226bbafad9fbddfeb030.zip
messages ui
Diffstat (limited to 'src/lib/nip17.ts')
-rw-r--r--src/lib/nip17.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/nip17.ts b/src/lib/nip17.ts
index 29a14549..1400b5a5 100644
--- a/src/lib/nip17.ts
+++ b/src/lib/nip17.ts
@@ -302,6 +302,58 @@ const createAndPublishMessageEvent = async (
return publishToPreferred(event, eventFor, expiryDays != null);
};
+// Subscribes to new incoming gift wraps for the user.
+// Decrypts new events and passes the rumour to onNewRumour().
+// Returns a close function to stop the subscription.
+// Uses Relay directly instead of SimplePool.subscribeMany to avoid
+// a filter double-nesting bug in nostr-tools v2.23.0.
+export const subscribeToMessages = async (
+ user: any,
+ onNewRumour: (rumour: any) => void,
+): Promise<() => void> => {
+ const preferredRelays = await getPreferredRelays(user.pubkey);
+ const relayUrls = Array.from(new Set([...preferredRelays, ...DM_RELAYS_LIST]));
+ const since = Math.floor(Date.now() / 1000) - TWO_DAYS;
+ const seen = new Set<string>();
+ let eoseCount = 0;
+
+ const closers: (() => void)[] = [];
+
+ for (const url of relayUrls) {
+ try {
+ const relay = await Relay.connect(url);
+ const sub = relay.subscribe(
+ [{ kinds: [1059], "#p": [user.pubkey], since }],
+ {
+ onevent: async (event) => {
+ if (seen.has(event.id)) return;
+ seen.add(event.id);
+ if (eoseCount < relayUrls.length) return;
+ if (expired(event)) return;
+ try {
+ const rumour: any = await decrypt(event, user);
+ const expires = expiration(event);
+ if (expires) rumour.expiration = expires;
+ onNewRumour(rumour);
+ } catch (e) {
+ console.error("Failed to decrypt subscription event:", event.id, e);
+ }
+ },
+ oneose: () => {
+ eoseCount++;
+ },
+ },
+ );
+ closers.push(() => sub.close());
+ } catch (e) {
+ console.error("Failed to connect to relay for subscription:", url, e);
+ eoseCount++;
+ }
+ }
+
+ return () => closers.forEach((close) => close());
+};
+
// Sends a NIP-17 message from `user` to `recipient`.
// If expiryDays is set, messages will expire after that many days.
export const send = async (message: string, user: any, recipient: any, expiryDays?: number) => {