2023-03-05 04:10:56 +00:00
|
|
|
import { Author, RelayPool } from '@/deps.ts';
|
|
|
|
|
|
|
|
import { poolRelays } from './config.ts';
|
|
|
|
|
2023-03-18 19:49:44 +00:00
|
|
|
import type { Event, SignedEvent } from './event.ts';
|
2023-03-18 22:47:34 +00:00
|
|
|
import { eventDateComparator } from './utils.ts';
|
2023-03-05 04:10:56 +00:00
|
|
|
|
|
|
|
const pool = new RelayPool(poolRelays);
|
|
|
|
|
|
|
|
/** Fetch a Nostr event by its ID. */
|
2023-03-18 19:49:44 +00:00
|
|
|
const fetchEvent = async (id: string): Promise<SignedEvent | null> => {
|
|
|
|
const event = await (pool.getEventById(id, poolRelays, 0) as Promise<SignedEvent>);
|
2023-03-05 04:10:56 +00:00
|
|
|
return event?.id === id ? event : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Fetch a Nostr `set_medatadata` event for a user's pubkey. */
|
2023-03-18 19:49:44 +00:00
|
|
|
const fetchUser = async (pubkey: string): Promise<SignedEvent<0> | null> => {
|
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-03-05 04:10:56 +00:00
|
|
|
return event?.pubkey === pubkey ? event : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Fetch users the given pubkey follows. */
|
2023-03-18 19:49:44 +00:00
|
|
|
const fetchFollows = (pubkey: string): Promise<SignedEvent<3> | null> => {
|
2023-03-05 04:10:56 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
pool.subscribe(
|
|
|
|
[{ authors: [pubkey], kinds: [3] }],
|
|
|
|
poolRelays,
|
2023-03-18 19:49:44 +00:00
|
|
|
(event: SignedEvent<3> | null) => {
|
2023-03-05 04:10:56 +00:00
|
|
|
resolve(event?.pubkey === pubkey ? event : null);
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-18 22:47:34 +00:00
|
|
|
interface PaginationParams {
|
|
|
|
since?: number;
|
|
|
|
until?: number;
|
|
|
|
limit?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fetch events from people the user follows. */
|
|
|
|
function fetchFeed(event3: Event<3>, params: PaginationParams = {}): Promise<SignedEvent<1>[]> {
|
|
|
|
const limit = params.limit ?? 20;
|
2023-03-18 19:49:44 +00:00
|
|
|
const authors = event3.tags.filter((tag) => tag[0] === 'p').map((tag) => tag[1]);
|
|
|
|
const results: SignedEvent<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
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
pool.subscribe(
|
2023-03-18 22:47:34 +00:00
|
|
|
[{
|
|
|
|
authors,
|
|
|
|
kinds: [1],
|
|
|
|
since: params.since,
|
|
|
|
until: params.until,
|
|
|
|
limit,
|
|
|
|
}],
|
2023-03-18 19:49:44 +00:00
|
|
|
poolRelays,
|
|
|
|
(event: SignedEvent<1> | null) => {
|
|
|
|
if (event) {
|
|
|
|
results.push(event);
|
2023-03-18 22:47:34 +00:00
|
|
|
|
|
|
|
if (results.length >= limit) {
|
|
|
|
resolve(results.slice(0, limit).sort(eventDateComparator));
|
|
|
|
}
|
2023-03-18 19:49:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
void 0,
|
2023-03-18 22:47:34 +00:00
|
|
|
() => resolve(results.sort(eventDateComparator)),
|
2023-03-18 19:49:44 +00:00
|
|
|
{ unsubscribeOnEose: true },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { fetchEvent, fetchFeed, fetchFollows, fetchUser, pool };
|