package state import ( "encoding/json" "errors" "os" "path/filepath" "sort" "sync" "time" "slackerbot/internal/model" ) type Store struct { path string mu sync.Mutex data struct { Owners map[string]*model.Owner `json:"owners"` Sessions map[string]*model.Session `json:"sessions"` Permissions *model.PermissionsConfig `json:"permissions,omitempty"` Listen map[string]model.ListenMode `json:"listen,omitempty"` } } func Open(path string) (*Store, error) { s := &Store{path: path} s.data.Owners = map[string]*model.Owner{} s.data.Sessions = map[string]*model.Session{} s.data.Listen = map[string]model.ListenMode{} 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]*model.Owner{} } if s.data.Sessions == nil { s.data.Sessions = map[string]*model.Session{} } if s.data.Listen == nil { s.data.Listen = map[string]model.ListenMode{} } 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) *model.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) *model.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 *model.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 model.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() []model.Owner { s.mu.Lock() defer s.mu.Unlock() out := make([]model.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) SetLifecycle(id, channel, ts string) error { s.mu.Lock() defer s.mu.Unlock() if u, ok := s.data.Owners[id]; ok { u.LifecycleChannel, u.LifecycleTS = channel, ts return s.save() } return nil } 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 model.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() model.PermissionsConfig { s.mu.Lock() defer s.mu.Unlock() if s.data.Permissions == nil { return model.PermissionsConfig{} } return s.data.Permissions.Clone() } func (s *Store) UpdatePermissions(change func(p *model.PermissionsConfig) error) error { s.mu.Lock() defer s.mu.Unlock() var p model.PermissionsConfig if s.data.Permissions != nil { p = s.data.Permissions.Clone() } else { p = model.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) *model.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 *model.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) SetSessionListen(channel, threadTS string, mode model.ListenMode) error { s.mu.Lock() defer s.mu.Unlock() if x, ok := s.data.Sessions[sessionKey(channel, threadTS)]; ok { x.Listen = mode return s.save() } return nil } func (s *Store) ChannelListen(channel string) model.ListenMode { s.mu.Lock() defer s.mu.Unlock() if mode, ok := s.data.Listen[channel]; ok { return mode } return model.ListenMentions } func (s *Store) SetChannelListen(channel string, mode model.ListenMode) error { s.mu.Lock() defer s.mu.Unlock() if mode == model.ListenMentions { delete(s.data.Listen, channel) } else { s.data.Listen[channel] = mode } return s.save() } // What a thread uses: its own setting if it has one, else the channel's. func (s *Store) ThreadListen(channel, threadTS string) model.ListenMode { if x := s.Session(channel, threadTS); x != nil && x.Listen != "" { return x.Listen } return s.ChannelListen(channel) } func (s *Store) Sessions(ownerID string, limit int) []model.Session { s.mu.Lock() defer s.mu.Unlock() var out []model.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 }