-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstate.go
74 lines (59 loc) · 1.89 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package relay29
import (
"context"
"fmt"
"github.com/fiatjaf/eventstore"
"github.com/fiatjaf/set"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip29"
"github.com/puzpuzpuz/xsync/v3"
)
type State struct {
Domain string
Groups *xsync.MapOf[string, *Group]
DB eventstore.Store
Relay interface {
BroadcastEvent(*nostr.Event)
AddEvent(context.Context, *nostr.Event) (skipBroadcast bool, writeError error)
}
GetAuthed func(context.Context) string
AllowPrivateGroups bool
deletedCache set.Set[string]
publicKey string
secretKey string
defaultRoles []*nip29.Role
groupCreatorDefaultRole *nip29.Role
AllowAction func(ctx context.Context, group nip29.Group, role *nip29.Role, action Action) bool
}
type Options struct {
Domain string
DB eventstore.Store
SecretKey string
DefaultRoles []*nip29.Role
GroupCreatorDefaultRole *nip29.Role
}
func New(opts Options) *State {
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{
Domain: opts.Domain,
Groups: groups,
DB: opts.DB,
AllowPrivateGroups: true,
deletedCache: deletedCache,
publicKey: pubkey,
secretKey: opts.SecretKey,
defaultRoles: opts.DefaultRoles,
groupCreatorDefaultRole: opts.GroupCreatorDefaultRole,
}
// load all groups
err := state.loadGroupsFromDB(context.Background())
if err != nil {
panic(fmt.Errorf("failed to load groups from db: %w", err))
}
return state
}