2024-05-02 00:51:12 +00:00
|
|
|
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
2024-05-01 21:42:07 +00:00
|
|
|
import stringifyStable from 'fast-stable-stringify';
|
2024-04-23 20:03:20 +00:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
2024-01-03 22:35:40 +00:00
|
|
|
import { isReplaceableKind } from '@/kinds.ts';
|
2023-08-25 18:38:21 +00:00
|
|
|
|
2023-12-28 05:55:42 +00:00
|
|
|
/** Microfilter to get one specific event by ID. */
|
2024-01-23 18:07:22 +00:00
|
|
|
type IdMicrofilter = { ids: [NostrEvent['id']] };
|
2023-12-28 05:55:42 +00:00
|
|
|
/** Microfilter to get an author. */
|
2024-01-23 18:07:22 +00:00
|
|
|
type AuthorMicrofilter = { kinds: [0]; authors: [NostrEvent['pubkey']] };
|
2023-12-22 01:10:42 +00:00
|
|
|
/** Filter to get one specific event. */
|
2023-12-28 05:55:42 +00:00
|
|
|
type MicroFilter = IdMicrofilter | AuthorMicrofilter;
|
2023-12-22 01:10:42 +00:00
|
|
|
|
|
|
|
/** Get deterministic ID for a microfilter. */
|
|
|
|
function getFilterId(filter: MicroFilter): string {
|
|
|
|
if ('ids' in filter) {
|
2023-12-28 03:14:45 +00:00
|
|
|
return stringifyStable({ ids: [filter.ids[0]] });
|
2023-12-22 01:10:42 +00:00
|
|
|
} else {
|
|
|
|
return stringifyStable({
|
|
|
|
kinds: [filter.kinds[0]],
|
|
|
|
authors: [filter.authors[0]],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a microfilter from a Nostr event. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function eventToMicroFilter(event: NostrEvent): MicroFilter {
|
2023-12-28 05:22:24 +00:00
|
|
|
const [microfilter] = getMicroFilters(event);
|
|
|
|
return microfilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get all the microfilters for an event, in order of priority. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function getMicroFilters(event: NostrEvent): MicroFilter[] {
|
2023-12-28 05:22:24 +00:00
|
|
|
const microfilters: MicroFilter[] = [];
|
2023-12-22 01:10:42 +00:00
|
|
|
if (event.kind === 0) {
|
2023-12-28 05:22:24 +00:00
|
|
|
microfilters.push({ kinds: [0], authors: [event.pubkey] });
|
2023-12-22 01:10:42 +00:00
|
|
|
}
|
2023-12-28 05:22:24 +00:00
|
|
|
microfilters.push({ ids: [event.id] });
|
|
|
|
return microfilters;
|
2023-12-22 01:10:42 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 04:49:35 +00:00
|
|
|
/** Microfilter schema. */
|
|
|
|
const microFilterSchema = z.union([
|
2024-05-02 00:51:12 +00:00
|
|
|
z.object({ ids: z.tuple([n.id()]) }).strict(),
|
|
|
|
z.object({ kinds: z.tuple([z.literal(0)]), authors: z.tuple([n.id()]) }).strict(),
|
2023-12-28 04:49:35 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
/** Checks whether the filter is a microfilter. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function isMicrofilter(filter: NostrFilter): filter is MicroFilter {
|
2023-12-28 04:49:35 +00:00
|
|
|
return microFilterSchema.safeParse(filter).success;
|
|
|
|
}
|
|
|
|
|
2024-01-03 22:35:40 +00:00
|
|
|
/** Calculate the intrinsic limit of a filter. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function getFilterLimit(filter: NostrFilter): number {
|
2024-01-03 22:35:40 +00:00
|
|
|
if (filter.ids && !filter.ids.length) return 0;
|
|
|
|
if (filter.kinds && !filter.kinds.length) return 0;
|
|
|
|
if (filter.authors && !filter.authors.length) return 0;
|
|
|
|
|
|
|
|
return Math.min(
|
|
|
|
Math.max(0, filter.limit ?? Infinity),
|
|
|
|
filter.ids?.length ?? Infinity,
|
|
|
|
filter.authors?.length &&
|
|
|
|
filter.kinds?.every((kind) => isReplaceableKind(kind))
|
|
|
|
? filter.authors.length * filter.kinds.length
|
|
|
|
: Infinity,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the filter could potentially return any stored events at all. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function canFilter(filter: NostrFilter): boolean {
|
2024-01-03 22:35:40 +00:00
|
|
|
return getFilterLimit(filter) > 0;
|
|
|
|
}
|
|
|
|
|
2024-01-04 00:15:28 +00:00
|
|
|
/** Normalize the `limit` of each filter, and remove filters that can't produce any events. */
|
2024-01-23 18:07:22 +00:00
|
|
|
function normalizeFilters<F extends NostrFilter>(filters: F[]): F[] {
|
2024-01-04 00:15:28 +00:00
|
|
|
return filters.reduce<F[]>((acc, filter) => {
|
|
|
|
const limit = getFilterLimit(filter);
|
|
|
|
if (limit > 0) {
|
|
|
|
acc.push(limit === Infinity ? filter : { ...filter, limit });
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
2023-12-22 01:10:42 +00:00
|
|
|
export {
|
2023-12-28 05:55:42 +00:00
|
|
|
type AuthorMicrofilter,
|
2024-01-03 22:35:40 +00:00
|
|
|
canFilter,
|
2023-12-22 01:10:42 +00:00
|
|
|
eventToMicroFilter,
|
|
|
|
getFilterId,
|
2024-01-03 22:35:40 +00:00
|
|
|
getFilterLimit,
|
2023-12-28 05:22:24 +00:00
|
|
|
getMicroFilters,
|
2023-12-28 05:55:42 +00:00
|
|
|
type IdMicrofilter,
|
2023-12-28 04:49:35 +00:00
|
|
|
isMicrofilter,
|
2023-12-22 01:10:42 +00:00
|
|
|
type MicroFilter,
|
2024-01-04 00:15:28 +00:00
|
|
|
normalizeFilters,
|
2023-12-22 01:10:42 +00:00
|
|
|
};
|