package main import ( "encoding/json" "errors" "os" "path/filepath" "sort" "sync" "time" ) type OwnerKind string const ( OwnerUser OwnerKind = "user" OwnerChannel OwnerKind = "channel" ) type Owner struct { ID string `json:"id"` Kind OwnerKind `json:"kind"` Machine string `json:"machine"` Secret string `json:"secret"` Size Size `json:"size"` LastActive time.Time `json:"last_active"` Node string `json:"node,omitempty"` Drive string `json:"drive,omitempty"` StorageMinMB int64 `json:"storage_min_mb"` StorageMaxMB int64 `json:"storage_max_mb"` // 0 means the config default. IdleTimeout time.Duration `json:"idle_timeout,omitempty"` } const Indefinite time.Duration = -1 type Session struct { Channel string `json:"channel"` ThreadTS string `json:"thread_ts"` OwnerID string `json:"owner_id"` ACPID string `json:"acp_id"` Title string `json:"title"` CreatedAt time.Time `json:"created_at"` } type Store struct { path string mu sync.Mutex data struct { Owners map[string]*Owner `json:"owners"` Sessions map[string]*Session `json:"sessions"` Permissions *PermissionsConfig `json:"permissions,omitempty"` } } func OpenStore(path string) (*Store, error) { s := &Store{path: path} s.data.Owners = map[string]*Owner{} s.data.Sessions = map[string]*Session{} b, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { return s, os.MkdirAll(filepath.Dir(path), 0o700) } if err != nil { return nil, err } if err := json.Unmarshal(b, &s.data); err != nil { return nil, err } if s.data.Owners == nil { s.data.Owners = map[string]*Owner{} } if s.data.Sessions == nil { s.data.Sessions = map[string]*Session{} } return s, nil } func (s *Store) save() error { b, err := json.MarshalIndent(&s.data, "", " ") if err != nil { return err } tmp := s.path + ".tmp" if err := os.WriteFile(tmp, b, 0o600); err != nil { return err } return os.Rename(tmp, s.path) } func (s *Store) Owner(id string) *Owner { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { c := *u return &c } return nil } func (s *Store) OwnerByMachine(machine string) *Owner { s.mu.Lock() defer s.mu.Unlock() for _, u := range s.data.Owners { if u.Machine == machine { c := *u return &c } } return nil } func (s *Store) PutOwner(u *Owner) error { s.mu.Lock() defer s.mu.Unlock() c := *u s.data.Owners[u.ID] = &c return s.save() } func (s *Store) Touch(id string) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.LastActive = time.Now() return s.save() } return nil } func (s *Store) SetSize(id string, size Size) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.Size = size return s.save() } return nil } func (s *Store) Owners() []Owner { s.mu.Lock() defer s.mu.Unlock() out := make([]Owner, 0, len(s.data.Owners)) for _, u := range s.data.Owners { out = append(out, *u) } return out } func (s *Store) SetStorage(id string, minMB, maxMB int64) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.StorageMinMB, u.StorageMaxMB = minMB, maxMB return s.save() } return nil } func (s *Store) SetPlacement(id, node, drive string) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.Node, u.Drive = node, drive return s.save() } return nil } func (s *Store) DeleteSessions(ownerID string) error { s.mu.Lock() defer s.mu.Unlock() for k, x := range s.data.Sessions { if x.OwnerID == ownerID { delete(s.data.Sessions, k) } } return s.save() } func (s *Store) SetIdleTimeout(id string, d time.Duration) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.IdleTimeout = d return s.save() } return nil } // The config's permissions are only a starting point: once saved here, // changes made in Slack win over later config edits. func (s *Store) SeedPermissions(initial PermissionsConfig) (seeded bool, err error) { s.mu.Lock() defer s.mu.Unlock() if s.data.Permissions != nil { return false, nil } p := initial.clone() s.data.Permissions = &p return true, s.save() } func (s *Store) Permissions() PermissionsConfig { s.mu.Lock() defer s.mu.Unlock() if s.data.Permissions == nil { return PermissionsConfig{} } return s.data.Permissions.clone() } func (s *Store) UpdatePermissions(change func(p *PermissionsConfig) error) error { s.mu.Lock() defer s.mu.Unlock() var p PermissionsConfig if s.data.Permissions != nil { p = s.data.Permissions.clone() } else { p = PermissionsConfig{}.clone() } if err := change(&p); err != nil { return err } if err := p.validate(); err != nil { return err } s.data.Permissions = &p return s.save() } func sessionKey(channel, threadTS string) string { return channel + "/" + threadTS } func (s *Store) Session(channel, threadTS string) *Session { s.mu.Lock() defer s.mu.Unlock() if x, ok := s.data.Sessions[sessionKey(channel, threadTS)]; ok { c := *x return &c } return nil } func (s *Store) AddSession(x *Session) error { s.mu.Lock() defer s.mu.Unlock() c := *x s.data.Sessions[sessionKey(x.Channel, x.ThreadTS)] = &c return s.save() } func (s *Store) Sessions(ownerID string, limit int) []Session { s.mu.Lock() defer s.mu.Unlock() var out []Session for _, x := range s.data.Sessions { if x.OwnerID == ownerID { out = append(out, *x) } } sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt.After(out[j].CreatedAt) }) if limit > 0 && len(out) > limit { out = out[:limit] } return out }