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
@@ -526,9 +526,15 @@ open class WrappedEvent(
content: String,
sig: HexKey,
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
@Transient var host: Event? = null // host event to broadcast when needed
@Transient var host: HostStub? = null // host event to broadcast when needed
}
class HostStub(
val id: HexKey,
val pubKey: HexKey,
val kind: Int,
)
@Immutable
interface AddressableEvent {
fun dTag(): String
@@ -38,6 +38,22 @@ class GiftWrapEvent(
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
@Transient private var cachedInnerEvent: Map<HexKey, Event?> = mapOf()
fun copyNoContent(): GiftWrapEvent {
val copy =
GiftWrapEvent(
id,
pubKey,
createdAt,
tags,
"",
sig,
)
copy.cachedInnerEvent = cachedInnerEvent
return copy
}
override fun isContentEncoded() = true
fun preCachedGift(signer: NostrSigner): Event? {
@@ -61,7 +77,7 @@ class GiftWrapEvent(
}
unwrap(signer) { gift ->
if (gift is WrappedEvent) {
gift.host = this
gift.host = HostStub(this.id, this.pubKey, this.kind)
}
addToCache(signer.pubKey, gift)
@@ -39,6 +39,23 @@ class SealedGossipEvent(
) : WrappedEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
@Transient private var cachedInnerEvent: Map<HexKey, Event?> = mapOf()
fun copyNoContent(): SealedGossipEvent {
val copy =
SealedGossipEvent(
id,
pubKey,
createdAt,
tags,
"",
sig,
)
copy.cachedInnerEvent = cachedInnerEvent
copy.host = host
return copy
}
override fun isContentEncoded() = true
fun preCachedGossip(signer: NostrSigner): Event? {
@@ -64,7 +81,7 @@ class SealedGossipEvent(
unseal(signer) { gossip ->
val event = gossip.mergeWith(this)
if (event is WrappedEvent) {
event.host = host ?: this
event.host = host ?: HostStub(this.id, this.pubKey, this.kind)
}
addToCache(signer.pubKey, event)
@@ -51,6 +51,8 @@ object TimeUtils {
fun eightHoursAgo() = now() - EIGHT_HOURS
fun twoDays() = ONE_DAY * 2
fun oneWeekAgo() = now() - ONE_WEEK
fun randomWithinAWeek() = System.currentTimeMillis() / 1000 - CryptoUtils.randomInt(ONE_WEEK)