Improves vanishing tests

This commit is contained in:
Vitor Pamplona
2026-03-27 13:59:55 -04:00
parent 1ece456f41
commit 153666b9b4
8 changed files with 88 additions and 56 deletions
@@ -23,14 +23,16 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.normalizeRelayUrl
import com.vitorpamplona.quartz.nip01Core.store.IEventStore
class EventStore(
dbName: String? = "events.db",
relayUrl: String? = "wss://quartz.local",
relay: NormalizedRelayUrl? = "wss://quartz.local/".normalizeRelayUrl(),
val indexStrategy: IndexingStrategy = DefaultIndexingStrategy(),
) : IEventStore {
val store = SQLiteEventStore(BundledSQLiteDriver(), dbName, relayUrl, indexStrategy)
val store = SQLiteEventStore(BundledSQLiteDriver(), dbName, relay, indexStrategy)
override fun insert(event: Event) = store.insertEvent(event)
@@ -22,6 +22,7 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
import androidx.sqlite.SQLiteConnection
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip62RequestToVanish.RequestToVanishEvent
class RightToVanishModule(
@@ -100,11 +101,11 @@ class RightToVanishModule(
fun insert(
event: Event,
relayUrl: String?,
relay: NormalizedRelayUrl?,
headerId: Long,
db: SQLiteConnection,
) {
if (event is RequestToVanishEvent && event.shouldVanishFrom(relayUrl)) {
if (event is RequestToVanishEvent && event.shouldVanishFrom(relay)) {
db.prepare(insertRTV).use { stmt ->
stmt.bindLong(1, headerId)
stmt.bindLong(2, hasher(db).hash(event.pubKey))
@@ -30,6 +30,7 @@ import com.vitorpamplona.quartz.nip01Core.core.Kind
import com.vitorpamplona.quartz.nip01Core.core.OptimizedJsonMapper
import com.vitorpamplona.quartz.nip01Core.core.isEphemeral
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.store.IEventStore
import com.vitorpamplona.quartz.nip40Expiration.isExpired
import com.vitorpamplona.quartz.utils.EventFactory
@@ -40,7 +41,7 @@ import kotlinx.coroutines.withContext
class SQLiteEventStore(
val driver: SQLiteDriver = BundledSQLiteDriver(),
val dbName: String? = "events.db",
val relayUrl: String? = null,
val relay: NormalizedRelayUrl? = null,
val indexStrategy: IndexingStrategy = DefaultIndexingStrategy(),
) {
companion object {
@@ -182,7 +183,7 @@ class SQLiteEventStore(
deletionModule.insert(event, db)
expirationModule.insert(event, headerId, db)
fullTextSearchModule.insert(event, headerId, db)
rightToVanishModule.insert(event, relayUrl, headerId, db)
rightToVanishModule.insert(event, relay, headerId, db)
}
fun insertEvent(event: Event) {
@@ -28,6 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip31Alts.alt
import com.vitorpamplona.quartz.nip62RequestToVanish.tags.shouldVanishFrom
import com.vitorpamplona.quartz.nip62RequestToVanish.tags.vanishFrom
import com.vitorpamplona.quartz.nip62RequestToVanish.tags.vanishFromAllRelays
import com.vitorpamplona.quartz.nip62RequestToVanish.tags.vanishFromEverywhere
import com.vitorpamplona.quartz.nip62RequestToVanish.tags.vanishFromRelays
import com.vitorpamplona.quartz.utils.TimeUtils
@@ -42,7 +43,9 @@ class RequestToVanishEvent(
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
fun vanishFromRelays() = tags.vanishFromRelays()
fun shouldVanishFrom(relayUrl: String?) = tags.shouldVanishFrom(relayUrl)
fun vanishFromAllRelays() = tags.vanishFromAllRelays()
fun shouldVanishFrom(relay: NormalizedRelayUrl?) = tags.shouldVanishFrom(relay)
companion object {
const val KIND = 62
@@ -22,6 +22,8 @@ package com.vitorpamplona.quartz.nip62RequestToVanish.tags
import com.vitorpamplona.quartz.nip01Core.core.has
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.isLocalHost
import com.vitorpamplona.quartz.utils.ensure
class RelayTag {
@@ -33,16 +35,23 @@ class RelayTag {
fun notMatch(tag: Array<String>) = !(tag.has(0) && tag[0] == TAG_NAME)
fun shouldVanishFromEverywhere(tag: Array<String>) = tag.has(1) && tag[0] == TAG_NAME && tag[1] == EVERYWHERE
fun shouldVanishFrom(
tag: Array<String>,
relayUrl: String?,
) = tag.has(1) && tag[0] == TAG_NAME && (tag[1] == relayUrl || tag[1] == EVERYWHERE)
relay: NormalizedRelayUrl,
) = tag.has(1) && tag[0] == TAG_NAME && (tag[1] == relay.url || tag[1] == EVERYWHERE)
fun parse(tag: Array<String>): String? {
fun parse(tag: Array<String>): NormalizedRelayUrl? {
ensure(tag.has(1)) { return null }
ensure(tag[0] == TAG_NAME) { return null }
ensure(tag[1].isNotEmpty()) { return null }
return tag[1]
val relay = RelayUrlNormalizer.normalizeOrNull(tag[1])
ensure(relay != null && !relay.isLocalHost()) { return null }
return relay
}
fun assemble(relay: NormalizedRelayUrl) = arrayOf(TAG_NAME, relay.url)
@@ -22,7 +22,15 @@ package com.vitorpamplona.quartz.nip62RequestToVanish.tags
import com.vitorpamplona.quartz.nip01Core.core.TagArray
import com.vitorpamplona.quartz.nip01Core.core.any
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
fun TagArray.vanishFromRelays() = mapNotNull(RelayTag::parse)
fun TagArray.shouldVanishFrom(relayUrl: String?) = any(RelayTag::shouldVanishFrom, relayUrl)
fun TagArray.vanishFromAllRelays() = any(RelayTag::shouldVanishFromEverywhere)
fun TagArray.shouldVanishFrom(relay: NormalizedRelayUrl?): Boolean =
if (relay != null) {
any(RelayTag::shouldVanishFrom, relay)
} else {
any(RelayTag::shouldVanishFromEverywhere)
}