mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-19 18:47:37 +01:00
docstrings for many functions.
This commit is contained in:
18
pointers.go
18
pointers.go
@@ -6,9 +6,18 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Pointer is an interface for different types of Nostr pointers.
|
||||
//
|
||||
// In this context, a "pointer" is a reference to an event or profile potentially including
|
||||
// relays and other metadata that might help find it.
|
||||
type Pointer interface {
|
||||
// AsTagReference returns the pointer as a string as it would be seen in the value of a tag (i.e. the tag's second item).
|
||||
AsTagReference() string
|
||||
|
||||
// AsTag converts the pointer with all the information available to a tag that can be included in events.
|
||||
AsTag() Tag
|
||||
|
||||
// AsFilter converts the pointer to a Filter that can be used to query for it on relays.
|
||||
AsFilter() Filter
|
||||
MatchesEvent(Event) bool
|
||||
}
|
||||
@@ -19,11 +28,13 @@ var (
|
||||
_ Pointer = (*EntityPointer)(nil)
|
||||
)
|
||||
|
||||
// ProfilePointer represents a pointer to a Nostr profile.
|
||||
type ProfilePointer struct {
|
||||
PublicKey string `json:"pubkey"`
|
||||
Relays []string `json:"relays,omitempty"`
|
||||
}
|
||||
|
||||
// ProfilePointerFromTag creates a ProfilePointer from a "p" tag (but it doesn't have to be necessarily a "p" tag, could be something else).
|
||||
func ProfilePointerFromTag(refTag Tag) (ProfilePointer, error) {
|
||||
pk := (refTag)[1]
|
||||
if !IsValidPublicKey(pk) {
|
||||
@@ -41,6 +52,7 @@ func ProfilePointerFromTag(refTag Tag) (ProfilePointer, error) {
|
||||
return pointer, nil
|
||||
}
|
||||
|
||||
// MatchesEvent checks if the pointer matches an event.
|
||||
func (ep ProfilePointer) MatchesEvent(_ Event) bool { return false }
|
||||
func (ep ProfilePointer) AsTagReference() string { return ep.PublicKey }
|
||||
func (ep ProfilePointer) AsFilter() Filter { return Filter{Authors: []string{ep.PublicKey}} }
|
||||
@@ -52,6 +64,7 @@ func (ep ProfilePointer) AsTag() Tag {
|
||||
return Tag{"p", ep.PublicKey}
|
||||
}
|
||||
|
||||
// EventPointer represents a pointer to a nostr event.
|
||||
type EventPointer struct {
|
||||
ID string `json:"id"`
|
||||
Relays []string `json:"relays,omitempty"`
|
||||
@@ -59,6 +72,7 @@ type EventPointer struct {
|
||||
Kind int `json:"kind,omitempty"`
|
||||
}
|
||||
|
||||
// EventPointerFromTag creates an EventPointer from an "e" tag (but it could be other tag name, it isn't checked).
|
||||
func EventPointerFromTag(refTag Tag) (EventPointer, error) {
|
||||
id := (refTag)[1]
|
||||
if !IsValid32ByteHex(id) {
|
||||
@@ -83,6 +97,7 @@ func (ep EventPointer) MatchesEvent(evt Event) bool { return evt.ID == ep.ID }
|
||||
func (ep EventPointer) AsTagReference() string { return ep.ID }
|
||||
func (ep EventPointer) AsFilter() Filter { return Filter{IDs: []string{ep.ID}} }
|
||||
|
||||
// AsTag converts the pointer to a Tag.
|
||||
func (ep EventPointer) AsTag() Tag {
|
||||
if len(ep.Relays) > 0 {
|
||||
if ep.Author != "" {
|
||||
@@ -94,6 +109,7 @@ func (ep EventPointer) AsTag() Tag {
|
||||
return Tag{"e", ep.ID}
|
||||
}
|
||||
|
||||
// EntityPointer represents a pointer to a nostr entity (addressable event).
|
||||
type EntityPointer struct {
|
||||
PublicKey string `json:"pubkey"`
|
||||
Kind int `json:"kind,omitempty"`
|
||||
@@ -101,6 +117,7 @@ type EntityPointer struct {
|
||||
Relays []string `json:"relays,omitempty"`
|
||||
}
|
||||
|
||||
// EntityPointerFromTag creates an EntityPointer from an "a" tag (but it doesn't check if the tag is really "a", it could be anything).
|
||||
func EntityPointerFromTag(refTag Tag) (EntityPointer, error) {
|
||||
spl := strings.SplitN(refTag[1], ":", 3)
|
||||
if len(spl) != 3 {
|
||||
@@ -129,6 +146,7 @@ func EntityPointerFromTag(refTag Tag) (EntityPointer, error) {
|
||||
return pointer, nil
|
||||
}
|
||||
|
||||
// MatchesEvent checks if the pointer matches an event.
|
||||
func (ep EntityPointer) MatchesEvent(evt Event) bool {
|
||||
return ep.PublicKey == evt.PubKey &&
|
||||
ep.Kind == evt.Kind &&
|
||||
|
||||
Reference in New Issue
Block a user