Massive refactoring of quartz to prepare for nip-based packages.
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays
|
||||
|
||||
import com.vitorpamplona.quartz.encoders.RelayUrlFormatter
|
||||
import com.vitorpamplona.quartz.nip65RelayList.RelayUrlFormatter
|
||||
|
||||
object Constants {
|
||||
val activeTypes = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS)
|
||||
|
||||
@@ -22,7 +22,8 @@ package com.vitorpamplona.ammolite.relays
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import com.vitorpamplona.ammolite.relays.filters.Filter
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip13Pow.getPoWRank
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils.now
|
||||
|
||||
class Limits(
|
||||
|
||||
@@ -23,8 +23,7 @@ package com.vitorpamplona.ammolite.relays
|
||||
import android.util.Log
|
||||
import com.vitorpamplona.ammolite.service.checkNotInMainThread
|
||||
import com.vitorpamplona.ammolite.sockets.WebsocketBuilder
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.events.EventInterface
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
@@ -144,7 +143,7 @@ class NostrClient(
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
suspend fun sendAndWaitForResponse(
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
relay: String? = null,
|
||||
forceProxy: Boolean = false,
|
||||
feedTypes: Set<FeedType>? = null,
|
||||
@@ -196,7 +195,7 @@ class NostrClient(
|
||||
message: String,
|
||||
relay: Relay,
|
||||
) {
|
||||
if (eventId == signedEvent.id()) {
|
||||
if (eventId == signedEvent.id) {
|
||||
if (success) {
|
||||
result = true
|
||||
}
|
||||
@@ -243,7 +242,7 @@ class NostrClient(
|
||||
}
|
||||
|
||||
fun sendIfExists(
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
connectedRelay: Relay,
|
||||
) {
|
||||
checkNotInMainThread()
|
||||
@@ -254,7 +253,7 @@ class NostrClient(
|
||||
}
|
||||
|
||||
fun sendSingle(
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
relayTemplate: RelaySetupInfoToConnect,
|
||||
onDone: (() -> Unit),
|
||||
) {
|
||||
@@ -265,13 +264,13 @@ class NostrClient(
|
||||
}
|
||||
}
|
||||
|
||||
fun send(signedEvent: EventInterface) {
|
||||
fun send(signedEvent: Event) {
|
||||
checkNotInMainThread()
|
||||
relayPool.send(signedEvent)
|
||||
}
|
||||
|
||||
fun send(
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
relayList: List<RelaySetupInfo>,
|
||||
) {
|
||||
checkNotInMainThread()
|
||||
@@ -280,7 +279,7 @@ class NostrClient(
|
||||
}
|
||||
|
||||
fun sendPrivately(
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
relayList: List<RelaySetupInfoToConnect>,
|
||||
) {
|
||||
checkNotInMainThread()
|
||||
@@ -375,7 +374,7 @@ class NostrClient(
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
override fun onBeforeSend(
|
||||
relay: Relay,
|
||||
event: EventInterface,
|
||||
event: Event,
|
||||
) {
|
||||
GlobalScope.launch(Dispatchers.Default) {
|
||||
listeners.forEach { it.onBeforeSend(relay, event) }
|
||||
@@ -453,7 +452,7 @@ class NostrClient(
|
||||
|
||||
open fun onBeforeSend(
|
||||
relay: Relay,
|
||||
event: EventInterface,
|
||||
event: Event,
|
||||
) = Unit
|
||||
|
||||
open fun onError(
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.vitorpamplona.ammolite.relays
|
||||
|
||||
import android.util.Log
|
||||
import com.vitorpamplona.ammolite.service.checkNotInMainThread
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
|
||||
@@ -26,10 +26,10 @@ import com.vitorpamplona.ammolite.service.checkNotInMainThread
|
||||
import com.vitorpamplona.ammolite.sockets.WebSocket
|
||||
import com.vitorpamplona.ammolite.sockets.WebSocketListener
|
||||
import com.vitorpamplona.ammolite.sockets.WebsocketBuilder
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.events.EventInterface
|
||||
import com.vitorpamplona.quartz.events.RelayAuthEvent
|
||||
import com.vitorpamplona.quartz.nip01Core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
import com.vitorpamplona.quartz.nip42RelayAuth.RelayAuthEvent
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import com.vitorpamplona.quartz.utils.bytesUsedInMemory
|
||||
import kotlinx.coroutines.CancellationException
|
||||
@@ -80,7 +80,7 @@ class Relay(
|
||||
|
||||
private val authResponse = mutableMapOf<HexKey, Boolean>()
|
||||
private val authChallengesSent = mutableSetOf<String>()
|
||||
private val outboxCache = mutableMapOf<HexKey, EventInterface>()
|
||||
private val outboxCache = mutableMapOf<HexKey, Event>()
|
||||
|
||||
fun register(listener: Listener) {
|
||||
listeners = listeners.plus(listener)
|
||||
@@ -257,12 +257,12 @@ class Relay(
|
||||
}
|
||||
|
||||
fun processNewRelayMessage(newMessage: String) {
|
||||
val msgArray = Event.mapper.readTree(newMessage)
|
||||
val msgArray = EventMapper.mapper.readTree(newMessage)
|
||||
|
||||
when (val type = msgArray.get(0).asText()) {
|
||||
"EVENT" -> {
|
||||
val subscriptionId = msgArray.get(1).asText()
|
||||
val event = Event.fromJson(msgArray.get(2))
|
||||
val event = EventMapper.fromJson(msgArray.get(2))
|
||||
|
||||
// Log.w("Relay", "Relay onEVENT ${event.kind} $url, $subscriptionId ${msgArray.get(2)}")
|
||||
|
||||
@@ -452,7 +452,7 @@ class Relay(
|
||||
}
|
||||
|
||||
// This function sends the event regardless of the relay being write or not.
|
||||
fun sendOverride(signedEvent: EventInterface) {
|
||||
fun sendOverride(signedEvent: Event) {
|
||||
checkNotInMainThread()
|
||||
|
||||
listeners.forEach { listener ->
|
||||
@@ -466,7 +466,7 @@ class Relay(
|
||||
}
|
||||
}
|
||||
|
||||
fun send(signedEvent: EventInterface) {
|
||||
fun send(signedEvent: Event) {
|
||||
checkNotInMainThread()
|
||||
|
||||
listeners.forEach { listener ->
|
||||
@@ -498,9 +498,9 @@ class Relay(
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendEvent(signedEvent: EventInterface) {
|
||||
private fun sendEvent(signedEvent: Event) {
|
||||
synchronized(outboxCache) {
|
||||
outboxCache.put(signedEvent.id(), signedEvent)
|
||||
outboxCache.put(signedEvent.id, signedEvent)
|
||||
}
|
||||
|
||||
if (isConnected()) {
|
||||
@@ -618,7 +618,7 @@ class Relay(
|
||||
|
||||
fun onBeforeSend(
|
||||
relay: Relay,
|
||||
event: EventInterface,
|
||||
event: Event,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ package com.vitorpamplona.ammolite.relays
|
||||
|
||||
import android.util.LruCache
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.encoders.RelayUrlFormatter
|
||||
import com.vitorpamplona.quartz.nip65RelayList.RelayUrlFormatter
|
||||
|
||||
object RelayBriefInfoCache {
|
||||
val cache = LruCache<String, RelayBriefInfo?>(50)
|
||||
|
||||
@@ -22,8 +22,7 @@ package com.vitorpamplona.ammolite.relays
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.ammolite.service.checkNotInMainThread
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.events.EventInterface
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
@@ -121,16 +120,16 @@ class RelayPool : Relay.Listener {
|
||||
|
||||
fun sendToSelectedRelays(
|
||||
list: List<RelaySetupInfo>,
|
||||
signedEvent: EventInterface,
|
||||
signedEvent: Event,
|
||||
) {
|
||||
list.forEach { relay -> relays.filter { it.url == relay.url }.forEach { it.sendOverride(signedEvent) } }
|
||||
}
|
||||
|
||||
fun send(signedEvent: EventInterface) {
|
||||
fun send(signedEvent: Event) {
|
||||
relays.forEach { it.send(signedEvent) }
|
||||
}
|
||||
|
||||
fun sendOverride(signedEvent: EventInterface) {
|
||||
fun sendOverride(signedEvent: Event) {
|
||||
relays.forEach { it.sendOverride(signedEvent) }
|
||||
}
|
||||
|
||||
@@ -205,7 +204,7 @@ class RelayPool : Relay.Listener {
|
||||
|
||||
fun onBeforeSend(
|
||||
relay: Relay,
|
||||
event: EventInterface,
|
||||
event: Event,
|
||||
)
|
||||
|
||||
fun onError(
|
||||
@@ -277,7 +276,7 @@ class RelayPool : Relay.Listener {
|
||||
|
||||
override fun onBeforeSend(
|
||||
relay: Relay,
|
||||
event: EventInterface,
|
||||
event: Event,
|
||||
) {
|
||||
listeners.forEach { it.onBeforeSend(relay, event) }
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
|
||||
class Filter(
|
||||
val ids: List<String>? = null,
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
|
||||
object FilterMatcher {
|
||||
fun match(
|
||||
|
||||
+4
-3
@@ -20,8 +20,9 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
|
||||
object FilterSerializer {
|
||||
fun toJsonObject(
|
||||
@@ -34,7 +35,7 @@ object FilterSerializer {
|
||||
limit: Int? = null,
|
||||
search: String? = null,
|
||||
): ObjectNode {
|
||||
val factory = Event.mapper.nodeFactory
|
||||
val factory = JsonNodeFactory.instance
|
||||
return factory.objectNode().apply {
|
||||
ids?.run {
|
||||
replace(
|
||||
@@ -79,7 +80,7 @@ object FilterSerializer {
|
||||
limit: Int? = null,
|
||||
search: String? = null,
|
||||
): String =
|
||||
Event.mapper.writeValueAsString(
|
||||
EventMapper.mapper.writeValueAsString(
|
||||
toJsonObject(
|
||||
ids,
|
||||
authors,
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
|
||||
interface IPerRelayFilter {
|
||||
fun toJson(forRelay: String): String
|
||||
|
||||
+6
-4
@@ -20,8 +20,10 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
|
||||
/**
|
||||
* This is a nostr filter with per-relay authors list and since parameters
|
||||
@@ -49,7 +51,7 @@ class SinceAuthorPerRelayFilter(
|
||||
) = FilterMatcher.match(event, ids, authors?.get(forRelay), kinds, tags, since?.get(forRelay)?.time, until)
|
||||
|
||||
override fun toDebugJson(): String {
|
||||
val factory = Event.mapper.nodeFactory
|
||||
val factory = JsonNodeFactory.instance
|
||||
val obj = FilterSerializer.toJsonObject(ids, null, kinds, tags, null, until, limit, search)
|
||||
authors?.run {
|
||||
if (isNotEmpty()) {
|
||||
@@ -70,6 +72,6 @@ class SinceAuthorPerRelayFilter(
|
||||
obj.put("since", jsonObjectSince)
|
||||
}
|
||||
}
|
||||
return Event.mapper.writeValueAsString(obj)
|
||||
return EventMapper.mapper.writeValueAsString(obj)
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -20,7 +20,9 @@
|
||||
*/
|
||||
package com.vitorpamplona.ammolite.relays.filters
|
||||
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
|
||||
/**
|
||||
* This is a nostr filter with per-relay authors list and since parameters
|
||||
@@ -45,7 +47,7 @@ class SincePerRelayFilter(
|
||||
) = FilterMatcher.match(event, ids, authors, kinds, tags, since?.get(forRelay)?.time, until)
|
||||
|
||||
override fun toDebugJson(): String {
|
||||
val factory = Event.mapper.nodeFactory
|
||||
val factory = JsonNodeFactory.instance
|
||||
val obj = FilterSerializer.toJsonObject(ids, authors, kinds, tags, null, until, limit, search)
|
||||
|
||||
since?.run {
|
||||
@@ -57,6 +59,6 @@ class SincePerRelayFilter(
|
||||
obj.put("since", jsonObjectSince)
|
||||
}
|
||||
}
|
||||
return Event.mapper.writeValueAsString(obj)
|
||||
return EventMapper.toJson(obj)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user