Documentation
¶
Index ¶
- Constants
- func ContainsPrefixOf(haystack []string, needle string) bool
- func FilterEqual(a Filter, b Filter) bool
- func GeneratePrivateKey() string
- func GetPublicKey(sk string) (string, error)
- func NormalizeURL(u string) string
- func Similar[E constraints.Ordered](as, bs []E) bool
- type Connection
- type Event
- func (evt Event) CheckSignature() (bool, error)
- func (evt Event) GetExtra(key string) any
- func (evt Event) GetExtraBoolean(key string) bool
- func (evt Event) GetExtraNumber(key string) float64
- func (evt Event) GetExtraString(key string) string
- func (evt *Event) GetID() string
- func (evt Event) MarshalJSON() ([]byte, error)
- func (evt *Event) Serialize() []byte
- func (evt *Event) SetExtra(key string, value any)
- func (evt *Event) Sign(privateKey string) error
- func (evt *Event) UnmarshalJSON(payload []byte) error
- type EventMessage
- type Filter
- type Filters
- type ProfileMetadata
- type Relay
- func (r *Relay) Close() error
- func (r *Relay) Connect(ctx context.Context) error
- func (r *Relay) PrepareSubscription() *Subscription
- func (r *Relay) Publish(ctx context.Context, event Event) Status
- func (r *Relay) QuerySync(ctx context.Context, filter Filter) []Event
- func (r *Relay) String() string
- func (r *Relay) Subscribe(ctx context.Context, filters Filters) *Subscription
- type Status
- type Subscription
- type Tag
- type TagMap
- type Tags
- func (tags Tags) AppendUnique(tag Tag) Tags
- func (tags Tags) ContainsAny(tagName string, values []string) bool
- func (tags Tags) FilterOut(tagPrefix []string) Tags
- func (tags Tags) GetAll(tagPrefix []string) Tags
- func (tags Tags) GetFirst(tagPrefix []string) *Tag
- func (tags Tags) GetLast(tagPrefix []string) *Tag
- func (t *Tags) Scan(src interface{}) error
Examples ¶
Constants ¶
const ( KindSetMetadata int = 0 KindTextNote int = 1 KindRecommendServer int = 2 KindContactList int = 3 KindEncryptedDirectMessage int = 4 KindDeletion int = 5 KindBoost int = 6 KindReaction int = 7 KindChannelCreation int = 40 KindChannelMetadata int = 41 KindChannelMessage int = 42 KindChannelHideMessage int = 43 KindChannelMuteUser int = 44 )
Variables ¶
This section is empty.
Functions ¶
func ContainsPrefixOf ¶
func FilterEqual ¶
func GeneratePrivateKey ¶
func GeneratePrivateKey() string
func GetPublicKey ¶
func NormalizeURL ¶
Example ¶
fmt.Println(NormalizeURL("")) fmt.Println(NormalizeURL("wss://x.com/y")) fmt.Println(NormalizeURL("wss://x.com/y/")) fmt.Println(NormalizeURL("http://x.com/y")) fmt.Println(NormalizeURL(NormalizeURL("http://x.com/y"))) fmt.Println(NormalizeURL("wss://x.com")) fmt.Println(NormalizeURL("wss://x.com/")) fmt.Println(NormalizeURL(NormalizeURL(NormalizeURL("wss://x.com/")))) fmt.Println(NormalizeURL("x.com")) fmt.Println(NormalizeURL("x.com/")) fmt.Println(NormalizeURL("x.com////")) fmt.Println(NormalizeURL("x.com/?x=23"))
Output: wss://x.com/y wss://x.com/y ws://x.com/y ws://x.com/y wss://x.com wss://x.com wss://x.com wss://x.com wss://x.com wss://x.com wss://x.com?x=23
func Similar ¶
func Similar[E constraints.Ordered](as, bs []E) bool
Types ¶
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
func NewConnection ¶
func NewConnection(socket *websocket.Conn) *Connection
func (*Connection) Close ¶
func (c *Connection) Close() error
func (*Connection) WriteJSON ¶
func (c *Connection) WriteJSON(v interface{}) error
func (*Connection) WriteMessage ¶
func (c *Connection) WriteMessage(messageType int, data []byte) error
type Event ¶
type Event struct { ID string PubKey string CreatedAt time.Time Kind int Tags Tags Content string Sig string // contains filtered or unexported fields }
func (Event) CheckSignature ¶
CheckSignature checks if the signature is valid for the id (which is a hash of the serialized event content). returns an error if the signature itself is invalid.
func (Event) GetExtra ¶ added in v0.8.2
GetExtra tries to get a value under the given key that may be present in the event object but is hidden in the basic type since it is out of the spec.
func (Event) GetExtraBoolean ¶ added in v0.8.2
GetExtraBoolean is like Event.GetExtra, but only works if the value is a boolean, otherwise returns the zero-value.
func (Event) GetExtraNumber ¶ added in v0.8.2
GetExtraNumber is like Event.GetExtra, but only works if the value is a float64, otherwise returns the zero-value.
func (Event) GetExtraString ¶ added in v0.8.2
GetExtraString is like Event.GetExtra, but only works if the value is a string, otherwise returns the zero-value.
func (Event) MarshalJSON ¶
func (*Event) Serialize ¶
Serialize outputs a byte array that can be hashed/signed to identify/authenticate
func (*Event) SetExtra ¶ added in v0.8.2
SetExtra sets an out-of-the-spec value under the given key into the event object.
func (*Event) UnmarshalJSON ¶
type EventMessage ¶
type Filter ¶
type Filter struct { IDs []string Kinds []int Authors []string Tags TagMap Since *time.Time Until *time.Time Limit int }
func (Filter) MarshalJSON ¶
func (*Filter) UnmarshalJSON ¶
type ProfileMetadata ¶ added in v0.9.0
type ProfileMetadata struct { Name string `json:"name,omitempty"` About string `json:"about,omitempty"` Picture string `json:"picture,omitempty"` NIP05 string `json:"nip05,omitempty"` }
func ParseMetadata ¶ added in v0.9.0
func ParseMetadata(event Event) (*ProfileMetadata, error)
type Relay ¶
type Relay struct { URL string Connection *Connection Notices chan string ConnectionError chan error // contains filtered or unexported fields }
func RelayConnect ¶ added in v0.8.1
RelayConnect returns a relay object connected to url Once successfully connected, cancelling ctx has no effect To close the connection, call r.Close()
func (*Relay) Connect ¶
Connect tries to establish a websocket connection to r.URL. If the context expires before the connection is complete, an error is returned. Once successfully connected, context expiration has no effect: call r.Close to close the connection.
func (*Relay) PrepareSubscription ¶ added in v0.8.2
func (r *Relay) PrepareSubscription() *Subscription
func (*Relay) Publish ¶
Publish sends an "EVENT" command to the relay r as in NIP-01 status can be: success, failed, or sent (no response from relay before ctx times out)
type Subscription ¶
type Subscription struct { Relay *Relay Filters Filters Events chan Event EndOfStoredEvents chan struct{} // contains filtered or unexported fields }
func (*Subscription) Fire ¶ added in v0.8.2
func (sub *Subscription) Fire(ctx context.Context)
Fire sends the "REQ" command to the relay when ctx is cancelled, sub.Unsub() is called, closing the subscription
func (*Subscription) Sub ¶
func (sub *Subscription) Sub(ctx context.Context, filters Filters)
Sub sets sub.Filters and then calls sub.Fire(ctx)
func (*Subscription) Unsub ¶
func (sub *Subscription) Unsub()
Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01 Unsub() also closes the channel sub.Events
type Tag ¶
type Tag []string
func (Tag) StartsWith ¶
StartsWith checks if a tag contains a prefix. for example,
["p", "abcdef...", "wss://relay.com"]
would match against
["p", "abcdef..."]
or even
["p", "abcdef...", "wss://"]
type Tags ¶
type Tags []Tag
func (Tags) AppendUnique ¶
AppendUnique appends a tag if it doesn't exist yet, otherwise does nothing
func (Tags) FilterOut ¶
FilterOut removes all tags that match the prefix, see Tag.StartsWith
func (Tags) GetAll ¶
GetLast gets all the tags that match the prefix, see Tag.StartsWith
func (Tags) GetFirst ¶
GetFirst gets the first tag in tags that matches the prefix, see Tag.StartsWith
func (Tags) GetLast ¶
GetLast gets the last tag in tags that matches the prefix, see Tag.StartsWith