62 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-07-05 00:32:46 -03:00
package relay29
import (
"context"
"github.com/fiatjaf/eventstore"
"github.com/fiatjaf/set"
"github.com/nbd-wtf/go-nostr"
"github.com/puzpuzpuz/xsync/v3"
)
type State struct {
Domain string
Groups *xsync.MapOf[string, *Group]
DB eventstore.Store
2024-09-14 11:02:22 +08:00
Relay interface {
BroadcastEvent(*nostr.Event)
AddEvent(context.Context, *nostr.Event) (skipBroadcast bool, writeError error)
}
GetAuthed func(context.Context) string
AllowPrivateGroups bool
2024-07-05 00:32:46 -03:00
deletedCache set.Set[string]
publicKey string
2024-09-14 11:02:22 +08:00
secretKey string
2024-07-05 00:32:46 -03:00
}
type Options struct {
Domain string
DB eventstore.Store
SecretKey string
}
2024-09-14 11:02:22 +08:00
func New(opts Options) *State {
2024-07-05 00:32:46 -03:00
pubkey, _ := nostr.GetPublicKey(opts.SecretKey)
// events that just got deleted will be cached here for `tooOld` seconds such that someone doesn't rebroadcast
// them -- after that time we won't accept them anymore, so we can remove their ids from this cache
deletedCache := set.NewSliceSet[string]()
// we keep basic data about all groups in memory
groups := xsync.NewMapOf[string, *Group]()
state := &State{
2024-09-14 11:02:22 +08:00
Domain: opts.Domain,
Groups: groups,
DB: opts.DB,
AllowPrivateGroups: true,
deletedCache: deletedCache,
publicKey: pubkey,
secretKey: opts.SecretKey,
2024-07-05 00:32:46 -03:00
}
// load all groups
state.loadGroups(context.Background())
return state
}