TAO/client/src/utils/subscriptions.ts

239 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-09-15 15:56:25 +00:00
import {sub, subOnce, unsubAll} from './relays';
import { Event } from 'nostr-tools';
type SubCallback = (
event: Event,
relay: string,
) => void;
/** subscribe to global feed */
export const subGlobalFeed = (onEvent: SubCallback) => {
console.info('subscribe to global feed');
unsubAll();
const now = Math.floor(Date.now() * 0.001);
const pubkeys = new Set<string>();
const notes = new Set<string>();
2023-09-15 16:58:12 +00:00
const prefix = Math.floor(16 / 4); // 4 bits in each '0' character
2023-09-15 15:56:25 +00:00
sub({ // get past events
cb: (evt, relay) => {
pubkeys.add(evt.pubkey);
notes.add(evt.id);
onEvent(evt, relay);
},
filter: {
2023-09-15 16:58:12 +00:00
...(prefix && {ids: ['0'.repeat(prefix)]}),
2023-09-15 15:56:25 +00:00
kinds: [1],
since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)),
2023-09-15 16:58:12 +00:00
limit: 100,
2023-09-15 15:56:25 +00:00
},
unsub: true
});
2023-09-24 13:22:51 +00:00
// // New Callback to only add events that pass the PoW requirement
// const powFilteredCallback = (evt: Event, relay: string) => {
// if (getPow(evt.id) > 2) { // Replace '5' with your actual PoW requirement
// pubkeys.add(evt.pubkey);
// notes.add(evt.id);
// onEvent(evt, relay);
// }
// };
2023-09-15 16:58:12 +00:00
2023-09-15 15:56:25 +00:00
setTimeout(() => {
// get profile info
sub({
2023-09-24 13:22:51 +00:00
cb: onEvent,
2023-09-15 15:56:25 +00:00
filter: {
authors: Array.from(pubkeys),
kinds: [0],
limit: pubkeys.size,
},
unsub: true,
});
pubkeys.clear();
2023-09-24 13:22:51 +00:00
sub({
cb: onEvent,
filter: {
'#e': Array.from(notes),
kinds: [1],
},
unsub: true,
});
2023-09-15 15:56:25 +00:00
notes.clear();
}, 2000);
// subscribe to future notes, reactions and profile updates
sub({
cb: (evt, relay) => {
onEvent(evt, relay);
if (
evt.kind !== 1
|| pubkeys.has(evt.pubkey)
) {
return;
}
2023-09-15 16:58:12 +00:00
subOnce({ // get profile data
2023-09-15 15:56:25 +00:00
relay,
cb: onEvent,
filter: {
authors: [evt.pubkey],
kinds: [0],
limit: 1,
}
});
},
filter: {
2023-09-15 16:58:12 +00:00
...(prefix && {ids: ['0'.repeat(prefix)]}),
kinds: [1],
2023-09-15 15:56:25 +00:00
since: now,
},
});
};
2023-09-19 15:09:25 +00:00
/** subscribe to global feed */
2023-09-15 15:56:25 +00:00
export const simpleSub24hFeed = (onEvent: SubCallback) => {
unsubAll();
sub({
cb: onEvent,
filter: {
kinds: [1],
//until: Math.floor(Date.now() * 0.001),
since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)),
limit: 1,
}
});
2023-09-19 15:09:25 +00:00
};
/** subscribe to a note id (nip-19) */
export const subNote = (
eventId: string,
onEvent: SubCallback,
) => {
unsubAll();
2023-09-25 08:15:53 +00:00
const pubkeys = new Set<string>();
2023-09-19 15:09:25 +00:00
sub({
2023-09-25 08:15:53 +00:00
cb: (evt, relay) => {
pubkeys.add(evt.pubkey);
onEvent(evt, relay);
},
2023-09-19 15:09:25 +00:00
filter: {
ids: [eventId],
kinds: [1],
limit: 1,
},
unsub: true,
});
const replies = new Set<string>();
const onReply = (evt: Event, relay: string) => {
replies.add(evt.id)
onEvent(evt, relay);
unsubAll();
sub({
2023-09-25 08:15:53 +00:00
cb: (evt, relay) => {
pubkeys.add(evt.pubkey);
onEvent(evt, relay);
},
2023-09-19 15:09:25 +00:00
filter: {
'#e': Array.from(replies),
kinds: [1],
},
unsub: true,
});
};
2023-09-25 08:15:53 +00:00
setTimeout(() => {
// get profile info
sub({
cb: onEvent,
filter: {
authors: Array.from(pubkeys),
kinds: [0],
limit: pubkeys.size,
},
unsub: true,
});
pubkeys.clear();
}, 2000);
2023-09-19 15:09:25 +00:00
replies.add(eventId);
setTimeout(() => {
sub({
cb: onReply,
filter: {
'#e': [eventId],
kinds: [1],
},
unsub: true, // TODO: probably keep this subscription also after onReply/unsubAll
});
}, 200);
2023-10-26 09:46:48 +00:00
};
/** quick subscribe to a note id (nip-19) */
export const subNoteOnce = (
eventId: string,
onEvent: SubCallback,
) => {
const pubkeys = new Set<string>();
sub({
cb: (evt, relay) => {
pubkeys.add(evt.pubkey);
onEvent(evt, relay);
},
filter: {
ids: [eventId],
kinds: [1],
limit: 1,
},
unsub: true,
});
setTimeout(() => {
// get profile info
sub({
cb: onEvent,
filter: {
authors: Array.from(pubkeys),
kinds: [0],
limit: pubkeys.size,
},
unsub: true,
});
pubkeys.clear();
}, 2000);
2023-10-31 00:09:35 +00:00
};
/** quick subscribe to a note id (nip-19) */
export const subNotesOnce = (
eventIds: string[],
onEvent: SubCallback,
) => {
const pubkeys = new Set<string>();
sub({
cb: (evt, relay) => {
pubkeys.add(evt.pubkey);
onEvent(evt, relay);
},
filter: {
ids: eventIds,
kinds: [1],
limit: 1,
},
unsub: true,
});
setTimeout(() => {
// get profile info
sub({
cb: onEvent,
filter: {
authors: Array.from(pubkeys),
kinds: [0],
limit: pubkeys.size,
},
unsub: true,
});
pubkeys.clear();
}, 2000);
};