Renames PerRelayFilter to SincePerRelay

Creates PerRelay interface to allow other types of filters
This commit is contained in:
Vitor Pamplona
2024-08-07 15:44:03 -04:00
parent 1ca5713494
commit 263df0c895
22 changed files with 177 additions and 126 deletions

View File

@@ -20,6 +20,7 @@
*/
package com.vitorpamplona.ammolite.relays
import com.vitorpamplona.ammolite.relays.filters.SincePerRelayFilter
import java.util.UUID
data class Subscription(
@@ -42,28 +43,40 @@ data class Subscription(
typedFilters?.forEachIndexed { index, typedFilter ->
val otherFilter = otherFilters?.getOrNull(index) ?: return true
// Does not check SINCE on purpose. Avoids replacing the filter if SINCE was all that changed.
// fast check
if (typedFilter.filter.authors?.size != otherFilter.filter.authors?.size ||
typedFilter.filter.ids?.size != otherFilter.filter.ids?.size ||
typedFilter.filter.tags?.size != otherFilter.filter.tags?.size ||
typedFilter.filter.kinds?.size != otherFilter.filter.kinds?.size ||
typedFilter.filter.limit != otherFilter.filter.limit ||
typedFilter.filter.search?.length != otherFilter.filter.search?.length ||
typedFilter.filter.until != otherFilter.filter.until
) {
return true
if (typedFilter.filter is SincePerRelayFilter && otherFilter.filter is SincePerRelayFilter) {
return isDifferent(typedFilter.filter, otherFilter.filter)
}
// deep check
if (typedFilter.filter.ids != otherFilter.filter.ids ||
typedFilter.filter.authors != otherFilter.filter.authors ||
typedFilter.filter.tags != otherFilter.filter.tags ||
typedFilter.filter.kinds != otherFilter.filter.kinds ||
typedFilter.filter.search != otherFilter.filter.search
) {
return true
}
return true
}
return false
}
fun isDifferent(
filter1: SincePerRelayFilter,
filter2: SincePerRelayFilter,
): Boolean {
// Does not check SINCE on purpose. Avoids replacing the filter if SINCE was all that changed.
// fast check
if (filter1.authors?.size != filter2.authors?.size ||
filter1.ids?.size != filter2.ids?.size ||
filter1.tags?.size != filter2.tags?.size ||
filter1.kinds?.size != filter2.kinds?.size ||
filter1.limit != filter2.limit ||
filter1.search?.length != filter2.search?.length ||
filter1.until != filter2.until
) {
return true
}
// deep check
if (filter1.ids != filter2.ids ||
filter1.authors != filter2.authors ||
filter1.tags != filter2.tags ||
filter1.kinds != filter2.kinds ||
filter1.search != filter2.search
) {
return true
}
return false
}

View File

@@ -20,9 +20,9 @@
*/
package com.vitorpamplona.ammolite.relays
import com.vitorpamplona.ammolite.relays.filters.PerRelayFilter
import com.vitorpamplona.ammolite.relays.filters.IPerRelayFilter
class TypedFilter(
val types: Set<FeedType>,
val filter: PerRelayFilter,
val filter: IPerRelayFilter,
)

View File

@@ -0,0 +1,34 @@
/**
* 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.events.Event
interface IPerRelayFilter {
fun toJson(forRelay: String): String
fun match(
event: Event,
forRelay: String,
): Boolean
fun toDebugJson(): String
}

View File

@@ -22,7 +22,10 @@ package com.vitorpamplona.ammolite.relays.filters
import com.vitorpamplona.quartz.events.Event
class PerRelayFilter(
/**
* This is a nostr filter with per-relay authors list and since parameters
*/
class SincePerRelayFilter(
val ids: List<String>? = null,
val authors: List<String>? = null,
val kinds: List<Int>? = null,
@@ -31,17 +34,18 @@ class PerRelayFilter(
val until: Long? = null,
val limit: Int? = null,
val search: String? = null,
) {
fun toJson(forRelay: String) = FilterSerializer.toJson(ids, authors, kinds, tags, since?.get(forRelay)?.time, until, limit, search)
) : IPerRelayFilter {
override fun toJson(forRelay: String) = FilterSerializer.toJson(ids, authors, kinds, tags, since?.get(forRelay)?.time, until, limit, search)
fun match(
override fun match(
event: Event,
forRelay: String? = null,
forRelay: String,
) = FilterMatcher.match(event, ids, authors, kinds, tags, since?.get(forRelay)?.time, until)
fun toDebugJson(): String {
override fun toDebugJson(): String {
val factory = Event.mapper.nodeFactory
val obj = FilterSerializer.toJsonObject(ids, authors, kinds, tags, null, until, limit, search)
since?.run {
if (isNotEmpty()) {
val jsonObjectSince = factory.objectNode()