mirror of
https://github.com/aljazceru/khatru.git
synced 2026-02-19 03:34:37 +01:00
prevent storing duplicates.
This commit is contained in:
44
adding.go
44
adding.go
@@ -31,10 +31,27 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
oee(ctx, evt)
|
||||
}
|
||||
} else {
|
||||
// will store
|
||||
|
||||
// but first check if we already have it
|
||||
filter := nostr.Filter{IDs: []string{evt.ID}}
|
||||
for _, query := range rl.QueryEvents {
|
||||
ch, err := query(ctx, filter)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for range ch {
|
||||
// if we run this it means we already have this event, so we just return a success and exit
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// if it's replaceable we first delete old versions
|
||||
if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
|
||||
// replaceable event, delete before storing
|
||||
filter := nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}}
|
||||
for _, query := range rl.QueryEvents {
|
||||
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
|
||||
ch, err := query(ctx, filter)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -49,17 +66,20 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
} else if 30000 <= evt.Kind && evt.Kind < 40000 {
|
||||
// parameterized replaceable event, delete before storing
|
||||
d := evt.Tags.GetFirst([]string{"d", ""})
|
||||
if d != nil {
|
||||
for _, query := range rl.QueryEvents {
|
||||
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for previous := range ch {
|
||||
if isOlder(previous, evt) {
|
||||
for _, del := range rl.DeleteEvent {
|
||||
del(ctx, previous)
|
||||
}
|
||||
if d == nil {
|
||||
return fmt.Errorf("invalid: missing 'd' tag on parameterized replaceable event")
|
||||
}
|
||||
|
||||
filter := nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{(*d)[1]}}}
|
||||
for _, query := range rl.QueryEvents {
|
||||
ch, err := query(ctx, filter)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for previous := range ch {
|
||||
if isOlder(previous, evt) {
|
||||
for _, del := range rl.DeleteEvent {
|
||||
del(ctx, previous)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user