more conversions.
This commit is contained in:
@@ -6,9 +6,9 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip19"
|
||||
"github.com/nbd-wtf/go-nostr/nip73"
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip19"
|
||||
"fiatjaf.com/nostr/nip73"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
@@ -54,7 +54,7 @@ func Parse(content string) iter.Seq[Block] {
|
||||
var pointer nostr.Pointer
|
||||
switch prefix {
|
||||
case "npub":
|
||||
pointer = nostr.ProfilePointer{PublicKey: data.(string)}
|
||||
pointer = nostr.ProfilePointer{PublicKey: data.(nostr.PubKey)}
|
||||
case "nprofile", "nevent", "naddr":
|
||||
pointer = data.(nostr.Pointer)
|
||||
case "note", "nsec":
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip73"
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip73"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -19,9 +19,9 @@ func TestParse(t *testing.T) {
|
||||
"hello, nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg wrote nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4!",
|
||||
[]Block{
|
||||
{Text: "hello, ", Start: 0},
|
||||
{Text: "nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg", Start: 7, Pointer: nostr.ProfilePointer{PublicKey: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393"}},
|
||||
{Text: "nostr:nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg", Start: 7, Pointer: nostr.ProfilePointer{PublicKey: nostr.MustPubKeyFromHex("cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393")}},
|
||||
{Text: " wrote ", Start: 83},
|
||||
{Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4", Start: 90, Pointer: nostr.EventPointer{ID: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393"}},
|
||||
{Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4", Start: 90, Pointer: nostr.EventPointer{ID: nostr.MustIDFromHex("cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393")}},
|
||||
{Text: "!", Start: 164},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
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
|
||||
Pointer nostr.Pointer
|
||||
}
|
||||
|
||||
var mentionRegex = regexp.MustCompile(`\bnostr:((note|npub|naddr|nevent|nprofile)1\w+)\b`)
|
||||
|
||||
// Deprecated: this is useless, use Parse() isntead (but the semantics is different)
|
||||
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":
|
||||
pointer := nostr.ProfilePointer{
|
||||
PublicKey: data.(string), Relays: []string{},
|
||||
}
|
||||
tag := evt.Tags.FindWithValue("p", pointer.PublicKey)
|
||||
if tag != nil && len(tag) >= 3 {
|
||||
pointer.Relays = []string{tag[2]}
|
||||
}
|
||||
if nostr.IsValidPublicKey(pointer.PublicKey) {
|
||||
reference.Pointer = pointer
|
||||
}
|
||||
case "nprofile":
|
||||
pointer := data.(nostr.ProfilePointer)
|
||||
tag := evt.Tags.FindWithValue("p", pointer.PublicKey)
|
||||
if tag != nil && len(tag) >= 3 {
|
||||
pointer.Relays = append(pointer.Relays, tag[2])
|
||||
}
|
||||
if nostr.IsValidPublicKey(pointer.PublicKey) {
|
||||
reference.Pointer = pointer
|
||||
}
|
||||
case "note":
|
||||
// we don't even bother here because people using note1 codes aren't including relay hints anyway
|
||||
reference.Pointer = nostr.EventPointer{ID: data.(string), Relays: nil}
|
||||
case "nevent":
|
||||
pointer := data.(nostr.EventPointer)
|
||||
tag := evt.Tags.FindWithValue("e", pointer.ID)
|
||||
if tag != nil && len(tag) >= 3 {
|
||||
pointer.Relays = append(pointer.Relays, tag[2])
|
||||
if pointer.Author == "" && len(tag) >= 5 && nostr.IsValidPublicKey(tag[4]) {
|
||||
pointer.Author = tag[4]
|
||||
}
|
||||
}
|
||||
reference.Pointer = pointer
|
||||
case "naddr":
|
||||
pointer := data.(nostr.EntityPointer)
|
||||
tag := evt.Tags.FindWithValue("a", pointer.AsTagReference())
|
||||
if tag != nil && len(tag) >= 3 {
|
||||
pointer.Relays = append(pointer.Relays, tag[2])
|
||||
}
|
||||
reference.Pointer = pointer
|
||||
}
|
||||
}
|
||||
|
||||
if !yield(reference) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
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,
|
||||
Pointer: nostr.ProfilePointer{
|
||||
PublicKey: "cc6b9fea033f59c3c39a0407c5f1bfee439b077508d918cfdc0d6fd431d39393",
|
||||
Relays: []string{"wss://xawr.com"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Text: "nostr:nevent1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8ychxp5v4",
|
||||
Start: 90,
|
||||
End: 164,
|
||||
Pointer: 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