feat(cli): publishers in member add, ls, set and import

- member add --publisher makes a publisher, --publisher=false demotes; without
  the flag an existing member keeps its role
- member ls has a ROLE column: publisher or subscriber
- new member set <list> <email> --publisher=true|false (usage error without the flag)
- member import reads an optional third column, the keyword publisher (any case);
  anything else there fails that line; an empty third column is ignored
- list set --post publishers; help text updated
- the old import test now uses the publisher keyword in its third column
This commit is contained in:
tgiachi 2026-11-10 12:40:10 +01:00
commit e850547b3d
4 changed files with 234 additions and 14 deletions

View file

@ -29,15 +29,20 @@ Administration (talks to the REST API):
list ls show all lists
list add <name@domain> [--title T] create a list
list show <list> show one list
list set <list> [--title T] [--post members|moderated]
list set <list> [--title T] [--post members|moderated|publishers]
[--join open|closed] [--archive public|private|off]
list rm <list> --yes delete a list with its members and archive
member ls <list> [--emails] show subscribers
member add <list> <email> [--name N] subscribe (or re-enable) an address
member ls <list> [--emails] show subscribers and publishers
member add <list> <email> [--name N] [--publisher]
subscribe (or re-enable) an address;
--publisher lets it post to a publishers list,
--publisher=false takes that away
member set <list> <email> --publisher=true|false
make a member a publisher or a plain subscriber
member rm <list> <email> unsubscribe an address
member import <list> <file|-> subscribe addresses from a file or stdin,
one "email[,name]" per line
one "email[,name[,publisher]]" per line
held ls <list> messages waiting for moderation
held approve <id> deliver a held message
@ -101,7 +106,7 @@ func Run(args []string, io IO, getenv func(string) string) int {
commands := map[string]map[string]func([]string) int{
"list": {"ls": c.listLs, "add": c.listAdd, "show": c.listShow, "set": c.listSet, "rm": c.listRm},
"member": {"ls": c.memberLs, "add": c.memberAdd, "rm": c.memberRm, "import": c.memberImport},
"member": {"ls": c.memberLs, "add": c.memberAdd, "set": c.memberSet, "rm": c.memberRm, "import": c.memberImport},
"held": {"ls": c.heldLs, "approve": c.heldApprove, "discard": c.heldDiscard},
"archive": {"ls": c.archiveLs, "show": c.archiveShow, "rm": c.archiveRm, "import": c.archiveImport},
"queue": {"ls": c.queueLs, "rm": c.queueRm},

View file

@ -274,7 +274,7 @@ func TestMembers(t *testing.T) {
func TestMemberImport(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
input := "# subscribers exported from Mailman\n\nalice@example.com, Alice Smith\nbob@example.com\n carol@example.com ,Carol \nnot-an-address\ndave@example.com,Dave,extra\n"
input := "# subscribers exported from Mailman\n\nalice@example.com, Alice Smith\nbob@example.com\n carol@example.com ,Carol \nnot-an-address\ndave@example.com,Dave,Publisher\n"
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, input, "member", "import", "dev@example.org", "-")
if code != 1 {
t.Fatalf("a failed line must make the exit code non-zero, got %d", code)

View file

@ -179,6 +179,14 @@ type member struct {
DisplayName string `json:"display_name"`
Disabled bool `json:"disabled"`
BounceCount int `json:"bounce_count"`
Publisher bool `json:"publisher"`
}
func (m member) role() string {
if m.Publisher {
return "publisher"
}
return "subscriber"
}
func (m member) status() string {
@ -228,9 +236,9 @@ func (c *cmd) memberLs(args []string) int {
fmt.Fprintln(c.io.Out, "No members yet.")
return 0
}
rows := [][]string{{"ADDRESS", "NAME", "STATUS"}}
rows := [][]string{{"ADDRESS", "NAME", "ROLE", "STATUS"}}
for _, m := range ms {
rows = append(rows, []string{m.Address, m.DisplayName, m.status()})
rows = append(rows, []string{m.Address, m.DisplayName, m.role(), m.status()})
}
c.table(rows)
return 0
@ -239,18 +247,19 @@ func (c *cmd) memberLs(args []string) int {
func (c *cmd) memberAdd(args []string) int {
fs := newFlagSet("member add")
name := fs.String("name", "", "")
pub := fs.Bool("publisher", false, "")
pos, err := parseArgs(fs, args)
if err != nil {
return c.usageError("%v", err)
}
if len(pos) != 2 {
return c.usageError("usage: parsimony member add <list> <email> [--name NAME]")
return c.usageError("usage: parsimony member add <list> <email> [--name NAME] [--publisher]")
}
l, err := c.resolveList(pos[0])
if err != nil {
return c.fail(err)
}
raw, err := c.addMember(l.ID, pos[1], *name)
raw, err := c.addMember(l.ID, pos[1], *name, flagBool(fs, "publisher", *pub))
if err != nil {
return c.fail(err)
}
@ -259,16 +268,65 @@ func (c *cmd) memberAdd(args []string) int {
}
var m member
json.Unmarshal(raw, &m)
fmt.Fprintf(c.io.Out, "Added %s to %s\n", printable(m.Address), printable(l.Address))
as := ""
if m.Publisher {
as = " as a publisher"
}
fmt.Fprintf(c.io.Out, "Added %s to %s%s\n", printable(m.Address), printable(l.Address), as)
return 0
}
func (c *cmd) addMember(listID int64, address, name string) (json.RawMessage, error) {
// flagBool returns &v when the flag was given on the command line, nil otherwise.
func flagBool(fs *flag.FlagSet, name string, v bool) *bool {
set := false
fs.Visit(func(f *flag.Flag) { set = set || f.Name == name })
if !set {
return nil
}
return &v
}
// addMember subscribes an address; a nil publisher leaves the flag of an existing member alone.
func (c *cmd) addMember(listID int64, address, name string, publisher *bool) (json.RawMessage, error) {
cl, err := c.client()
if err != nil {
return nil, err
}
return cl.do("POST", fmt.Sprintf("/lists/%d/members", listID), map[string]string{"address": address, "display_name": name})
body := map[string]any{"address": address, "display_name": name}
if publisher != nil {
body["publisher"] = *publisher
}
return cl.do("POST", fmt.Sprintf("/lists/%d/members", listID), body)
}
func (c *cmd) memberSet(args []string) int {
fs := newFlagSet("member set")
pub := fs.Bool("publisher", false, "")
pos, err := parseArgs(fs, args)
if err != nil {
return c.usageError("%v", err)
}
if len(pos) != 2 {
return c.usageError("usage: parsimony member set <list> <email> --publisher=true|false")
}
publisher := flagBool(fs, "publisher", *pub)
if publisher == nil {
return c.usageError("nothing to change: give --publisher=true or --publisher=false")
}
l, err := c.resolveList(pos[0])
if err != nil {
return c.fail(err)
}
cl, _ := c.client()
path := fmt.Sprintf("/lists/%d/members/%s", l.ID, url.PathEscape(pos[1]))
if _, err := cl.do("PATCH", path, map[string]bool{"publisher": *publisher}); err != nil {
return c.fail(err)
}
role := "a subscriber"
if *publisher {
role = "a publisher"
}
return c.done(fmt.Sprintf("%s is now %s of %s", strings.ToLower(pos[1]), role, l.Address))
}
func (c *cmd) memberRm(args []string) int {
@ -325,7 +383,20 @@ func (c *cmd) memberImport(args []string) int {
if len(parts) > 1 {
name = strings.TrimSpace(parts[1])
}
if _, err := c.addMember(l.ID, address, name); err != nil {
// The optional third column is the keyword "publisher"; an empty one is no column.
var publisher *bool
if len(parts) > 2 {
switch kw := strings.TrimSpace(parts[2]); {
case strings.EqualFold(kw, "publisher"):
yes := true
publisher = &yes
case kw != "":
failed++
fmt.Fprintf(c.io.Err, "line %d: %s\n", n, printable(fmt.Sprintf("%s: the third column may only be \"publisher\"", line)))
continue
}
}
if _, err := c.addMember(l.ID, address, name, publisher); err != nil {
failed++
fmt.Fprintf(c.io.Err, "line %d: %s\n", n, printable(fmt.Sprintf("%s: %v", line, err)))
continue

View file

@ -0,0 +1,144 @@
package cli
import (
"strings"
"testing"
)
// isPublisher asks the store whether addr is a publisher of dev@example.org.
func (e *env) isPublisher(t *testing.T, addr string) bool {
t.Helper()
l, err := e.s.FindList("dev@example.org")
if err != nil {
t.Fatal(err)
}
ok, err := e.s.IsPublisher(l.ID, addr)
if err != nil {
t.Fatal(err)
}
return ok
}
func TestMemberAddWithThePublisherFlag(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher")
if !strings.Contains(out, "Added alice@example.com") || !strings.Contains(out, "publisher") {
t.Fatalf("add: %q", out)
}
if !e.isPublisher(t, "alice@example.com") {
t.Fatal("--publisher makes a publisher")
}
// Without the flag an existing member keeps its role.
if out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--name", "Alice"); !strings.Contains(out, "publisher") {
t.Fatalf("re-add: %q", out)
}
if !e.isPublisher(t, "alice@example.com") {
t.Fatal("re-adding without the flag keeps it")
}
if out := e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher=false"); strings.Contains(out, "publisher") {
t.Fatalf("demote: %q", out)
}
if e.isPublisher(t, "alice@example.com") {
t.Fatal("--publisher=false demotes")
}
e.ok(t, "member", "add", "dev@example.org", "bob@example.com")
if e.isPublisher(t, "bob@example.com") {
t.Fatal("a plain add is a subscriber")
}
}
func TestMemberLsShowsTheRole(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
e.ok(t, "member", "add", "dev@example.org", "alice@example.com", "--publisher")
e.ok(t, "member", "add", "dev@example.org", "bob@example.com")
out := e.ok(t, "member", "ls", "dev@example.org")
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 3 || !strings.Contains(lines[0], "ROLE") {
t.Fatalf("ls:\n%s", out)
}
if !strings.Contains(lines[1], "alice@example.com") || !strings.Contains(lines[1], "publisher") {
t.Fatalf("alice:\n%s", out)
}
if !strings.Contains(lines[2], "bob@example.com") || !strings.Contains(lines[2], "subscriber") {
t.Fatalf("bob:\n%s", out)
}
if out := e.ok(t, "--json", "member", "ls", "dev@example.org"); !strings.Contains(out, `"publisher": true`) {
t.Fatalf("json: %s", out)
}
}
func TestMemberSetChangesTheRole(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
e.ok(t, "member", "add", "dev@example.org", "alice@example.com")
out := e.ok(t, "member", "set", "dev@example.org", "Alice@Example.com", "--publisher=true")
if !strings.Contains(out, "alice@example.com") || !strings.Contains(out, "publisher") {
t.Fatalf("set: %q", out)
}
if !e.isPublisher(t, "alice@example.com") {
t.Fatal("promoted")
}
out = e.ok(t, "member", "set", "dev@example.org", "alice@example.com", "--publisher=false")
if !strings.Contains(out, "subscriber") {
t.Fatalf("set false: %q", out)
}
if e.isPublisher(t, "alice@example.com") {
t.Fatal("demoted")
}
e.ok(t, "member", "set", "dev@example.org", "alice@example.com", "--publisher")
if !e.isPublisher(t, "alice@example.com") {
t.Fatal("a bare --publisher means true")
}
e.fails(t, 2, "--publisher", "member", "set", "dev@example.org", "alice@example.com")
e.fails(t, 2, "usage: parsimony member set", "member", "set", "dev@example.org", "--publisher")
e.fails(t, 2, "invalid boolean", "member", "set", "dev@example.org", "alice@example.com", "--publisher=maybe")
e.fails(t, 1, "not found", "member", "set", "dev@example.org", "bob@example.com", "--publisher")
e.fails(t, 1, "no such list", "member", "set", "ghost@example.org", "alice@example.com", "--publisher")
}
func TestMemberImportReadsThePublisherColumn(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
e.ok(t, "member", "add", "dev@example.org", "erin@example.com", "--publisher")
input := "alice@example.com,Alice,publisher\nbob@example.com,, PUBLISHER \ncarol@example.com,Carol,admin\ndave@example.com,Dave,\nerin@example.com,Erin\nfrank@example.com,Frank,publisher,x\n"
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, input, "member", "import", "dev@example.org", "-")
if code != 1 || !strings.Contains(out, "Imported 4") || !strings.Contains(out, "2 failed") {
t.Fatalf("%d %q %q", code, out, errs)
}
if !strings.Contains(errs, "line 3") || !strings.Contains(errs, "admin") || !strings.Contains(errs, "line 6") {
t.Fatalf("the bad third columns are reported: %q", errs)
}
for addr, want := range map[string]bool{"alice@example.com": true, "bob@example.com": true, "dave@example.com": false, "erin@example.com": true} {
if got := e.isPublisher(t, addr); got != want {
t.Errorf("%s publisher = %v, want %v", addr, got, want)
}
}
l, _ := e.s.FindList("dev@example.org")
for _, addr := range []string{"carol@example.com", "frank@example.com"} {
if ok, _ := e.s.IsMember(l.ID, addr); ok {
t.Errorf("%s: a line with a bad third column imports nothing", addr)
}
}
}
func TestListSetPostPublishers(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
e.ok(t, "list", "set", "dev@example.org", "--post", "publishers")
if out := e.ok(t, "list", "ls"); !strings.Contains(out, "publishers") {
t.Fatalf("ls: %s", out)
}
e.fails(t, 1, "members, moderated or publishers", "list", "set", "dev@example.org", "--post", "bogus")
}
func TestHelpMentionsPublishers(t *testing.T) {
e := newEnv(t)
out := e.ok(t, "help")
for _, want := range []string{"--post members|moderated|publishers", "member add <list> <email> [--name N] [--publisher]", "member set <list> <email> --publisher=true|false", "publisher"} {
if !strings.Contains(out, want) {
t.Errorf("help lacks %q", want)
}
}
}