mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-18 11:32:25 +02:00
nip27 parsing improved (and with nip08 support removed) in its own package.
This commit is contained in:
78
nip27/references.go
Normal file
78
nip27/references.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package nip27
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"regexp"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip19"
|
||||
)
|
||||
|
||||
type Reference struct {
|
||||
Text string
|
||||
Start int
|
||||
End int
|
||||
Profile *nostr.ProfilePointer
|
||||
Event *nostr.EventPointer
|
||||
Entity *nostr.EntityPointer
|
||||
}
|
||||
|
||||
var mentionRegex = regexp.MustCompile(`\bnostr:((note|npub|naddr|nevent|nprofile)1\w+)\b`)
|
||||
|
||||
func ParseReferences(evt nostr.Event) iter.Seq[Reference] {
|
||||
return func(yield func(Reference) bool) {
|
||||
for _, ref := range mentionRegex.FindAllStringSubmatchIndex(evt.Content, -1) {
|
||||
reference := Reference{
|
||||
Text: evt.Content[ref[0]:ref[1]],
|
||||
Start: ref[0],
|
||||
End: ref[1],
|
||||
}
|
||||
|
||||
nip19code := evt.Content[ref[2]:ref[3]]
|
||||
|
||||
if prefix, data, err := nip19.Decode(nip19code); err == nil {
|
||||
switch prefix {
|
||||
case "npub":
|
||||
reference.Profile = &nostr.ProfilePointer{
|
||||
PublicKey: data.(string), Relays: []string{},
|
||||
}
|
||||
tag := evt.Tags.GetFirst([]string{"p", reference.Profile.PublicKey})
|
||||
if tag != nil && len(*tag) >= 3 {
|
||||
reference.Profile.Relays = []string{(*tag)[2]}
|
||||
}
|
||||
case "nprofile":
|
||||
pp := data.(nostr.ProfilePointer)
|
||||
reference.Profile = &pp
|
||||
tag := evt.Tags.GetFirst([]string{"p", reference.Profile.PublicKey})
|
||||
if tag != nil && len(*tag) >= 3 {
|
||||
reference.Profile.Relays = append(reference.Profile.Relays, (*tag)[2])
|
||||
}
|
||||
case "note":
|
||||
// we don't even bother here because people using note1 codes aren't including relay hints anyway
|
||||
reference.Event = &nostr.EventPointer{ID: data.(string), Relays: []string{}}
|
||||
case "nevent":
|
||||
evp := data.(nostr.EventPointer)
|
||||
reference.Event = &evp
|
||||
tag := evt.Tags.GetFirst([]string{"e", reference.Event.ID})
|
||||
if tag != nil && len(*tag) >= 3 {
|
||||
reference.Event.Relays = append(reference.Event.Relays, (*tag)[2])
|
||||
if reference.Event.Author == "" && len(*tag) >= 5 {
|
||||
reference.Event.Author = (*tag)[4]
|
||||
}
|
||||
}
|
||||
case "naddr":
|
||||
addr := data.(nostr.EntityPointer)
|
||||
reference.Entity = &addr
|
||||
tag := evt.Tags.GetFirst([]string{"a", reference.Entity.AsTagReference()})
|
||||
if tag != nil && len(*tag) >= 3 {
|
||||
reference.Entity.Relays = append(reference.Entity.Relays, (*tag)[2])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !yield(reference) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
nip27/references_test.go
Normal file
46
nip27/references_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package nip27
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseReferences(t *testing.T) {
|
||||
evt := nostr.Event{
|
||||
Tags: nostr.Tags{
|
||||
{"p", "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393", "wss://xawr.com"},
|
||||
{"e", "a84c5de86efc2ec2cff7bad077c4171e09146b633b7ad117fffe088d9579ac33", "wss://other.com", "reply"},
|
||||
{"e", "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393", "wss://nasdj.com"},
|
||||
},
|
||||
Content: "hello, nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg wrote nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4!",
|
||||
}
|
||||
|
||||
expected := []Reference{
|
||||
{
|
||||
Text: "nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg",
|
||||
Start: 7,
|
||||
End: 83,
|
||||
Profile: &nostr.ProfilePointer{
|
||||
PublicKey: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
||||
Relays: []string{"wss://xawr.com"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4",
|
||||
Start: 90,
|
||||
End: 164,
|
||||
Event: &nostr.EventPointer{
|
||||
ID: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
||||
Relays: []string{"wss://nasdj.com"},
|
||||
Author: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
got := slices.Collect(ParseReferences(evt))
|
||||
|
||||
require.EqualValues(t, expected, got)
|
||||
}
|
Reference in New Issue
Block a user