2023-08-18 00:32:05 +00:00
|
|
|
import * as eventsDB from '@/db/events.ts';
|
2023-12-06 18:04:24 +00:00
|
|
|
import { type Event, findReplyTag } from '@/deps.ts';
|
|
|
|
import { type DittoFilter, type Relation } from '@/filter.ts';
|
2023-08-18 00:32:05 +00:00
|
|
|
import * as mixer from '@/mixer.ts';
|
2023-12-28 02:07:13 +00:00
|
|
|
import { reqmeister } from '@/reqmeister.ts';
|
2023-08-17 18:12:24 +00:00
|
|
|
|
2023-08-17 19:51:27 +00:00
|
|
|
interface GetEventOpts<K extends number> {
|
2023-12-22 16:38:48 +00:00
|
|
|
/** Signal to abort the request. */
|
|
|
|
signal?: AbortSignal;
|
2023-08-17 19:51:27 +00:00
|
|
|
/** Event kind. */
|
|
|
|
kind?: K;
|
2023-12-06 18:04:24 +00:00
|
|
|
/** Relations to include on the event. */
|
|
|
|
relations?: Relation[];
|
2023-08-17 19:51:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:12:24 +00:00
|
|
|
/** Get a Nostr event by its ID. */
|
2023-08-17 19:51:27 +00:00
|
|
|
const getEvent = async <K extends number = number>(
|
|
|
|
id: string,
|
|
|
|
opts: GetEventOpts<K> = {},
|
|
|
|
): Promise<Event<K> | undefined> => {
|
2023-12-22 16:38:48 +00:00
|
|
|
const { kind, relations, signal = AbortSignal.timeout(1000) } = opts;
|
2023-12-06 18:04:24 +00:00
|
|
|
const filter: DittoFilter<K> = { ids: [id], relations, limit: 1 };
|
2023-08-17 19:51:27 +00:00
|
|
|
if (kind) {
|
|
|
|
filter.kinds = [kind];
|
|
|
|
}
|
2023-12-22 16:38:48 +00:00
|
|
|
const [event] = await mixer.getFilters([filter], { limit: 1, signal });
|
2023-08-17 18:12:24 +00:00
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
|
2023-12-10 22:21:18 +00:00
|
|
|
const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise<Event<0> | undefined> => {
|
2023-12-22 16:38:48 +00:00
|
|
|
const { relations, signal = AbortSignal.timeout(1000) } = opts;
|
2023-12-10 22:21:18 +00:00
|
|
|
|
2023-12-22 01:10:42 +00:00
|
|
|
const event = await eventsDB.getFilters(
|
2023-12-10 22:21:18 +00:00
|
|
|
[{ authors: [pubkey], relations, kinds: [0], limit: 1 }],
|
2023-12-22 16:38:48 +00:00
|
|
|
{ limit: 1, signal },
|
2023-12-22 01:10:42 +00:00
|
|
|
).then(([event]) => event) || await reqmeister.req({ kinds: [0], authors: [pubkey] }).catch(() => {});
|
2023-12-10 22:21:18 +00:00
|
|
|
|
2023-08-17 18:12:24 +00:00
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Get users the given pubkey follows. */
|
2023-12-22 16:38:48 +00:00
|
|
|
const getFollows = async (pubkey: string, signal = AbortSignal.timeout(1000)): Promise<Event<3> | undefined> => {
|
|
|
|
const [event] = await mixer.getFilters([{ authors: [pubkey], kinds: [3], limit: 1 }], { limit: 1, signal });
|
2023-08-17 18:12:24 +00:00
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
2023-08-28 18:34:15 +00:00
|
|
|
/** Get pubkeys the user follows. */
|
2023-12-22 16:38:48 +00:00
|
|
|
async function getFollowedPubkeys(pubkey: string, signal?: AbortSignal): Promise<string[]> {
|
|
|
|
const event = await getFollows(pubkey, signal);
|
2023-08-28 18:34:15 +00:00
|
|
|
if (!event) return [];
|
2023-08-17 18:15:47 +00:00
|
|
|
|
2023-08-28 18:34:15 +00:00
|
|
|
return event.tags
|
2023-08-17 18:12:24 +00:00
|
|
|
.filter((tag) => tag[0] === 'p')
|
|
|
|
.map((tag) => tag[1]);
|
2023-08-28 18:34:15 +00:00
|
|
|
}
|
2023-08-17 18:12:24 +00:00
|
|
|
|
2023-08-28 18:34:15 +00:00
|
|
|
/** Get pubkeys the user follows, including the user's own pubkey. */
|
|
|
|
async function getFeedPubkeys(pubkey: string): Promise<string[]> {
|
|
|
|
const authors = await getFollowedPubkeys(pubkey);
|
|
|
|
return [...authors, pubkey];
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:12:24 +00:00
|
|
|
async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> {
|
|
|
|
if (result.length < 100) {
|
|
|
|
const replyTag = findReplyTag(event);
|
|
|
|
const inReplyTo = replyTag ? replyTag[1] : undefined;
|
|
|
|
|
|
|
|
if (inReplyTo) {
|
2023-12-10 22:21:18 +00:00
|
|
|
const parentEvent = await getEvent(inReplyTo, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] });
|
2023-08-17 18:12:24 +00:00
|
|
|
|
|
|
|
if (parentEvent) {
|
|
|
|
result.push(parentEvent);
|
|
|
|
return getAncestors(parentEvent, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.reverse();
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:38:48 +00:00
|
|
|
function getDescendants(eventId: string, signal = AbortSignal.timeout(2000)): Promise<Event<1>[]> {
|
2023-12-10 22:21:18 +00:00
|
|
|
return mixer.getFilters(
|
|
|
|
[{ kinds: [1], '#e': [eventId], relations: ['author', 'event_stats', 'author_stats'] }],
|
2023-12-22 16:38:48 +00:00
|
|
|
{ limit: 200, signal },
|
2023-12-10 22:21:18 +00:00
|
|
|
);
|
2023-08-17 18:12:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 00:32:05 +00:00
|
|
|
/** Returns whether the pubkey is followed by a local user. */
|
|
|
|
async function isLocallyFollowed(pubkey: string): Promise<boolean> {
|
2023-08-19 18:34:35 +00:00
|
|
|
const [event] = await eventsDB.getFilters([{ kinds: [3], '#p': [pubkey], local: true, limit: 1 }], { limit: 1 });
|
2023-08-18 00:32:05 +00:00
|
|
|
return Boolean(event);
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:49:06 +00:00
|
|
|
export {
|
|
|
|
getAncestors,
|
|
|
|
getAuthor,
|
|
|
|
getDescendants,
|
|
|
|
getEvent,
|
|
|
|
getFeedPubkeys,
|
|
|
|
getFollowedPubkeys,
|
|
|
|
getFollows,
|
|
|
|
isLocallyFollowed,
|
|
|
|
};
|