From 18d810295b4d2cfcee8182b99d16be486d15ad10 Mon Sep 17 00:00:00 2001 From: Vincent Bourdon Date: Fri, 20 Nov 2026 15:40:56 +0100 Subject: [PATCH] feat: list groups Co-Authored-By: Claude Opus 5.5 --- groups.go | 57 +++++++++++++++++++++++++++++++++++++++++++ groups_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 groups.go create mode 100644 groups_test.go diff --git a/groups.go b/groups.go new file mode 100644 index 0000000..171a9cf --- /dev/null +++ b/groups.go @@ -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."). + 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) + }) +} diff --git a/groups_test.go b/groups_test.go new file mode 100644 index 0000000..b84c62e --- /dev/null +++ b/groups_test.go @@ -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) + } +}