2023-05-03 20:22:24 +00:00
|
|
|
import { Author, findReplyTag, matchFilter, RelayPool } from '@/deps.ts';
|
2023-04-29 20:49:22 +00:00
|
|
|
import { type Event, type SignedEvent } from '@/event.ts';
|
2023-03-05 04:10:56 +00:00
|
|
|
|
|
|
|
import { poolRelays } from './config.ts';
|
|
|
|
|
2023-03-18 23:09:16 +00:00
|
|
|
import { eventDateComparator, nostrNow } from './utils.ts';
|
2023-03-05 04:10:56 +00:00
|
|
|
|
|
|
|
const pool = new RelayPool(poolRelays);
|
|
|
|
|
2023-05-03 20:22:24 +00:00
|
|
|
type Filter<K extends number = number> = {
|
|
|
|
ids?: string[];
|
|
|
|
kinds?: K[];
|
|
|
|
authors?: string[];
|
|
|
|
since?: number;
|
|
|
|
until?: number;
|
|
|
|
limit?: number;
|
|
|
|
search?: string;
|
|
|
|
[key: `#${string}`]: string[];
|
|
|
|
};
|
|
|
|
|
2023-05-01 01:21:55 +00:00
|
|
|
interface GetFilterOpts {
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:23:51 +00:00
|
|
|
/** Get events from a NIP-01 filter. */
|
2023-05-03 20:22:24 +00:00
|
|
|
function getFilter<K extends number>(filter: Filter<K>, opts: GetFilterOpts = {}): Promise<SignedEvent<K>[]> {
|
2023-04-30 01:23:51 +00:00
|
|
|
return new Promise((resolve) => {
|
2023-05-01 01:21:55 +00:00
|
|
|
let tid: number;
|
2023-04-30 01:23:51 +00:00
|
|
|
const results: SignedEvent[] = [];
|
2023-05-01 01:21:55 +00:00
|
|
|
|
|
|
|
const unsub = pool.subscribe(
|
2023-04-30 01:23:51 +00:00
|
|
|
[filter],
|
|
|
|
poolRelays,
|
|
|
|
(event: SignedEvent | null) => {
|
|
|
|
if (event && matchFilter(filter, event)) {
|
2023-05-04 02:54:22 +00:00
|
|
|
results.push({
|
|
|
|
id: event.id,
|
|
|
|
kind: event.kind,
|
|
|
|
pubkey: event.pubkey,
|
|
|
|
content: event.content,
|
|
|
|
tags: event.tags,
|
|
|
|
created_at: event.created_at,
|
|
|
|
sig: event.sig,
|
|
|
|
});
|
2023-04-30 01:23:51 +00:00
|
|
|
}
|
2023-05-01 01:21:55 +00:00
|
|
|
if (filter.limit && results.length >= filter.limit) {
|
|
|
|
unsub();
|
|
|
|
clearTimeout(tid);
|
2023-05-03 20:22:24 +00:00
|
|
|
resolve(results as SignedEvent<K>[]);
|
2023-05-01 01:21:55 +00:00
|
|
|
}
|
2023-04-30 01:23:51 +00:00
|
|
|
},
|
|
|
|
undefined,
|
2023-05-01 01:21:55 +00:00
|
|
|
() => {
|
|
|
|
unsub();
|
|
|
|
clearTimeout(tid);
|
2023-05-03 20:22:24 +00:00
|
|
|
resolve(results as SignedEvent<K>[]);
|
2023-05-01 01:21:55 +00:00
|
|
|
},
|
2023-04-30 01:23:51 +00:00
|
|
|
);
|
2023-05-01 01:21:55 +00:00
|
|
|
|
|
|
|
if (typeof opts.timeout === 'number') {
|
|
|
|
tid = setTimeout(() => {
|
|
|
|
unsub();
|
2023-05-03 20:22:24 +00:00
|
|
|
resolve(results as SignedEvent<K>[]);
|
2023-05-01 01:21:55 +00:00
|
|
|
}, opts.timeout);
|
|
|
|
}
|
2023-04-30 01:23:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-29 20:54:21 +00:00
|
|
|
/** Get a Nostr event by its ID. */
|
2023-04-30 01:23:51 +00:00
|
|
|
const getEvent = async <K extends number = number>(id: string, kind?: K): Promise<SignedEvent<K> | undefined> => {
|
2023-03-18 19:49:44 +00:00
|
|
|
const event = await (pool.getEventById(id, poolRelays, 0) as Promise<SignedEvent>);
|
2023-04-30 01:23:51 +00:00
|
|
|
if (event) {
|
|
|
|
if (event.id !== id) return undefined;
|
|
|
|
if (kind && event.kind !== kind) return undefined;
|
|
|
|
return event as SignedEvent<K>;
|
|
|
|
}
|
2023-03-05 04:10:56 +00:00
|
|
|
};
|
|
|
|
|
2023-04-29 20:54:21 +00:00
|
|
|
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
|
2023-04-29 22:49:03 +00:00
|
|
|
const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | undefined> => {
|
2023-03-05 04:10:56 +00:00
|
|
|
const author = new Author(pool, poolRelays, pubkey);
|
2023-03-18 19:49:44 +00:00
|
|
|
const event: SignedEvent<0> | null = await new Promise((resolve) => author.metaData(resolve, 0));
|
2023-04-29 22:49:03 +00:00
|
|
|
return event?.pubkey === pubkey ? event : undefined;
|
2023-03-05 04:10:56 +00:00
|
|
|
};
|
|
|
|
|
2023-04-29 20:54:21 +00:00
|
|
|
/** Get users the given pubkey follows. */
|
2023-05-01 01:21:55 +00:00
|
|
|
const getFollows = async (pubkey: string): Promise<SignedEvent<3> | undefined> => {
|
2023-05-04 02:53:36 +00:00
|
|
|
const [event] = await getFilter({ authors: [pubkey], kinds: [3] }, { timeout: 5000 });
|
|
|
|
return event;
|
2023-03-05 04:10:56 +00:00
|
|
|
};
|
|
|
|
|
2023-03-18 22:47:34 +00:00
|
|
|
interface PaginationParams {
|
|
|
|
since?: number;
|
|
|
|
until?: number;
|
|
|
|
limit?: number;
|
|
|
|
}
|
|
|
|
|
2023-04-29 20:54:21 +00:00
|
|
|
/** Get events from people the user follows. */
|
2023-05-01 01:21:55 +00:00
|
|
|
async function getFeed(event3: Event<3>, params: PaginationParams = {}): Promise<SignedEvent<1>[]> {
|
2023-03-18 23:09:16 +00:00
|
|
|
const limit = Math.max(params.limit ?? 20, 40);
|
2023-05-01 01:21:55 +00:00
|
|
|
|
|
|
|
const authors = event3.tags
|
|
|
|
.filter((tag) => tag[0] === 'p')
|
|
|
|
.map((tag) => tag[1]);
|
|
|
|
|
2023-03-18 21:39:34 +00:00
|
|
|
authors.push(event3.pubkey); // see own events in feed
|
2023-03-18 19:49:44 +00:00
|
|
|
|
2023-05-01 01:21:55 +00:00
|
|
|
const filter: Filter = {
|
|
|
|
authors,
|
|
|
|
kinds: [1],
|
|
|
|
since: params.since,
|
|
|
|
until: params.until ?? nostrNow(),
|
|
|
|
limit,
|
|
|
|
};
|
2023-03-18 22:47:34 +00:00
|
|
|
|
2023-05-01 01:21:55 +00:00
|
|
|
const results = await getFilter(filter, { timeout: 5000 }) as SignedEvent<1>[];
|
|
|
|
return results.sort(eventDateComparator);
|
2023-03-18 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 01:23:51 +00:00
|
|
|
async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> {
|
|
|
|
if (result.length < 100) {
|
2023-04-30 01:33:52 +00:00
|
|
|
const replyTag = findReplyTag(event);
|
2023-04-30 01:23:51 +00:00
|
|
|
const inReplyTo = replyTag ? replyTag[1] : undefined;
|
|
|
|
|
|
|
|
if (inReplyTo) {
|
|
|
|
const parentEvent = await getEvent(inReplyTo, 1);
|
|
|
|
|
|
|
|
if (parentEvent) {
|
|
|
|
result.push(parentEvent);
|
|
|
|
return getAncestors(parentEvent, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDescendants(eventId: string): Promise<SignedEvent<1>[]> {
|
2023-05-01 01:21:55 +00:00
|
|
|
return getFilter({ kinds: [1], '#e': [eventId], limit: 200 }, { timeout: 2000 }) as Promise<SignedEvent<1>[]>;
|
2023-04-30 01:23:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 20:22:24 +00:00
|
|
|
export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFilter, getFollows, pool };
|