Fixes the lack of the Comparator interface for the Deletion Index.
This commit is contained in:
@@ -959,53 +959,53 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun consume(event: DeletionEvent) {
|
fun consume(event: DeletionEvent) {
|
||||||
deletionIndex.add(event)
|
if (deletionIndex.add(event)) {
|
||||||
|
var deletedAtLeastOne = false
|
||||||
|
|
||||||
var deletedAtLeastOne = false
|
event.deleteEvents()
|
||||||
|
.mapNotNull { getNoteIfExists(it) }
|
||||||
|
.forEach { deleteNote ->
|
||||||
|
// must be the same author
|
||||||
|
if (deleteNote.author?.pubkeyHex == event.pubKey) {
|
||||||
|
// reverts the add
|
||||||
|
deleteNote(deleteNote)
|
||||||
|
|
||||||
event.deleteEvents()
|
deletedAtLeastOne = true
|
||||||
.mapNotNull { getNoteIfExists(it) }
|
}
|
||||||
.forEach { deleteNote ->
|
}
|
||||||
// must be the same author
|
|
||||||
if (deleteNote.author?.pubkeyHex == event.pubKey) {
|
|
||||||
// reverts the add
|
|
||||||
deleteNote(deleteNote)
|
|
||||||
|
|
||||||
deletedAtLeastOne = true
|
val addressList = event.deleteAddressTags()
|
||||||
|
val addressSet = addressList.toSet()
|
||||||
|
|
||||||
|
addressList
|
||||||
|
.mapNotNull { getAddressableNoteIfExists(it) }
|
||||||
|
.forEach { deleteNote ->
|
||||||
|
// must be the same author
|
||||||
|
if (deleteNote.author?.pubkeyHex == event.pubKey && (deleteNote.createdAt() ?: 0) <= event.createdAt) {
|
||||||
|
// Counts the replies
|
||||||
|
deleteNote(deleteNote)
|
||||||
|
|
||||||
|
addressables.remove(deleteNote.idHex)
|
||||||
|
|
||||||
|
deletedAtLeastOne = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notes.forEach { key, note ->
|
||||||
|
val noteEvent = note.event
|
||||||
|
if (noteEvent is AddressableEvent && noteEvent.addressTag() in addressSet) {
|
||||||
|
if (noteEvent.pubKey() == event.pubKey && noteEvent.createdAt() <= event.createdAt) {
|
||||||
|
deleteNote(note)
|
||||||
|
deletedAtLeastOne = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val addressList = event.deleteAddresses()
|
if (deletedAtLeastOne) {
|
||||||
val addressSet = addressList.toSet()
|
val note = Note(event.id)
|
||||||
|
note.loadEvent(event, getOrCreateUser(event.pubKey), emptyList())
|
||||||
addressList
|
refreshObservers(note)
|
||||||
.mapNotNull { getAddressableNoteIfExists(it.toTag()) }
|
|
||||||
.forEach { deleteNote ->
|
|
||||||
// must be the same author
|
|
||||||
if (deleteNote.author?.pubkeyHex == event.pubKey && (deleteNote.createdAt() ?: 0) < event.createdAt) {
|
|
||||||
// Counts the replies
|
|
||||||
deleteNote(deleteNote)
|
|
||||||
|
|
||||||
addressables.remove(deleteNote.idHex)
|
|
||||||
|
|
||||||
deletedAtLeastOne = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
notes.forEach { key, note ->
|
|
||||||
val noteEvent = note.event
|
|
||||||
if (noteEvent is AddressableEvent && noteEvent.address() in addressSet) {
|
|
||||||
if (noteEvent.pubKey() == event.pubKey && noteEvent.createdAt() <= event.createdAt) {
|
|
||||||
deleteNote(note)
|
|
||||||
deletedAtLeastOne = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deletedAtLeastOne) {
|
|
||||||
val note = Note(event.id)
|
|
||||||
note.loadEvent(event, getOrCreateUser(event.pubKey), emptyList())
|
|
||||||
refreshObservers(note)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,30 +26,49 @@ import com.vitorpamplona.quartz.events.DeletionEvent
|
|||||||
import com.vitorpamplona.quartz.events.Event
|
import com.vitorpamplona.quartz.events.Event
|
||||||
|
|
||||||
class DeletionIndex {
|
class DeletionIndex {
|
||||||
data class DeletionRequest(val reference: String, val publicKey: HexKey)
|
data class DeletionRequest(val reference: String, val publicKey: HexKey) : Comparable<DeletionRequest> {
|
||||||
|
override fun compareTo(other: DeletionRequest): Int {
|
||||||
|
val compared = reference.compareTo(other.reference)
|
||||||
|
|
||||||
|
return if (compared == 0) {
|
||||||
|
publicKey.compareTo(publicKey)
|
||||||
|
} else {
|
||||||
|
compared
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// stores a set of id OR atags (kind:pubkey:dtag) by pubkey with the created at of the deletion event.
|
// stores a set of id OR atags (kind:pubkey:dtag) by pubkey with the created at of the deletion event.
|
||||||
// Anything newer than the date should not be deleted.
|
// Anything newer than the date should not be deleted.
|
||||||
private val deletedReferencesBefore = LargeCache<DeletionRequest, Long>()
|
private val deletedReferencesBefore = LargeCache<DeletionRequest, Long>()
|
||||||
|
|
||||||
fun add(event: DeletionEvent) {
|
fun add(event: DeletionEvent): Boolean {
|
||||||
|
var atLeastOne = false
|
||||||
|
|
||||||
event.tags.forEach {
|
event.tags.forEach {
|
||||||
if (it.size > 1 && (it[0] == "a" || it[0] == "e")) {
|
if (it.size > 1 && (it[0] == "a" || it[0] == "e")) {
|
||||||
add(it[1], event.pubKey, event.createdAt)
|
if (add(it[1], event.pubKey, event.createdAt)) {
|
||||||
|
atLeastOne = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return atLeastOne
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun add(
|
private fun add(
|
||||||
ref: String,
|
ref: String,
|
||||||
byPubKey: HexKey,
|
byPubKey: HexKey,
|
||||||
createdAt: Long,
|
createdAt: Long,
|
||||||
) {
|
): Boolean {
|
||||||
val key = DeletionRequest(ref, byPubKey)
|
val key = DeletionRequest(ref, byPubKey)
|
||||||
val previousDeletionTime = deletedReferencesBefore.get(key)
|
val previousDeletionTime = deletedReferencesBefore.get(key)
|
||||||
|
|
||||||
if (previousDeletionTime == null || createdAt > previousDeletionTime) {
|
if (previousDeletionTime == null || createdAt > previousDeletionTime) {
|
||||||
deletedReferencesBefore.put(key, createdAt)
|
deletedReferencesBefore.put(key, createdAt)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun hasBeenDeleted(event: Event): Boolean {
|
fun hasBeenDeleted(event: Event): Boolean {
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ class DeletionEvent(
|
|||||||
|
|
||||||
fun deleteAddresses() = taggedAddresses()
|
fun deleteAddresses() = taggedAddresses()
|
||||||
|
|
||||||
|
fun deleteAddressTags() = tags.mapNotNull { if (it.size > 1 && it[0] == "a") it[1] else null }
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KIND = 5
|
const val KIND = 5
|
||||||
const val ALT = "Deletion event"
|
const val ALT = "Deletion event"
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class DraftEvent(
|
|||||||
pubKey: HexKey,
|
pubKey: HexKey,
|
||||||
dTag: String,
|
dTag: String,
|
||||||
): String {
|
): String {
|
||||||
return ATag(KIND, pubKey, dTag, null).toTag()
|
return ATag.assembleATag(KIND, pubKey, dTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun create(
|
fun create(
|
||||||
|
|||||||
Reference in New Issue
Block a user