making use of the extended perms and added future work
This commit is contained in:
1 parent
296e465f48
commit
22fd8fbce8
19 files changed
+617
-40
No files matched your search
@@ -16,9 +16,10 @@ 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"`
|
||||
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"`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +27,7 @@ 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) {
|
||||
@@ -43,6 +45,9 @@ func Open(path string) (*Store, error) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -220,6 +225,44 @@ func (s *Store) AddSession(x *model.Session) error {
|
||||
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()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -64,3 +65,30 @@ func TestPermissionsSavedAndSeededOnce(t *testing.T) {
|
||||
t.Fatal("invalid changes must be refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenModes(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
if err := os.WriteFile(path, []byte(`{"owners":{},"sessions":{"C1/1.0":{"channel":"C1","thread_ts":"1.0","acp_id":"a"}}}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.ThreadListen("C1", "1.0") != model.ListenMentions {
|
||||
t.Fatal("old state should default to only-mentions")
|
||||
}
|
||||
_ = s.SetChannelListen("C1", model.ListenAll)
|
||||
if s.ThreadListen("C1", "1.0") != model.ListenAll {
|
||||
t.Fatal("a thread without its own setting follows the channel")
|
||||
}
|
||||
_ = s.SetSessionListen("C1", "1.0", model.ListenMentions)
|
||||
|
||||
s2, _ := Open(path)
|
||||
if s2.ThreadListen("C1", "1.0") != model.ListenMentions || s2.ChannelListen("C1") != model.ListenAll {
|
||||
t.Fatal("listen settings not saved")
|
||||
}
|
||||
if s2.ChannelListen("C2") != model.ListenMentions {
|
||||
t.Fatal("other channels keep the default")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user