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)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user