making use of the extended perms and added future work

This commit is contained in:
ari committed 2026-11-01 13:55:25 -05:00
1 parent 296e465f48
commit 22fd8fbce8
19 files changed
+617 -40

No files matched your search

+34
View File
@@ -240,6 +240,40 @@ func ParseToggle(word string) (on, ok bool) {
return false, false
}
var listenModes = map[string]model.ListenMode{
"all": model.ListenAll, "all-messages": model.ListenAll, "all-message": model.ListenAll, "everything": model.ListenAll,
"mentions": model.ListenMentions, "only-mentions": model.ListenMentions, "only-mention": model.ListenMentions,
"mentions-only": model.ListenMentions,
}
type ListenCmd struct {
Channel bool
// Empty means show the current setting.
Mode model.ListenMode
}
// IsListen accepts `listen` and `/listen`. Slack only treats a leading slash
// as a slash command, so `@slacker /listen …` reaches the bot as text.
func IsListen(verb string) bool { return verb == "listen" || verb == "/listen" }
func ParseListen(args string) (ListenCmd, error) {
var c ListenCmd
words := strings.Fields(strings.ToLower(args))
if len(words) > 0 && (words[0] == "channel" || words[0] == "default") {
c.Channel, words = true, words[1:]
}
switch len(words) {
case 0:
return c, nil
case 1:
if mode, ok := listenModes[words[0]]; ok {
c.Mode = mode
return c, nil
}
}
return c, ErrNotCommand
}
func NeededPermission(verb, args string) model.Action {
switch verb {
case "scale":
@@ -152,3 +152,28 @@ func TestParseGrant(t *testing.T) {
t.Error("an unknown permission after a mention should be a usage error")
}
}
func TestParseListen(t *testing.T) {
cases := map[string]ListenCmd{
"": {},
"all-messages": {Mode: model.ListenAll},
"all-message": {Mode: model.ListenAll},
"Only-Mentions": {Mode: model.ListenMentions},
"channel all-messages": {Channel: true, Mode: model.ListenAll},
"default mentions": {Channel: true, Mode: model.ListenMentions},
"channel": {Channel: true},
}
for in, want := range cases {
if got, err := ParseListen(in); err != nil || got != want {
t.Fatalf("listen %q = %+v, %v; want %+v", in, got, err, want)
}
}
for _, in := range []string{"to me carefully", "all of it", "channel loudly"} {
if _, err := ParseListen(in); !errors.Is(err, ErrNotCommand) {
t.Fatalf("listen %q should go to the agent", in)
}
}
if !IsListen("/listen") || !IsListen("listen") || IsListen("/listening") {
t.Fatal("listen verb")
}
}