Refactors GiftWrap caching to delete encrypted text and reload the wrap if necessary.

- Changes host to a host stub to reduce memory use
- Only download GiftWraps form 2 days past the last EOSE
This commit is contained in:
Vitor Pamplona
2024-05-28 11:48:20 -04:00
parent 0c22e66e8f
commit 6a17a8a871
9 changed files with 108 additions and 7 deletions
@@ -39,7 +39,9 @@ import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.service.relays.Constants
import com.vitorpamplona.amethyst.service.relays.FeedType
import com.vitorpamplona.amethyst.service.relays.JsonFilter
import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.service.relays.TypedFilter
import com.vitorpamplona.amethyst.ui.components.BundledUpdate
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.ATag
@@ -856,7 +858,22 @@ class Account(
fun broadcast(note: Note) {
note.event?.let {
if (it is WrappedEvent && it.host != null) {
it.host?.let { hostEvent -> Client.send(hostEvent) }
it.host?.let {
Client.sendFilterAndStopOnFirstResponse(
filters =
listOf(
TypedFilter(
setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS, FeedType.GLOBAL),
JsonFilter(
ids = listOf(it.id),
),
),
),
onResponse = {
Client.send(it)
},
)
}
} else {
Client.send(it)
}
@@ -1808,7 +1808,7 @@ object LocalCache {
// Already processed this event.
if (note.event != null) return
note.loadEvent(event, author, emptyList())
note.loadEvent(event.copyNoContent(), author, emptyList())
refreshObservers(note)
}
@@ -1828,7 +1828,7 @@ object LocalCache {
// Already processed this event.
if (note.event != null) return
note.loadEvent(event, author, emptyList())
note.loadEvent(event.copyNoContent(), author, emptyList())
refreshObservers(note)
}
@@ -152,7 +152,7 @@ open class Note(val idHex: String) {
Nip19Bech32.createNEvent(
host.id,
host.pubKey,
host.kind(),
host.kind,
relays.firstOrNull()?.url,
)
} else {
@@ -239,6 +239,14 @@ object NostrAccountDataSource : NostrDataSource("AccountData") {
JsonFilter(
kinds = listOf(GiftWrapEvent.KIND),
tags = mapOf("p" to listOf(account.userProfile().pubkeyHex)),
since =
latestEOSEs.users[account.userProfile()]
?.followList
?.get("&&((GIFTWRAPS_EOSE))&&")
?.relayList
?.mapValues {
EOSETime(it.value.time - TimeUtils.twoDays())
},
),
)
@@ -251,6 +259,13 @@ object NostrAccountDataSource : NostrDataSource("AccountData") {
relayUrl,
time,
)
latestEOSEs.addOrUpdate(
account.userProfile(),
"&&((GIFTWRAPS_EOSE))&&",
relayUrl,
time,
)
} else {
hasLoadedTheBasics[account.userProfile()] = true
@@ -100,6 +100,34 @@ object Client : RelayPool.Listener {
RelayPool.sendFilter(subscriptionId, filters)
}
fun sendFilterAndStopOnFirstResponse(
subscriptionId: String = UUID.randomUUID().toString().substring(0..10),
filters: List<TypedFilter> = listOf(),
onResponse: (Event) -> Unit,
) {
checkNotInMainThread()
subscribe(
object : Listener() {
override fun onEvent(
event: Event,
subId: String,
relay: Relay,
afterEOSE: Boolean,
) {
if (subId == subscriptionId) {
onResponse(event)
unsubscribe(this)
close(subscriptionId)
}
}
},
)
subscriptions = subscriptions + Pair(subscriptionId, filters)
RelayPool.sendFilter(subscriptionId, filters)
}
fun sendFilterOnlyIfDisconnected(
subscriptionId: String = UUID.randomUUID().toString().substring(0..10),
filters: List<TypedFilter> = listOf(),