khatru/policies: PreventNormalDuplicates()

This commit is contained in:
fiatjaf
2026-02-14 22:34:16 -03:00
parent 65ef1c50a7
commit d30c1bff46
+53
View File
@@ -3,6 +3,7 @@ package policies
import (
"context"
"fmt"
"iter"
"regexp"
"slices"
"strings"
@@ -144,3 +145,55 @@ func RejectUnprefixedNostrReferences(ctx context.Context, event nostr.Event) (bo
return false, ""
}
// PreventNormalDuplicates prevents normal events that refer to the same thing from being saved.
// For kinds 6, 7, 16, 1018 it checks "e" tags.
// For kind 1163 it checks "p" tags.
// For kinds 1163, 6, 16, 7516, 7517 it checks "a" tags.
func PreventNormalDuplicates(query func(nostr.Filter, int) iter.Seq[nostr.Event]) func(ctx context.Context, event nostr.Event) (bool, string) {
exists := func(event nostr.Event, tagName string) bool {
hasAll := true
for e := range event.Tags.FindAll("e") {
hasThis := false
for range query(nostr.Filter{
Authors: []nostr.PubKey{event.PubKey},
Kinds: []nostr.Kind{event.Kind},
Tags: nostr.TagMap{"e": []string{e[1]}},
}, 1) {
hasThis = true
}
if !hasThis {
hasAll = false
break
}
}
return hasAll
}
return func(ctx context.Context, event nostr.Event) (bool, string) {
reject := false
switch event.Kind {
case 6:
reject = exists(event, "e") && exists(event, "a")
case 7:
reject = exists(event, "e")
case 16:
reject = exists(event, "e") && exists(event, "a")
case 1018:
reject = exists(event, "e")
case 1163:
reject = exists(event, "p")
case 7516:
reject = exists(event, "a")
case 7517:
reject = exists(event, "a")
}
if reject {
return true, "an event similar to this already exists"
}
return false, ""
}
}