ditto/src/utils/outbox.ts

29 lines
736 B
TypeScript
Raw Normal View History

2024-05-14 18:23:41 -05:00
import { NStore } from '@nostrify/nostrify';
2024-04-02 18:29:26 -05:00
import { Conf } from '@/config.ts';
2024-05-14 18:23:41 -05:00
export async function getRelays(store: NStore, pubkey: string): Promise<Set<string>> {
2024-04-02 18:29:26 -05:00
const relays = new Set<`wss://${string}`>();
2024-05-14 18:23:41 -05:00
const events = await store.query([
2024-04-02 18:29:26 -05:00
{ kinds: [10002], authors: [pubkey, Conf.pubkey], limit: 2 },
]);
for (const event of events) {
for (const [name, relay, marker] of event.tags) {
if (name === 'r' && (marker === 'write' || !marker)) {
try {
const url = new URL(relay);
if (url.protocol === 'wss:') {
relays.add(url.toString() as `wss://${string}`);
}
} catch (_e) {
// do nothing
}
}
}
}
return relays;
}