Renames JsonFilter to just Filter and adds a matching function

This commit is contained in:
Vitor Pamplona
2024-06-25 10:06:06 -04:00
parent c448e75953
commit 79ace7f18c
20 changed files with 311 additions and 232 deletions

View File

@@ -22,7 +22,7 @@ package com.vitorpamplona.ammolite.relays
import com.vitorpamplona.quartz.events.Event
class JsonFilter(
class Filter(
val ids: List<String>? = null,
val authors: List<String>? = null,
val kinds: List<Int>? = null,
@@ -84,4 +84,20 @@ class JsonFilter(
}
return Event.mapper.writeValueAsString(filter)
}
fun match(
event: Event,
forRelay: String? = null,
): Boolean {
if (ids?.any { event.id == it } == false) return false
if (kinds?.any { event.kind == it } == false) return false
if (authors?.any { event.pubKey == it } == false) return false
tags?.forEach { tag ->
if (!event.tags.any { it.first() == tag.key && it[1] in tag.value }) return false
}
if (event.createdAt !in (since?.get(forRelay)?.time ?: Long.MIN_VALUE)..(until ?: Long.MAX_VALUE)) {
return false
}
return true
}
}

View File

@@ -22,5 +22,5 @@ package com.vitorpamplona.ammolite.relays
class TypedFilter(
val types: Set<FeedType>,
val filter: JsonFilter,
val filter: Filter,
)