Moves limits and Filters to quartz

This commit is contained in:
Vitor Pamplona
2025-01-13 12:46:15 -05:00
parent b871954293
commit 57fe3740fc
6 changed files with 63 additions and 43 deletions
@@ -18,7 +18,7 @@
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip19bech32
package com.vitorpamplona.quartz.nip19Bech32
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.experimental.medical.FhirResourceEvent
@@ -28,8 +28,6 @@ import com.vitorpamplona.quartz.nip01Core.hasValidSignature
import com.vitorpamplona.quartz.nip01Core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import com.vitorpamplona.quartz.nip19Bech32.Nip19Parser
import com.vitorpamplona.quartz.nip19Bech32.decodePrivateKeyAsHexOrNull
import com.vitorpamplona.quartz.nip19Bech32.entities.NEmbed
import com.vitorpamplona.quartz.utils.Hex
import junit.framework.TestCase.assertEquals
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.experimental.limits
import com.vitorpamplona.ammolite.relays.filters.Filter
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip13Pow.getPoWRank
import com.vitorpamplona.quartz.utils.TimeUtils
class LimitProcessor {
fun wrapFilterToLimits(
filter: Filter,
sendingStr: String,
limits: Limits,
): Filter? {
var newFilter: Filter? = filter
if (limits.canRead != null && !limits.canRead) {
newFilter = null
}
if (limits.maxLimit != null && filter.limit != null && filter.limit > limits.maxLimit) {
newFilter = filter.copy(limit = limits.maxLimit)
}
if (!limits.acceptedEventKinds.isNullOrEmpty() && !filter.kinds.isNullOrEmpty()) {
val intersect = filter.kinds.filter { it in limits.acceptedEventKinds }
if (intersect.isNotEmpty()) {
newFilter = filter.copy(kinds = intersect)
} else {
newFilter = null
}
}
if (!limits.blockedEventKinds.isNullOrEmpty() && !filter.kinds.isNullOrEmpty()) {
val intersect = filter.kinds.filter { it !in limits.blockedEventKinds }
if (intersect.isNotEmpty()) {
newFilter = filter.copy(kinds = intersect)
} else {
newFilter = null
}
}
if (limits.maxMessageLength != null && sendingStr.length > limits.maxMessageLength) {
// TODO: figure out how to dynamically reduce filter size
newFilter = null
}
return newFilter
}
fun canSendEvent(
ev: Event,
sendingStr: String,
limits: Limits,
): Boolean {
if (limits.canWrite != null && !limits.canWrite) return false
if (!limits.acceptedEventKinds.isNullOrEmpty() && ev.kind !in limits.acceptedEventKinds) return false
if (!limits.blockedEventKinds.isNullOrEmpty() && ev.kind in limits.blockedEventKinds) return false
if (limits.minPoW != null && ev.getPoWRank() < limits.minPoW) return false
if (limits.maxEventTags != null && ev.tags.size > limits.maxEventTags) return false
if (limits.maxContentLength != null && ev.content.length > limits.maxContentLength) return false
if (limits.createdAtMillisecsAgo != null && ev.createdAt < TimeUtils.now() - limits.createdAtMillisecsAgo) return false
if (limits.createdAtMillisecsAhead != null && ev.createdAt > TimeUtils.now() + limits.createdAtMillisecsAhead) return false
if (limits.requiredTags != null && !matchAll(ev, limits.requiredTags)) return false
if (limits.maxMessageLength != null && sendingStr.length > limits.maxMessageLength) return false
return true
}
private fun matchAll(
ev: Event,
requiredTags: Array<Array<String>>,
): Boolean =
requiredTags.all { requiredTag ->
if (requiredTag.isNotEmpty()) {
if (requiredTag.getOrNull(1) == null) {
ev.tags.any { eventTag ->
eventTag.getOrNull(0) == requiredTag[0]
}
} else {
ev.tags.any { eventTag ->
eventTag.getOrNull(0) == requiredTag[0] && eventTag.getOrNull(1) == requiredTag[1]
}
}
} else {
true
}
}
}
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.experimental.limits
import com.fasterxml.jackson.annotation.JsonProperty
class Limits(
@JsonProperty("can_write")
val canWrite: Boolean?,
@JsonProperty("can_read")
val canRead: Boolean?,
@JsonProperty("accepted_event_kinds")
val acceptedEventKinds: Set<Int>?,
@JsonProperty("blocked_event_kinds")
val blockedEventKinds: Set<Int>?,
@JsonProperty("min_pow_difficulty")
val minPoW: Int?,
@JsonProperty("max_message_length")
val maxMessageLength: Int?,
@JsonProperty("max_subscriptions")
val maxSubscriptions: Int?,
@JsonProperty("max_filters")
val maxFilters: Int?,
@JsonProperty("max_limit")
val maxLimit: Int?,
@JsonProperty("max_event_tags")
val maxEventTags: Int?,
@JsonProperty("max_content_length")
val maxContentLength: Int?,
@JsonProperty("created_at_msecs_ago")
val createdAtMillisecsAgo: Long?,
@JsonProperty("created_at_msecs_ahead")
val createdAtMillisecsAhead: Long?,
@JsonProperty("filter_rate_limit")
val filterRateLimit: Long?,
@JsonProperty("publishing_rate_limit")
val publishingRateLimit: Long?,
@JsonProperty("required_tags")
val requiredTags: Array<Array<String>>?,
)
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.ammolite.relays.filters
import com.vitorpamplona.quartz.nip01Core.core.Event
class Filter(
val ids: List<String>? = null,
val authors: List<String>? = null,
val kinds: List<Int>? = null,
val tags: Map<String, List<String>>? = null,
val since: Long? = null,
val until: Long? = null,
val limit: Int? = null,
val search: String? = null,
) {
fun toJson() = FilterSerializer.toJson(ids, authors, kinds, tags, since, until, limit, search)
fun match(event: Event) = FilterMatcher.match(event, ids, authors, kinds, tags, since, until)
fun copy(
ids: List<String>? = this.ids,
authors: List<String>? = this.authors,
kinds: List<Int>? = this.kinds,
tags: Map<String, List<String>>? = this.tags,
since: Long? = this.since,
until: Long? = this.until,
limit: Int? = this.limit,
search: String? = this.search,
) = Filter(ids, authors, kinds, tags, since, until, limit, search)
}
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.ammolite.relays.filters
import com.vitorpamplona.quartz.nip01Core.core.Event
object FilterMatcher {
fun match(
event: Event,
ids: List<String>? = null,
authors: List<String>? = null,
kinds: List<Int>? = null,
tags: Map<String, List<String>>? = null,
since: Long? = null,
until: Long? = null,
): Boolean {
if (ids?.contains(event.id) == false) return false
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
}
if (event.createdAt !in (since ?: Long.MIN_VALUE)..(until ?: Long.MAX_VALUE)) {
return false
}
return true
}
}
@@ -0,0 +1,95 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.ammolite.relays.filters
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
object FilterSerializer {
fun toJsonObject(
ids: List<String>? = null,
authors: List<String>? = null,
kinds: List<Int>? = null,
tags: Map<String, List<String>>? = null,
since: Long? = null,
until: Long? = null,
limit: Int? = null,
search: String? = null,
): ObjectNode {
val factory = JsonNodeFactory.instance
return factory.objectNode().apply {
ids?.run {
replace(
"ids",
factory.arrayNode(ids.size).apply { ids.forEach { add(it) } },
)
}
authors?.run {
replace(
"authors",
factory.arrayNode(authors.size).apply { authors.forEach { add(it) } },
)
}
kinds?.run {
replace(
"kinds",
factory.arrayNode(kinds.size).apply { kinds.forEach { add(it) } },
)
}
tags?.run {
entries.forEach { kv ->
replace(
"#${kv.key}",
factory.arrayNode(kv.value.size).apply { kv.value.forEach { add(it) } },
)
}
}
since?.run { put("since", since) }
until?.run { put("until", until) }
limit?.run { put("limit", limit) }
search?.run { put("search", search) }
}
}
fun toJson(
ids: List<String>? = null,
authors: List<String>? = null,
kinds: List<Int>? = null,
tags: Map<String, List<String>>? = null,
since: Long? = null,
until: Long? = null,
limit: Int? = null,
search: String? = null,
): String =
EventMapper.mapper.writeValueAsString(
toJsonObject(
ids,
authors,
kinds,
tags,
since,
until,
limit,
search,
),
)
}