diff --git a/eventstore/boltdb/replace.go b/eventstore/boltdb/replace.go index f488ba3..2f3588b 100644 --- a/eventstore/boltdb/replace.go +++ b/eventstore/boltdb/replace.go @@ -11,6 +11,14 @@ import ( func (b *BoltBackend) ReplaceEvent(evt nostr.Event) error { return b.DB.Update(func(txn *bbolt.Tx) error { + rawBucket := txn.Bucket(rawEventStore) + + // check if we already have this id + bin := rawBucket.Get(evt.ID[16:24]) + if bin != nil { + return nil + } + filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}} if evt.Kind.IsAddressable() { // when addressable, add the "d" tag to the filter diff --git a/eventstore/lmdb/replace.go b/eventstore/lmdb/replace.go index abb722f..4931540 100644 --- a/eventstore/lmdb/replace.go +++ b/eventstore/lmdb/replace.go @@ -11,6 +11,15 @@ import ( func (b *LMDBBackend) ReplaceEvent(evt nostr.Event) error { return b.lmdbEnv.Update(func(txn *lmdb.Txn) error { + // check if we already have this id + _, existsErr := txn.Get(b.indexId, evt.ID[0:8]) + if existsErr == nil { + return nil + } + if operr, ok := existsErr.(*lmdb.OpError); !ok || operr.Errno != lmdb.NotFound { + return existsErr + } + filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}} if evt.Kind.IsAddressable() { // when addressable, add the "d" tag to the filter diff --git a/eventstore/mmm/replace.go b/eventstore/mmm/replace.go index 079b84e..46cc8a9 100644 --- a/eventstore/mmm/replace.go +++ b/eventstore/mmm/replace.go @@ -7,6 +7,7 @@ import ( "fiatjaf.com/nostr" "fiatjaf.com/nostr/eventstore/internal" + "github.com/PowerDNS/lmdb-go/lmdb" ) func (il *IndexingLayer) ReplaceEvent(evt nostr.Event) error { @@ -47,6 +48,15 @@ func (il *IndexingLayer) ReplaceEvent(evt nostr.Event) error { }() iltxn.RawRead = true + // check if we already have this id + _, existsErr := mmmtxn.Get(il.mmmm.indexId, evt.ID[0:8]) + if existsErr == nil { + return nil + } + if !lmdb.IsNotFound(existsErr) { + return fmt.Errorf("error checking existence: %w", existsErr) + } + // now we fetch the past events, whatever they are, delete them and then save the new var results iter.Seq[nostr.Event] = func(yield func(nostr.Event) bool) { err = il.query(iltxn, filter, 10 /* in theory limit could be just 1 and this should work */, yield)