feat: list groups
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff6bf16048
commit
18d810295b
2 files changed
+123
No files matched your search
@@ -0,0 +1,57 @@
|
|||||||
|
package signalbot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Group is a Signal group the bot account belongs to.
|
||||||
|
type Group struct {
|
||||||
|
// ID is the identifier to pass to Send ("group.<base64>").
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
// IsAdmin reports whether the bot account is an admin of the group.
|
||||||
|
IsAdmin bool
|
||||||
|
// AdminsOnly reports whether only admins may send messages
|
||||||
|
// (an "announcement" group, the closest thing to a channel).
|
||||||
|
AdminsOnly bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupEntry struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Admins []string `json:"admins"`
|
||||||
|
Permissions struct {
|
||||||
|
SendMessages string `json:"send_messages"`
|
||||||
|
} `json:"permissions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Groups lists the groups the bot account belongs to.
|
||||||
|
func (c *Client) Groups(ctx context.Context) ([]Group, error) {
|
||||||
|
acct, err := c.account(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var entries []groupEntry
|
||||||
|
if err := c.do(ctx, http.MethodGet, "/v1/groups/"+acct.id(), nil, &entries); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
groups := make([]Group, 0, len(entries))
|
||||||
|
for _, e := range entries {
|
||||||
|
groups = append(groups, Group{
|
||||||
|
ID: e.ID,
|
||||||
|
Name: e.Name,
|
||||||
|
IsAdmin: isSelf(acct, e.Admins),
|
||||||
|
AdminsOnly: e.Permissions.SendMessages == "only-admins",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return groups, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isSelf reports whether ids contains the account's number or ACI.
|
||||||
|
func isSelf(acct Account, ids []string) bool {
|
||||||
|
return slices.ContainsFunc(ids, func(id string) bool {
|
||||||
|
return id != "" && (id == acct.Number || id == acct.ACI)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package signalbot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (f *fakeAPI) groups(path, body string) {
|
||||||
|
f.mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(body))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupsMapping(t *testing.T) {
|
||||||
|
f := newFakeAPI(t)
|
||||||
|
f.accounts(`[{"number":"+331","aci":"a-1"}]`)
|
||||||
|
f.groups("/v1/groups/+331", `[
|
||||||
|
{"id":"group.QQ==","internal_id":"A","name":"News","admins":["+331"],"members":["+331","+332"],
|
||||||
|
"permissions":{"add_members":"only-admins","edit_group":"only-admins","send_messages":"only-admins"}},
|
||||||
|
{"id":"group.Qg==","internal_id":"B","name":"Chat","admins":["+332"],
|
||||||
|
"permissions":{"send_messages":"every-member"}}
|
||||||
|
]`)
|
||||||
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Groups: %v", err)
|
||||||
|
}
|
||||||
|
want := []Group{
|
||||||
|
{ID: "group.QQ==", Name: "News", IsAdmin: true, AdminsOnly: true},
|
||||||
|
{ID: "group.Qg==", Name: "Chat", IsAdmin: false, AdminsOnly: false},
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Fatalf("got %+v, want %+v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupsIsAdminByNumberOrACI(t *testing.T) {
|
||||||
|
f := newFakeAPI(t)
|
||||||
|
f.accounts(`[{"number":"+331","aci":"a-1"}]`)
|
||||||
|
f.groups("/v1/groups/+331", `[
|
||||||
|
{"id":"group.A","name":"A","admins":["+331"]},
|
||||||
|
{"id":"group.B","name":"B","admins":["a-1"]},
|
||||||
|
{"id":"group.C","name":"C","admins":["+339"]}
|
||||||
|
]`)
|
||||||
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Groups: %v", err)
|
||||||
|
}
|
||||||
|
for i, want := range []bool{true, true, false} {
|
||||||
|
if got[i].IsAdmin != want {
|
||||||
|
t.Errorf("group %s IsAdmin = %v, want %v", got[i].ID, got[i].IsAdmin, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupsEmpty(t *testing.T) {
|
||||||
|
f := newFakeAPI(t)
|
||||||
|
f.accounts(`[{"number":"+331"}]`)
|
||||||
|
f.groups("/v1/groups/+331", `[]`)
|
||||||
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
||||||
|
if err != nil || got == nil || len(got) != 0 {
|
||||||
|
t.Fatalf("got %#v, %v; want empty non-nil slice", got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user