underbbs/adapter/nostr.go

116 lines
2.4 KiB
Go
Raw Normal View History

2024-06-08 17:58:01 +00:00
package adapter
import (
"context"
"encoding/json"
"errors"
"fmt"
. "forge.lightcrystal.systems/lightcrystal/underbbs/models"
nostr "github.com/nbd-wtf/go-nostr"
"strings"
)
type NostrAdapter struct {
data chan SocketData
nickname string
privkey string
relays []*nostr.Relay
}
func (self *NostrAdapter) Name() string {
2024-07-07 20:54:33 +00:00
return self.nickname
}
2024-06-08 17:58:01 +00:00
func (self *NostrAdapter) Init(settings Settings, data chan SocketData) error {
self.nickname = settings.Nickname
self.privkey = *settings.PrivKey
self.data = data
ctx := context.Background()
for _, r := range settings.Relays {
pr, _ := nostr.RelayConnect(ctx, strings.Trim(r, " "))
if pr == nil {
return errors.New("Relay connection could not be completed")
}
self.relays = append(self.relays, pr)
}
return nil
}
func (self *NostrAdapter) Subscribe(filter string) []error {
var filters nostr.Filters
err := json.Unmarshal([]byte(filter), &filters)
if err != nil {
return []error{err}
}
errs := make([]error, 0)
fmt.Print("unmarshalled filter from json; iterating through relays to subscribe..")
for _, r := range self.relays {
fmt.Print(".")
sub, err := r.Subscribe(context.Background(), filters)
if err != nil {
errs = append(errs, err)
} else {
go func() {
for ev := range sub.Events {
fmt.Print("!")
// try sequentially to encode into an underbbs object
// and send it to the appropriate channel
m, err := self.nostrEventToMsg(ev)
2024-06-08 17:58:01 +00:00
if err == nil {
self.data <- m
}
}
}()
}
fmt.Println()
}
if len(errs) > 0 {
fmt.Println("subscription operation completed with errors")
return errs
}
fmt.Println("subscription operation completed without errors")
return nil
}
2024-06-22 16:39:02 +00:00
func (self *NostrAdapter) Fetch(etype string, ids []string) error {
2024-06-08 17:58:01 +00:00
return nil
}
2024-06-22 16:39:02 +00:00
func (self *NostrAdapter) Do(action string) error {
2024-06-08 17:58:01 +00:00
return nil
}
func (self *NostrAdapter) DefaultSubscriptionFilter() string {
return "[{\"kinds\":[1]}]"
}
func (self *NostrAdapter) nostrEventToMsg(evt *nostr.Event) (Message, error) {
2024-06-08 17:58:01 +00:00
m := Message{
2024-06-30 00:18:31 +00:00
Datagram: Datagram{
Protocol: "nostr",
Adapter: self.nickname,
Type: "message",
},
2024-06-08 17:58:01 +00:00
}
if evt == nil {
return m, errors.New("no event")
}
switch evt.Kind {
case nostr.KindTextNote:
2024-06-30 00:18:31 +00:00
m.Id = evt.ID
m.Author = evt.PubKey
m.Created = evt.CreatedAt.Time().UnixMilli()
2024-06-08 17:58:01 +00:00
m.Content = evt.Content
return m, nil
default:
return m, errors.New(fmt.Sprintf("unsupported event kind: %d", evt.Kind))
}
}