mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-13 11:52:34 +02:00
sdk: return start and end on references for replacement.
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
type Reference struct {
|
type Reference struct {
|
||||||
Text string
|
Text string
|
||||||
|
Start int
|
||||||
|
End int
|
||||||
Profile *nostr.ProfilePointer
|
Profile *nostr.ProfilePointer
|
||||||
Event *nostr.EventPointer
|
Event *nostr.EventPointer
|
||||||
Entity *nostr.EntityPointer
|
Entity *nostr.EntityPointer
|
||||||
@@ -21,95 +23,85 @@ var mentionRegex = regexp.MustCompile(`\bnostr:((note|npub|naddr|nevent|nprofile
|
|||||||
// ParseReferences parses both NIP-08 and NIP-27 references in a single unifying interface.
|
// ParseReferences parses both NIP-08 and NIP-27 references in a single unifying interface.
|
||||||
func ParseReferences(evt *nostr.Event) []*Reference {
|
func ParseReferences(evt *nostr.Event) []*Reference {
|
||||||
var references []*Reference
|
var references []*Reference
|
||||||
for _, ref := range mentionRegex.FindAllStringSubmatch(evt.Content, -1) {
|
content := evt.Content
|
||||||
if ref[2] != "" {
|
|
||||||
// it's a NIP-27 mention
|
for _, ref := range mentionRegex.FindAllStringSubmatchIndex(evt.Content, -1) {
|
||||||
if prefix, data, err := nip19.Decode(ref[1]); err == nil {
|
reference := &Reference{
|
||||||
|
Text: content[ref[0]:ref[1]],
|
||||||
|
Start: ref[0],
|
||||||
|
End: ref[1],
|
||||||
|
}
|
||||||
|
|
||||||
|
if ref[6] == -1 {
|
||||||
|
// didn't find a NIP-10 #[0] reference, so it's it's a NIP-27 mention
|
||||||
|
nip19code := content[ref[2]:ref[3]]
|
||||||
|
|
||||||
|
if prefix, data, err := nip19.Decode(nip19code); err == nil {
|
||||||
switch prefix {
|
switch prefix {
|
||||||
case "npub":
|
case "npub":
|
||||||
references = append(references, &Reference{
|
reference.Profile = &nostr.ProfilePointer{
|
||||||
Text: ref[0],
|
PublicKey: data.(string), Relays: []string{},
|
||||||
Profile: &nostr.ProfilePointer{
|
}
|
||||||
PublicKey: data.(string), Relays: []string{},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
case "nprofile":
|
case "nprofile":
|
||||||
pp := data.(nostr.ProfilePointer)
|
pp := data.(nostr.ProfilePointer)
|
||||||
references = append(references, &Reference{
|
reference.Profile = &pp
|
||||||
Text: ref[0],
|
|
||||||
Profile: &pp,
|
|
||||||
})
|
|
||||||
case "note":
|
case "note":
|
||||||
references = append(references, &Reference{
|
reference.Event = &nostr.EventPointer{ID: data.(string), Relays: []string{}}
|
||||||
Text: ref[0],
|
|
||||||
Event: &nostr.EventPointer{ID: data.(string), Relays: []string{}},
|
|
||||||
})
|
|
||||||
case "nevent":
|
case "nevent":
|
||||||
evp := data.(nostr.EventPointer)
|
evp := data.(nostr.EventPointer)
|
||||||
references = append(references, &Reference{
|
reference.Event = &evp
|
||||||
Text: ref[0],
|
|
||||||
Event: &evp,
|
|
||||||
})
|
|
||||||
case "naddr":
|
case "naddr":
|
||||||
addr := data.(nostr.EntityPointer)
|
addr := data.(nostr.EntityPointer)
|
||||||
references = append(references, &Reference{
|
reference.Entity = &addr
|
||||||
Text: ref[0],
|
|
||||||
Entity: &addr,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ref[3] != "" {
|
} else {
|
||||||
// it's a NIP-10 mention
|
// it's a NIP-10 mention.
|
||||||
idx, err := strconv.Atoi(ref[3])
|
// parse the number, get data from event tags.
|
||||||
|
n := content[ref[6]:ref[7]]
|
||||||
|
idx, err := strconv.Atoi(n)
|
||||||
if err != nil || len(evt.Tags) <= idx {
|
if err != nil || len(evt.Tags) <= idx {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if tag := evt.Tags[idx]; tag != nil {
|
if tag := evt.Tags[idx]; tag != nil && len(tag) >= 2 {
|
||||||
switch tag[0] {
|
switch tag[0] {
|
||||||
case "p":
|
case "p":
|
||||||
relays := make([]string, 0, 1)
|
relays := make([]string, 0, 1)
|
||||||
if len(tag) > 2 && tag[2] != "" {
|
if len(tag) > 2 && tag[2] != "" {
|
||||||
relays = append(relays, tag[2])
|
relays = append(relays, tag[2])
|
||||||
}
|
}
|
||||||
references = append(references, &Reference{
|
reference.Profile = &nostr.ProfilePointer{
|
||||||
Text: ref[0],
|
PublicKey: tag[1],
|
||||||
Profile: &nostr.ProfilePointer{
|
Relays: relays,
|
||||||
PublicKey: tag[1],
|
}
|
||||||
Relays: relays,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
case "e":
|
case "e":
|
||||||
relays := make([]string, 0, 1)
|
relays := make([]string, 0, 1)
|
||||||
if len(tag) > 2 && tag[2] != "" {
|
if len(tag) > 2 && tag[2] != "" {
|
||||||
relays = append(relays, tag[2])
|
relays = append(relays, tag[2])
|
||||||
}
|
}
|
||||||
references = append(references, &Reference{
|
reference.Event = &nostr.EventPointer{
|
||||||
Text: ref[0],
|
ID: tag[1],
|
||||||
Event: &nostr.EventPointer{
|
Relays: relays,
|
||||||
ID: tag[1],
|
}
|
||||||
Relays: relays,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
case "a":
|
case "a":
|
||||||
if parts := strings.Split(ref[1], ":"); len(parts) == 3 {
|
if parts := strings.Split(tag[1], ":"); len(parts) == 3 {
|
||||||
kind, _ := strconv.Atoi(parts[0])
|
kind, _ := strconv.Atoi(parts[0])
|
||||||
relays := make([]string, 0, 1)
|
relays := make([]string, 0, 1)
|
||||||
if len(tag) > 2 && tag[2] != "" {
|
if len(tag) > 2 && tag[2] != "" {
|
||||||
relays = append(relays, tag[2])
|
relays = append(relays, tag[2])
|
||||||
}
|
}
|
||||||
references = append(references, &Reference{
|
reference.Entity = &nostr.EntityPointer{
|
||||||
Text: ref[0],
|
Identifier: parts[2],
|
||||||
Entity: &nostr.EntityPointer{
|
PublicKey: parts[1],
|
||||||
Identifier: parts[2],
|
Kind: kind,
|
||||||
PublicKey: parts[1],
|
Relays: relays,
|
||||||
Kind: kind,
|
}
|
||||||
Relays: relays,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
references = append(references, reference)
|
||||||
}
|
}
|
||||||
|
|
||||||
return references
|
return references
|
||||||
|
@@ -19,28 +19,36 @@ func TestParseReferences(t *testing.T) {
|
|||||||
|
|
||||||
expected := []Reference{
|
expected := []Reference{
|
||||||
{
|
{
|
||||||
Text: "#[0]",
|
Text: "#[0]",
|
||||||
|
Start: 6,
|
||||||
|
End: 10,
|
||||||
Profile: &nostr.ProfilePointer{
|
Profile: &nostr.ProfilePointer{
|
||||||
PublicKey: "c9d556c6d3978d112d30616d0d20aaa81410e3653911dd67787b5aaf9b36ade8",
|
PublicKey: "c9d556c6d3978d112d30616d0d20aaa81410e3653911dd67787b5aaf9b36ade8",
|
||||||
Relays: []string{"wss://nostr.com"},
|
Relays: []string{"wss://nostr.com"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Text: "#[2]",
|
Text: "#[2]",
|
||||||
|
Start: 26,
|
||||||
|
End: 30,
|
||||||
Event: &nostr.EventPointer{
|
Event: &nostr.EventPointer{
|
||||||
ID: "31d7c2875b5fc8e6f9c8f9dc1f84de1b6b91d1947ea4c59225e55c325d330fa8",
|
ID: "31d7c2875b5fc8e6f9c8f9dc1f84de1b6b91d1947ea4c59225e55c325d330fa8",
|
||||||
Relays: []string{},
|
Relays: []string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Text: "nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg",
|
Text: "nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg",
|
||||||
|
Start: 47,
|
||||||
|
End: 123,
|
||||||
Profile: &nostr.ProfilePointer{
|
Profile: &nostr.ProfilePointer{
|
||||||
PublicKey: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
PublicKey: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
||||||
Relays: []string{},
|
Relays: []string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4",
|
Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4",
|
||||||
|
Start: 127,
|
||||||
|
End: 201,
|
||||||
Event: &nostr.EventPointer{
|
Event: &nostr.EventPointer{
|
||||||
ID: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
ID: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
||||||
Relays: []string{},
|
Relays: []string{},
|
||||||
@@ -61,6 +69,14 @@ func TestParseReferences(t *testing.T) {
|
|||||||
t.Errorf("%d: got text %s, expected %s", i, g.Text, e.Text)
|
t.Errorf("%d: got text %s, expected %s", i, g.Text, e.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if g.Start != e.Start {
|
||||||
|
t.Errorf("%d: got start %d, expected %d", i, g.Start, e.Start)
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.End != e.End {
|
||||||
|
t.Errorf("%d: got end %d, expected %d", i, g.End, e.End)
|
||||||
|
}
|
||||||
|
|
||||||
if (g.Entity == nil && e.Entity != nil) ||
|
if (g.Entity == nil && e.Entity != nil) ||
|
||||||
(g.Event == nil && e.Event != nil) ||
|
(g.Event == nil && e.Event != nil) ||
|
||||||
(g.Profile == nil && e.Profile != nil) {
|
(g.Profile == nil && e.Profile != nil) {
|
||||||
|
Reference in New Issue
Block a user