Adds checks in the filter to highlight issues with empty or invalid filters

This commit is contained in:
Vitor Pamplona
2025-07-09 17:20:59 -04:00
parent accc8d4439
commit 35005dddad
2 changed files with 33 additions and 13 deletions

View File

@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.quartz.nip01Core.relay.client.pool 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.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@@ -29,22 +30,16 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
class RelayBasedFilter( class RelayBasedFilter(
val relay: NormalizedRelayUrl, val relay: NormalizedRelayUrl,
val filter: Filter, 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<RelayBasedFilter>.groupByRelay(): Map<NormalizedRelayUrl, List<Filter>> { fun List<RelayBasedFilter>.groupByRelay(): Map<NormalizedRelayUrl, List<Filter>> {
val result = mutableMapOf<NormalizedRelayUrl, MutableList<Filter>>() val result = mutableMapOf<NormalizedRelayUrl, MutableList<Filter>>()
for (relayBasedFilter in this) { 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 return result
} }

View File

@@ -20,7 +20,9 @@
*/ */
package com.vitorpamplona.quartz.nip01Core.relay.filters package com.vitorpamplona.quartz.nip01Core.relay.filters
import android.util.Log
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.tags.addressables.Address
class Filter( class Filter(
val ids: List<String>? = null, val ids: List<String>? = null,
@@ -51,9 +53,32 @@ class Filter(
(ids != null && ids.isNotEmpty()) || (ids != null && ids.isNotEmpty()) ||
(authors != null && authors.isNotEmpty()) || (authors != null && authors.isNotEmpty()) ||
(kinds != null && kinds.isNotEmpty()) || (kinds != null && kinds.isNotEmpty()) ||
(tags != null && tags.isNotEmpty()) || (tags != null && tags.isNotEmpty() && tags.values.all { it.isNotEmpty() }) ||
(since != null) || (since != null) ||
(until != null) || (until != null) ||
(limit != null) || (limit != null) ||
(search != null && search.isNotEmpty()) (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()}")
}
}
} }