Support for pruning expired events.

This commit is contained in:
Vitor Pamplona
2023-08-24 13:13:30 -04:00
parent c052399a16
commit 52c0503b6d
3 changed files with 37 additions and 1 deletions

View File

@ -82,13 +82,14 @@ height="80">](https://github.com/vitorpamplona/amethyst/releases)
- [x] Private Messages and Small Groups (NIP-24) - [x] Private Messages and Small Groups (NIP-24)
- [x] Gift Wraps & Seals (NIP-59) - [x] Gift Wraps & Seals (NIP-59)
- [x] Versioned Encrypted Payloads (NIP-44) - [x] Versioned Encrypted Payloads (NIP-44)
- [x] Expiration Support (NIP-40)
- [x] Status Event (NIP-315)
- [ ] Marketplace (NIP-15) - [ ] Marketplace (NIP-15)
- [ ] Image/Video Capture in the app - [ ] Image/Video Capture in the app
- [ ] Local Database - [ ] Local Database
- [ ] Bookmarks, Pinned Posts, Muted Events (NIP-51) - [ ] Bookmarks, Pinned Posts, Muted Events (NIP-51)
- [ ] Proof of Work in the Phone (NIP-13, NIP-20) - [ ] Proof of Work in the Phone (NIP-13, NIP-20)
- [ ] Workspaces - [ ] Workspaces
- [ ] Expiration Support (NIP-40)
- [ ] Infinity Scroll - [ ] Infinity Scroll
- [ ] Relay List Metadata (NIP-65) - [ ] Relay List Metadata (NIP-65)
- [ ] Signing Requests (NIP-46) - [ ] Signing Requests (NIP-46)

View File

@ -126,6 +126,7 @@ object ServiceManager {
LocalCache.pruneContactLists(accounts) LocalCache.pruneContactLists(accounts)
LocalCache.pruneRepliesAndReactions(accounts) LocalCache.pruneRepliesAndReactions(accounts)
LocalCache.prunePastVersionsOfReplaceables() LocalCache.prunePastVersionsOfReplaceables()
LocalCache.pruneExpiredEvents()
} }
} }
} }

View File

@ -14,6 +14,7 @@ import com.vitorpamplona.quartz.encoders.Nip19
import com.vitorpamplona.quartz.encoders.decodePublicKeyAsHexOrNull import com.vitorpamplona.quartz.encoders.decodePublicKeyAsHexOrNull
import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.* import com.vitorpamplona.quartz.events.*
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toImmutableSet import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -1269,6 +1270,39 @@ object LocalCache {
} }
} }
fun pruneExpiredEvents() {
checkNotInMainThread()
val now = TimeUtils.now()
val toBeRemoved = notes.filter {
(it.value.event?.expiration() ?: Long.MAX_VALUE) < now
}.values
val childrenToBeRemoved = mutableListOf<Note>()
toBeRemoved.forEach {
notes.remove(it.idHex)
it.replyTo?.forEach { masterNote ->
masterNote.removeReply(it)
masterNote.removeBoost(it)
masterNote.removeReaction(it)
masterNote.removeZap(it)
masterNote.removeReport(it)
masterNote.clearEOSE() // allows reloading of these events
}
childrenToBeRemoved.addAll(it.removeAllChildNotes())
}
removeChildrenOf(childrenToBeRemoved)
if (toBeRemoved.size > 1) {
println("PRUNE: ${toBeRemoved.size} thread replies removed.")
}
}
fun pruneHiddenMessages(account: Account) { fun pruneHiddenMessages(account: Account) {
checkNotInMainThread() checkNotInMainThread()