From 35005dddad75f3a26b0a6ee7bb923ca220d4d4a2 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 9 Jul 2025 17:20:59 -0400 Subject: [PATCH] Adds checks in the filter to highlight issues with empty or invalid filters --- .../relay/client/pool/RelayBasedFilter.kt | 19 +++++-------- .../quartz/nip01Core/relay/filters/Filter.kt | 27 ++++++++++++++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/pool/RelayBasedFilter.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/pool/RelayBasedFilter.kt index b5ebfe358..ed0fcfa9c 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/pool/RelayBasedFilter.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/pool/RelayBasedFilter.kt @@ -20,6 +20,7 @@ */ package com.vitorpamplona.quartz.nip01Core.relay.client.pool +import android.util.Log import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl @@ -29,22 +30,16 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl class RelayBasedFilter( val relay: NormalizedRelayUrl, val filter: Filter, -) { - // This only exists because some relays confuse empty lists with null lists - fun isValidFor(relay: NormalizedRelayUrl) = relay == this.relay - - fun toFilter(relay: NormalizedRelayUrl): Filter? = - if (isValidFor(relay)) { - filter - } else { - null - } -} +) fun List.groupByRelay(): Map> { val result = mutableMapOf>() for (relayBasedFilter in this) { - result.getOrPut(relayBasedFilter.relay) { mutableListOf() }.add(relayBasedFilter.filter) + if (relayBasedFilter.filter.isFilledFilter()) { + result.getOrPut(relayBasedFilter.relay) { mutableListOf() }.add(relayBasedFilter.filter) + } else { + Log.e("FilterError", "Ignoring empty filter for ${relayBasedFilter.relay}") + } } return result } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt index 90e5d8356..06bea99c5 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/filters/Filter.kt @@ -20,7 +20,9 @@ */ package com.vitorpamplona.quartz.nip01Core.relay.filters +import android.util.Log import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.tags.addressables.Address class Filter( val ids: List? = null, @@ -51,9 +53,32 @@ class Filter( (ids != null && ids.isNotEmpty()) || (authors != null && authors.isNotEmpty()) || (kinds != null && kinds.isNotEmpty()) || - (tags != null && tags.isNotEmpty()) || + (tags != null && tags.isNotEmpty() && tags.values.all { it.isNotEmpty() }) || (since != null) || (until != null) || (limit != null) || (search != null && search.isNotEmpty()) + + init { + if (!isFilledFilter()) { + Log.e("FilterError", "Filter is empty: ${toJson()}") + } + + ids?.forEach { + if (it.length != 64) Log.e("FilterError", "Invalid id length $it on ${toJson()}") + } + authors?.forEach { + 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()}") + } + 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()}") + } + } }