From 41e974c31c6ef854163a3de7f5d066752c3786ae Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 2 Feb 2025 22:00:28 -0600 Subject: [PATCH] getIdsBySearch: AND and OR logic --- deno.json | 2 +- deno.lock | 8 ++++---- src/utils/search.ts | 17 ++++++++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/deno.json b/deno.json index a77a9b7c..f64b0a1f 100644 --- a/deno.json +++ b/deno.json @@ -45,7 +45,7 @@ "@lambdalisue/async": "jsr:@lambdalisue/async@^2.1.1", "@negrel/webpush": "jsr:@negrel/webpush@^0.3.0", "@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0", - "@nostrify/db": "jsr:@nostrify/db@^0.37.2", + "@nostrify/db": "jsr:@nostrify/db@^0.37.3", "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.38.0", "@nostrify/policies": "jsr:@nostrify/policies@^0.36.1", "@nostrify/types": "jsr:@nostrify/types@^0.36.0", diff --git a/deno.lock b/deno.lock index 0f563977..a3b02cbb 100644 --- a/deno.lock +++ b/deno.lock @@ -30,7 +30,7 @@ "jsr:@lambdalisue/async@^2.1.1": "2.1.1", "jsr:@negrel/http-ece@0.6.0": "0.6.0", "jsr:@negrel/webpush@0.3": "0.3.0", - "jsr:@nostrify/db@~0.37.2": "0.37.2", + "jsr:@nostrify/db@~0.37.3": "0.37.3", "jsr:@nostrify/nostrify@0.31": "0.31.0", "jsr:@nostrify/nostrify@0.32": "0.32.0", "jsr:@nostrify/nostrify@0.36": "0.36.2", @@ -348,8 +348,8 @@ "jsr:@std/path@0.224.0" ] }, - "@nostrify/db@0.37.2": { - "integrity": "1dba9a4664bccd504de34dbdcb792377808bd8affb2712906beb687056602ce7", + "@nostrify/db@0.37.3": { + "integrity": "fe7cacd67bb817f10fb44587e832cfb042a3a0d32db29b24a487b7d006438623", "dependencies": [ "jsr:@nostrify/nostrify@0.38", "jsr:@nostrify/types@0.36", @@ -2348,7 +2348,7 @@ "jsr:@hono/hono@^4.4.6", "jsr:@lambdalisue/async@^2.1.1", "jsr:@negrel/webpush@0.3", - "jsr:@nostrify/db@~0.37.2", + "jsr:@nostrify/db@~0.37.3", "jsr:@nostrify/nostrify@0.38", "jsr:@nostrify/policies@~0.36.1", "jsr:@nostrify/types@0.36", diff --git a/src/utils/search.ts b/src/utils/search.ts index a39f23ff..17a625bd 100644 --- a/src/utils/search.ts +++ b/src/utils/search.ts @@ -53,7 +53,7 @@ export async function getIdsBySearch( const tokens = NIP50.parseInput(q); - const ext = tokens.filter((token) => typeof token === 'object'); + const ext: Record = {}; const txt = tokens.filter((token) => typeof token === 'string').join(' '); let query = kysely @@ -72,12 +72,19 @@ export async function getIdsBySearch( } } - if (ext.length) { + for (const token of tokens) { + if (typeof token === 'object') { + ext[token.key] ??= []; + ext[token.key].push(token.value); + } + } + + for (const [key, values] of Object.entries(ext)) { + if (key === 'domain') continue; + query = query.where((eb) => eb.or( - ext - .filter((token) => token.key !== 'domain') - .map(({ key, value }) => eb('search_ext', '@>', { [key]: value })), + values.map((value) => eb('nostr_events.search_ext', '@>', { [key]: value })), ) ); }