db/events: refactor filterIndexableTags function

This commit is contained in:
Alex Gleason 2023-09-09 16:24:53 -05:00
parent f8b2efb484
commit c6b20e68f6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 17 additions and 6 deletions

View File

@ -194,21 +194,32 @@ async function countFilters<K extends number>(filters: DittoFilter<K>[]): Promis
function filterIndexableTags(event: Event, data: EventData): string[][] { function filterIndexableTags(event: Event, data: EventData): string[][] {
const tagCounts: Record<string, number> = {}; const tagCounts: Record<string, number> = {};
return event.tags.reduce<string[][]>((results, tag) => { function getCount(name: string) {
const [name, value] = tag; return tagCounts[name] || 0;
tagCounts[name] = (tagCounts[name] || 0) + 1; }
const shouldIndex = tagConditions[name]?.({ function incrementCount(name: string) {
tagCounts[name] = getCount(name) + 1;
}
function checkCondition(name: string, value: string, condition: TagCondition) {
return condition({
event, event,
data, data,
count: tagCounts[name] - 1, count: getCount(name),
value, value,
}); });
}
if (value && value.length < 200 && shouldIndex) { return event.tags.reduce<string[][]>((results, tag) => {
const [name, value] = tag;
const condition = tagConditions[name] as TagCondition | undefined;
if (value && condition && value.length < 200 && checkCondition(name, value, condition)) {
results.push(tag); results.push(tag);
} }
incrementCount(name);
return results; return results;
}, []); }, []);
} }