From df97b7c4447d615b14056effd6b24a1c5c0d351c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 30 Dec 2025 12:09:05 -0500 Subject: [PATCH] Improves efficiency of the filter class for matching and checking errors --- .../quartz/nip01Core/relay/filters/Filter.kt | 17 ++++++++++------- .../nip01Core/relay/filters/FilterMatcher.kt | 4 +++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt index a34cd01fb..f4d1f5aad 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt @@ -89,14 +89,17 @@ class Filter( if (it.length != 64) Log.e("FilterError", "Invalid author length $it on ${toJson()}") } // tests common tags. - tags?.get("p")?.forEach { - if (it.length != 64) Log.e("FilterError", "Invalid p-tag length $it on ${toJson()}") + if (tags != null) { + tags["p"]?.forEach { + if (it.length != 64) Log.e("FilterError", "Invalid p-tag length $it on ${toJson()}") + } + tags["e"]?.forEach { + if (it.length != 64) Log.e("FilterError", "Invalid e-tag length $it on ${toJson()}") + } + tags["a"]?.forEach { + if (Address.parse(it) == null) Log.e("FilterError", "Invalid a-tag $it on ${toJson()}") + } } - tags?.get("e")?.forEach { - if (it.length != 64) Log.e("FilterError", "Invalid e-tag length $it on ${toJson()}") - } - tags?.get("a")?.forEach { - if (Address.parse(it) == null) Log.e("FilterError", "Invalid a-tag $it on ${toJson()}") } } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/FilterMatcher.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/FilterMatcher.kt index 1367984ee..7c7a579c2 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/FilterMatcher.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/filters/FilterMatcher.kt @@ -36,7 +36,9 @@ object FilterMatcher { if (kinds?.contains(event.kind) == false) return false if (authors?.contains(event.pubKey) == false) return false tags?.forEach { tag -> - if (!event.tags.any { it.first() == tag.key && it[1] in tag.value }) return false + val valueSet = tag.value.toSet() + // AND between keys, OR between values + if (!event.tags.any { it.size > 1 && it[0] == tag.key && it[1] in valueSet }) return false } if (event.createdAt !in (since ?: Long.MIN_VALUE)..(until ?: Long.MAX_VALUE)) { return false