Merge branch 'rm-empty-tag-filters' into 'main'
Remove empty tag filters See merge request soapbox-pub/ditto!337
This commit is contained in:
commit
f98ebd359a
|
@ -1,6 +1,5 @@
|
||||||
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
import { NKinds, 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';
|
||||||
|
|
||||||
/** Microfilter to get one specific event by ID. */
|
/** Microfilter to get one specific event by ID. */
|
||||||
|
@ -65,6 +64,25 @@ function normalizeFilters<F extends NostrFilter>(filters: F[]): F[] {
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Calculate the intrinsic limit of a filter. This function may return `Infinity`. */
|
||||||
|
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;
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(filter)) {
|
||||||
|
if (key[0] === '#' && Array.isArray(value) && !value.length) return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(
|
||||||
|
Math.max(0, filter.limit ?? Infinity),
|
||||||
|
filter.ids?.length ?? Infinity,
|
||||||
|
filter.authors?.length && filter.kinds?.every((kind) => NKinds.replaceable(kind))
|
||||||
|
? filter.authors.length * filter.kinds.length
|
||||||
|
: Infinity,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
type AuthorMicrofilter,
|
type AuthorMicrofilter,
|
||||||
canFilter,
|
canFilter,
|
||||||
|
|
Loading…
Reference in New Issue