oficina / base Public

Retry invitations whose email failed

Record invite_sent_at (migration 0002) once an invitation is delivered.
Inviting again sends it if none has been delivered and the member has not
signed in. A failed send redirects to /people?problem=mail with a message.
Also tests the invalid-address redirect and splits the role-tag copy rows.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev cb5f4513aa229d2aeee7ea0cdd1bf2f686167411 parent 9a71f3a
7 files changed, +97 −11
  • docs/copy-pending.md +4 −1
  • internal/store/members.go +14 −3
  • internal/store/migrations/0002_invite_sent.sql +1 −0
  • internal/web/people.go +15 −7
  • internal/web/templates/people.html +1 −0
  • internal/webtest/harness_test.go +5 −0
  • internal/webtest/people_test.go +57 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md
index b565cfa..f08a51c 100644
--- a/docs/copy-pending.md
+++ b/docs/copy-pending.md
@@ -49,7 +49,10 @@ People and invitations:
| internal/web/templates/people.html | Invite field, placeholder (from the mockup) | maria@acme.co |
| internal/web/templates/people.html | Invite button (from the mockup) | Send invite |
| internal/web/templates/people.html | Person not yet signed in | Invited. Hasn't signed in yet. |
-| internal/web/templates/people.html | Role tags | Owner / Member / Invited |
+| internal/web/templates/people.html | Role tag, owner | Owner |
+| internal/web/templates/people.html | Role tag, signed-in member | Member |
+| internal/web/templates/people.html | Role tag, not yet signed in | Invited |
+| internal/web/templates/people.html | Invite email could not be sent | The invitation wasn't sent. Try inviting them again. |
| internal/web/templates/people.html | Invite refused, bad address | That doesn't look like an email address. |
| internal/web/people.go | Invitation email, subject | You're invited to {Base name} |
| internal/web/people.go | Invitation email, body | You've been invited to {Base name}'s Base, where the team's apps live.<br><br>Sign in with this email address to get started:<br>{Base origin}/signin |
diff --git a/internal/store/members.go b/internal/store/members.go
index 06b38aa..0840a69 100644
--- a/internal/store/members.go
+++ b/internal/store/members.go
@@ -17,22 +17,27 @@ type Member struct {
Role string // "owner" or "member"
InvitedAt time.Time
JoinedAt *time.Time
+ InviteSentAt *time.Time // nil until an invitation email was delivered
}
-const memberCols = `id, email, name, role, invited_at, joined_at`
+const memberCols = `id, email, name, role, invited_at, joined_at, invite_sent_at`
type scanner interface{ Scan(...any) error }
func scanMember(s scanner) (Member, error) {
var m Member
- var joined sql.NullTime
- if err := s.Scan(&m.ID, &m.Email, &m.Name, &m.Role, &m.InvitedAt, &joined); err != nil {
+ var joined, sent sql.NullTime
+ if err := s.Scan(&m.ID, &m.Email, &m.Name, &m.Role, &m.InvitedAt, &joined, &sent); err != nil {
return Member{}, err
}
if joined.Valid {
t := joined.Time
m.JoinedAt = &t
}
+ if sent.Valid {
+ t := sent.Time
+ m.InviteSentAt = &t
+ }
return m, nil
}
@@ -95,3 +100,9 @@ func MarkJoined(ctx context.Context, db *sql.DB, id string) error {
_, err := db.ExecContext(ctx, `UPDATE members SET joined_at = ? WHERE id = ? AND joined_at IS NULL`, time.Now().UTC(), id)
return err
}
+
+// MarkInviteSent records that the member's invitation email was delivered.
+func MarkInviteSent(ctx context.Context, db *sql.DB, id string) error {
+ _, err := db.ExecContext(ctx, `UPDATE members SET invite_sent_at = ? WHERE id = ?`, time.Now().UTC(), id)
+ return err
+}
diff --git a/internal/store/migrations/0002_invite_sent.sql b/internal/store/migrations/0002_invite_sent.sql
new file mode 100644
index 0000000..f214f77
--- /dev/null
+++ b/internal/store/migrations/0002_invite_sent.sql
@@ -0,0 +1 @@
+ALTER TABLE members ADD COLUMN invite_sent_at timestamp;
diff --git a/internal/web/people.go b/internal/web/people.go
index b970bbc..6aaecd7 100644
--- a/internal/web/people.go
+++ b/internal/web/people.go
@@ -20,10 +20,11 @@ type personRow struct {
}
type peoplePage struct {
- Name string
- IsOwner bool
- BadEmail bool
- People []personRow
+ Name string
+ IsOwner bool
+ BadEmail bool
+ MailFailed bool
+ People []personRow
}
func (a *app) people(w http.ResponseWriter, r *http.Request) {
@@ -34,7 +35,7 @@ func (a *app) people(w http.ResponseWriter, r *http.Request) {
http.Error(w, "unavailable", http.StatusServiceUnavailable)
return
}
- page := peoplePage{Name: a.cfg.Name, IsOwner: me.Role == "owner", BadEmail: r.URL.Query().Get("problem") == "email"}
+ page := peoplePage{Name: a.cfg.Name, IsOwner: me.Role == "owner", BadEmail: r.URL.Query().Get("problem") == "email", MailFailed: r.URL.Query().Get("problem") == "mail"}
for _, m := range ms {
label := m.Name
if label == "" {
@@ -64,18 +65,25 @@ func (a *app) invite(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/people?problem=email", http.StatusSeeOther)
return
}
- _, created, err := store.Invite(r.Context(), a.db, email)
+ m, _, err := store.Invite(r.Context(), a.db, email)
if err != nil {
a.logger.Error("invite", "err", err)
http.Error(w, "unavailable", http.StatusServiceUnavailable)
return
}
- if created {
+ // Send until one invitation has been delivered, so a failed send can be
+ // retried by inviting again.
+ if m.Role == "member" && m.JoinedAt == nil && m.InviteSentAt == nil {
ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), 20*time.Second)
defer cancel()
subject, body := invitationEmail(a.cfg.Name, a.cfg.Origin)
if err := a.mailer.Send(ctx, email, subject, body); err != nil {
a.logger.Error("invite: send", "err", err)
+ http.Redirect(w, r, "/people?problem=mail", http.StatusSeeOther)
+ return
+ }
+ if err := store.MarkInviteSent(ctx, a.db, m.ID); err != nil {
+ a.logger.Error("invite: mark sent", "err", err)
}
}
http.Redirect(w, r, "/people", http.StatusSeeOther)
diff --git a/internal/web/templates/people.html b/internal/web/templates/people.html
index 3adf53c..da36325 100644
--- a/internal/web/templates/people.html
+++ b/internal/web/templates/people.html
@@ -5,6 +5,7 @@
<h2>People</h2>
<p class="lead">Everyone here can sign in to every app on {{.Name}}'s Base. Each app decides what they can do in it.</p>
{{if .BadEmail}}<p role="status">That doesn't look like an email address.</p>
+{{end}}{{if .MailFailed}}<p role="status">The invitation wasn't sent. Try inviting them again.</p>
{{end}}{{if .IsOwner}}<form class="invite" method="post" action="/people">
<input class="input" type="email" name="email" placeholder="maria@acme.co" autocomplete="off" required aria-label="Teammate's email address">
<button class="btn" type="submit">Send invite</button>
diff --git a/internal/webtest/harness_test.go b/internal/webtest/harness_test.go
index 299e5c2..716fab3 100644
--- a/internal/webtest/harness_test.go
+++ b/internal/webtest/harness_test.go
@@ -5,6 +5,7 @@ package webtest
import (
"context"
"database/sql"
+ "errors"
"html"
"io"
"log/slog"
@@ -49,11 +50,15 @@ type Mail struct{ To, Subject, Body string }
type capturingMailer struct {
mu sync.Mutex
sent []Mail
+ fail bool // when set, Send returns an error and records nothing
}
func (c *capturingMailer) Send(_ context.Context, to, subject, body string) error {
c.mu.Lock()
defer c.mu.Unlock()
+ if c.fail {
+ return errors.New("smtp down")
+ }
c.sent = append(c.sent, Mail{to, subject, body})
return nil
}
diff --git a/internal/webtest/people_test.go b/internal/webtest/people_test.go
index 41ee5de..5eead49 100644
--- a/internal/webtest/people_test.go
+++ b/internal/webtest/people_test.go
@@ -96,3 +96,60 @@ func TestPeopleWorksWithoutScripts(t *testing.T) {
t.Error("page needs no scripts")
}
}
+
+func TestFailedInvitationCanBeRetried(t *testing.T) {
+ a := newApp(t)
+ a.signedInOwner()
+ n := len(a.mailbox())
+
+ a.mail.mu.Lock()
+ a.mail.fail = true
+ a.mail.mu.Unlock()
+ r := a.post("/people", invite("maria@acme.co"))
+ if r.Code != 303 || r.Location() != "/people?problem=mail" {
+ t.Fatalf("failed send: %d %q", r.Code, r.Location())
+ }
+ if page := a.get("/people?problem=mail"); !strings.Contains(page.Body, "The invitation wasn") {
+ t.Errorf("owner is not told the invitation failed:\n%s", page.Body)
+ }
+ m, ok, err := store.MemberByEmail(context.Background(), a.DB, "maria@acme.co")
+ if err != nil || !ok || m.InviteSentAt != nil {
+ t.Fatalf("want member with no sent invite, got %+v ok=%v err=%v", m, ok, err)
+ }
+
+ a.mail.mu.Lock()
+ a.mail.fail = false
+ a.mail.mu.Unlock()
+ if r := a.post("/people", invite("maria@acme.co")); r.Location() != "/people" {
+ t.Errorf("retry: %d %q", r.Code, r.Location())
+ }
+ if got := len(a.mailbox()); got != n+1 {
+ t.Fatalf("retry should send exactly one invitation, mailbox grew by %d", got-n)
+ }
+ if m, _, _ := store.MemberByEmail(context.Background(), a.DB, "maria@acme.co"); m.InviteSentAt == nil {
+ t.Error("delivery was not recorded")
+ }
+ a.post("/people", invite("maria@acme.co"))
+ if got := len(a.mailbox()); got != n+1 {
+ t.Errorf("third invite sent another email (grew by %d)", got-n)
+ }
+}
+
+func TestInvalidAddressIsRefused(t *testing.T) {
+ a := newApp(t)
+ a.signedInOwner()
+ n := len(a.mailbox())
+ r := a.post("/people", invite("not an email"))
+ if r.Code != 303 || r.Location() != "/people?problem=email" {
+ t.Fatalf("invalid: %d %q", r.Code, r.Location())
+ }
+ if page := a.get(r.Location()); !strings.Contains(page.Body, "look like an email address") {
+ t.Errorf("no visible error:\n%s", page.Body)
+ }
+ if ms, _ := store.Members(context.Background(), a.DB); len(ms) != 1 {
+ t.Errorf("want only the owner, got %d members", len(ms))
+ }
+ if len(a.mailbox()) != n {
+ t.Error("an email was sent")
+ }
+}