filter: use getFilterLimit from nostr-tools
This commit is contained in:
parent
091392088f
commit
6b20104327
|
@ -1,9 +1,8 @@
|
||||||
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
||||||
import stringifyStable from 'fast-stable-stringify';
|
import stringifyStable from 'fast-stable-stringify';
|
||||||
|
import { getFilterLimit } from 'nostr-tools';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { isReplaceableKind } from '@/kinds.ts';
|
|
||||||
|
|
||||||
/** Microfilter to get one specific event by ID. */
|
/** Microfilter to get one specific event by ID. */
|
||||||
type IdMicrofilter = { ids: [NostrEvent['id']] };
|
type IdMicrofilter = { ids: [NostrEvent['id']] };
|
||||||
/** Microfilter to get an author. */
|
/** Microfilter to get an author. */
|
||||||
|
@ -50,22 +49,6 @@ function isMicrofilter(filter: NostrFilter): filter is MicroFilter {
|
||||||
return microFilterSchema.safeParse(filter).success;
|
return microFilterSchema.safeParse(filter).success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Calculate the intrinsic limit of a filter. */
|
|
||||||
function getFilterLimit(filter: NostrFilter): number {
|
|
||||||
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. */
|
/** Returns true if the filter could potentially return any stored events at all. */
|
||||||
function canFilter(filter: NostrFilter): boolean {
|
function canFilter(filter: NostrFilter): boolean {
|
||||||
return getFilterLimit(filter) > 0;
|
return getFilterLimit(filter) > 0;
|
||||||
|
|
Loading…
Reference in New Issue