Assigns the first relay when a gift-wrapped message is received

This commit is contained in:
Vitor Pamplona
2025-05-05 18:51:45 -04:00
parent 7a8766551f
commit 1c23600c57
3 changed files with 36 additions and 25 deletions
@@ -1850,7 +1850,6 @@ object LocalCache : ILocalCache {
val author = getOrCreateUser(event.pubKey)
if (relay != null) {
author.addRelayBeingUsed(relay, event.createdAt)
note.addRelay(relay)
}
@@ -2803,6 +2802,16 @@ object LocalCache : ILocalCache {
toNote.addRelay(it)
}
}
fun copyRelaysFromTo(
from: Note,
to: HexKey,
) {
val toNote = getOrCreateNote(to)
from.relays.forEach {
toNote.addRelay(it)
}
}
}
@Stable
@@ -26,8 +26,6 @@ import com.vitorpamplona.ammolite.relays.RelayBriefInfoCache
import com.vitorpamplona.ammolite.relays.datasources.EventCollector
import com.vitorpamplona.ammolite.relays.datasources.RelayInsertConfirmationCollector
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
class CacheClientConnector(
val client: NostrClient,
@@ -41,7 +39,7 @@ class CacheClientConnector(
val confirmationWatcher =
RelayInsertConfirmationCollector(client) { eventId, relay ->
cache.markAsSeen(eventId, relay.brief)
unwrapAndMarkAsSeen(eventId, relay.brief)
markAsSeen(eventId, relay.brief)
}
fun destroy() {
@@ -49,25 +47,12 @@ class CacheClientConnector(
confirmationWatcher.destroy()
}
private fun unwrapAndMarkAsSeen(
private fun markAsSeen(
eventId: HexKey,
relay: RelayBriefInfoCache.RelayBriefInfo,
) {
val note = LocalCache.getNoteIfExists(eventId)
if (note != null) {
note.addRelay(relay)
val noteEvent = note.event
if (noteEvent is GiftWrapEvent) {
noteEvent.innerEventId?.let {
unwrapAndMarkAsSeen(it, relay)
}
} else if (noteEvent is SealedRumorEvent) {
noteEvent.innerEventId?.let {
unwrapAndMarkAsSeen(it, relay)
}
}
}
note?.addRelay(relay)
}
}
@@ -42,7 +42,11 @@ class PrecacheNewNotesProcessor(
fun consume(note: Note) {
val noteEvent = note.event
if (noteEvent != null) {
consumeAlreadyVerified(noteEvent, note)
try {
consumeAlreadyVerified(noteEvent, note)
} catch (e: Exception) {
Log.e("PrecacheNewNotesProcessor", "Error processing note", e)
}
}
}
@@ -70,22 +74,35 @@ class PrecacheNewNotesProcessor(
}
is GiftWrapEvent -> {
if (event.innerEventId == null) {
if (event.recipientPubKey() == account.signer.pubKey) {
if (event.recipientPubKey() == account.signer.pubKey) {
val inner = event.innerEventId
if (inner == null) {
event.unwrap(account.signer) {
cache.verifyAndConsume(it, null)
cache.copyRelaysFromTo(note, it)
if (cache.justVerify(it)) {
cache.justConsume(it, null)
cache.copyRelaysFromTo(note, it)
consumeAlreadyVerified(it, note)
}
}
} else {
cache.copyRelaysFromTo(note, inner)
val event = cache.getOrCreateNote(inner).event
if (event != null) {
consumeAlreadyVerified(event, note)
}
}
}
}
is SealedRumorEvent -> {
if (event.innerEventId == null) {
val inner = event.innerEventId
if (inner == null) {
event.unseal(account.signer) {
cache.justConsume(it, null)
cache.copyRelaysFromTo(note, it)
}
} else {
cache.copyRelaysFromTo(note, inner)
}
}