carlos / platform Public

console: an account's domains, bought and registered elsewhere

accountDomains and accountDomain assemble one card per registrable
domain: the account's registrations and live purchases, plus its
custom-domain claims on its apps (live or in Trash) from one claims
snapshot, each with the state the app's Domains tab shows (one
settingsDomains call per app holding addresses). An unread claim that
could be the account's fails the call rather than shorten it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 4e7c3f32229e677873e41d11e49e16bce7b87a82 parent 4b5ee06
2 files changed, +528 −0
  • internal/console/accountdomains.go +233 −0
  • internal/console/accountdomains_test.go +295 −0

CI failed — run details

diff --git a/internal/console/accountdomains.go b/internal/console/accountdomains.go
new file mode 100644
index 000000000..d0a893a17
--- /dev/null
+++ b/internal/console/accountdomains.go
@@ -0,0 +1,233 @@
+package console
+
+import (
+ "context"
+ "fmt"
+ "sort"
+ "strings"
+
+ "golang.org/x/net/publicsuffix"
+
+ "github.com/carlosframework/platform/internal/domains"
+)
+
+// domainAddress is one address under an account's domain: a custom-domain
+// claim of the account's, on one of its apps.
+type domainAddress struct {
+ Host, App string
+ AppInTrash bool
+ State string // the attach state (live, pending-dns, ...), as the app's Domains tab shows it
+}
+
+// domainCard is one registrable domain the account uses (spec §11.3): bought
+// here (Registration set), being bought (Purchase set), or registered
+// elsewhere and attached to its apps (both nil).
+type domainCard struct {
+ Domain string // registrable domain (publicsuffix.EffectiveTLDPlusOne), A-label
+ Registration *DomainRegistration // nil: registered elsewhere
+ Purchase *DomainPurchase // a non-terminal purchase for this domain, if any
+ Addresses []domainAddress // sorted: apex, www, then by host
+}
+
+// accountDomains is every domain the account uses, sorted by Domain: the
+// Domains page's cards and the CLI list. Nothing new is stored for a domain
+// registered elsewhere; its card is its addresses, grouped.
+func (s *Server) accountDomains(ctx context.Context, acct *Account) ([]domainCard, error) {
+ return s.assembleDomainCards(ctx, acct, "")
+}
+
+// accountDomain is one card of accountDomains; nil, nil when the account
+// has no such card. It reads the attach state only for the apps holding
+// that domain's addresses.
+func (s *Server) accountDomain(ctx context.Context, acct *Account, domain string) (*domainCard, error) {
+ domain = strings.TrimSuffix(strings.ToLower(strings.TrimSpace(domain)), ".")
+ if domain == "" {
+ return nil, nil
+ }
+ cards, err := s.assembleDomainCards(ctx, acct, domain)
+ if err != nil || len(cards) == 0 {
+ return nil, err
+ }
+ return &cards[0], nil
+}
+
+// domainCardKey is the card a host belongs to: the registrable domain of
+// the host without a leading "*.". A host that is itself a public suffix
+// (no attach accepts one) is its own card rather than lost.
+func domainCardKey(host string) string {
+ h := strings.TrimPrefix(strings.ToLower(host), "*.")
+ if d, err := publicsuffix.EffectiveTLDPlusOne(h); err == nil {
+ return d
+ }
+ return h
+}
+
+// assembleDomainCards builds the cards, all of them when only is "", else
+// at most the one for only.
+func (s *Server) assembleDomainCards(ctx context.Context, acct *Account, only string) ([]domainCard, error) {
+ byDomain := map[string]*domainCard{}
+ card := func(d string) *domainCard {
+ c, ok := byDomain[d]
+ if !ok {
+ c = &domainCard{Domain: d}
+ byDomain[d] = c
+ }
+ return c
+ }
+ if err := s.domainCardRecords(ctx, acct, only, card); err != nil {
+ return nil, err
+ }
+ if err := s.domainCardAddresses(ctx, acct, only, card); err != nil {
+ return nil, err
+ }
+ out := make([]domainCard, 0, len(byDomain))
+ for _, c := range byDomain {
+ sortDomainAddresses(c.Domain, c.Addresses)
+ out = append(out, *c)
+ }
+ sort.Slice(out, func(i, j int) bool { return out[i].Domain < out[j].Domain })
+ return out, nil
+}
+
+// domainCardRecords adds the account's registrations and live purchases.
+// AccountDomainPurchases is newest first, so a card keeps the newest.
+func (s *Server) domainCardRecords(ctx context.Context, acct *Account, only string, card func(string) *domainCard) error {
+ if only != "" {
+ r, err := s.store.DomainRegistrationFor(ctx, acct.ID, only)
+ if err != nil {
+ return fmt.Errorf("domain registration %s: %w", only, err)
+ }
+ if r != nil {
+ card(only).Registration = r
+ }
+ } else {
+ regs, err := s.store.DomainRegistrations(ctx, acct.ID)
+ if err != nil {
+ return fmt.Errorf("domain registrations: %w", err)
+ }
+ for i := range regs {
+ card(strings.ToLower(regs[i].Domain)).Registration = &regs[i]
+ }
+ }
+ purchases, err := s.store.AccountDomainPurchases(ctx, acct.ID)
+ if err != nil {
+ return fmt.Errorf("domain purchases: %w", err)
+ }
+ for i := range purchases {
+ d := strings.ToLower(purchases[i].Domain)
+ if only != "" && d != only {
+ continue
+ }
+ if c := card(d); c.Purchase == nil {
+ c.Purchase = &purchases[i]
+ }
+ }
+ return nil
+}
+
+// domainCardAddresses adds the account's custom-domain claims on its apps,
+// live or in Trash, from one claims snapshot, and fills each address's
+// state from one settingsDomains call per app holding addresses.
+func (s *Server) domainCardAddresses(ctx context.Context, acct *Account, only string, card func(string) *domainCard) error {
+ if s.cfg.Releases == nil {
+ return nil // no bucket, no claims: the Settings page shows none either
+ }
+ claims, unread, err := s.claims.ListWithUnread(ctx, s.cfg.Releases)
+ if err != nil {
+ return fmt.Errorf("domain claims: %w", err)
+ }
+ if err := s.domainCardsUnread(acct, only, unread); err != nil {
+ return err
+ }
+ // AppsIncludingTrashIn is the membership, in one read; AppsInContext
+ // only marks which are live, so an app trashed or restored between the
+ // two reads is shown under the wrong flag for one render, never dropped.
+ all, err := s.store.AppsIncludingTrashIn(acct.ID)
+ if err != nil {
+ return fmt.Errorf("apps: %w", err)
+ }
+ liveApps, err := s.store.AppsInContext(ctx, acct.ID)
+ if err != nil {
+ return fmt.Errorf("apps: %w", err)
+ }
+ retained := make(map[string]bool, len(all))
+ for _, a := range all {
+ retained[a] = true
+ }
+ live := make(map[string]bool, len(liveApps))
+ for _, a := range liveApps {
+ live[a] = true
+ }
+ type slot struct {
+ card *domainCard
+ i int
+ }
+ byApp := map[string][]slot{}
+ for _, c := range claims {
+ host := strings.ToLower(c.Host)
+ if c.Account != acct.Sqid || !retained[c.App] || !s.customDomain(strings.TrimPrefix(host, "*."), acct.Sqid) {
+ continue
+ }
+ d := domainCardKey(host)
+ if only != "" && d != only {
+ continue
+ }
+ cd := card(d)
+ cd.Addresses = append(cd.Addresses, domainAddress{Host: host, App: c.App, AppInTrash: !live[c.App]})
+ byApp[c.App] = append(byApp[c.App], slot{cd, len(cd.Addresses) - 1})
+ }
+ for app, rows := range byApp {
+ // nil routed: the state of a claim-backed row does not depend on
+ // the app's registry routes, and the routes cost a full app view.
+ states := map[string]string{}
+ for _, dv := range s.settingsDomains(ctx, acct, app, nil) {
+ states[strings.ToLower(dv.Domain)] = dv.State
+ }
+ for _, r := range rows {
+ a := &r.card.Addresses[r.i]
+ a.State = states[a.Host]
+ }
+ }
+ return nil
+}
+
+// domainCardsUnread fails the cards when a claim that could be one of
+// their addresses could not be read: one an earlier read knew was this
+// account's, or one of unknown account, on a custom domain (and under only,
+// when set). Unknown is not gone; another account's claim is never ours.
+func (s *Server) domainCardsUnread(acct *Account, only string, unread []domains.UnreadClaim) error {
+ for _, u := range unread {
+ host := strings.ToLower(u.Host)
+ if u.Account != "" && u.Account != acct.Sqid {
+ continue
+ }
+ if !s.customDomain(strings.TrimPrefix(host, "*."), acct.Sqid) {
+ continue
+ }
+ if only != "" && domainCardKey(host) != only {
+ continue
+ }
+ return fmt.Errorf("domain claim %s could not be read", host)
+ }
+ return nil
+}
+
+// sortDomainAddresses orders a card's rows: the apex, www, then by host.
+func sortDomainAddresses(domain string, rows []domainAddress) {
+ rank := func(h string) int {
+ switch h {
+ case domain:
+ return 0
+ case "www." + domain:
+ return 1
+ }
+ return 2
+ }
+ sort.Slice(rows, func(i, j int) bool {
+ ri, rj := rank(rows[i].Host), rank(rows[j].Host)
+ if ri != rj {
+ return ri < rj
+ }
+ return rows[i].Host < rows[j].Host
+ })
+}
diff --git a/internal/console/accountdomains_test.go b/internal/console/accountdomains_test.go
new file mode 100644
index 000000000..58bddcb65
--- /dev/null
+++ b/internal/console/accountdomains_test.go
@@ -0,0 +1,295 @@
+package console
+
+import (
+ "context"
+ "encoding/json"
+ "slices"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/domains"
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/status"
+)
+
+// adFixture is a console with the apps domain set and one fresh account.
+func adFixture(t *testing.T, o consoleFixtureOptions) (*consoleFixture, *Account) {
+ t.Helper()
+ fx := newConsoleFixtureWith(t, o)
+ fx.srv.cfg.AppsDomain = "oncarlos.test"
+ return fx, planAccount(t, fx, 501)
+}
+
+func adApp(t *testing.T, fx *consoleFixture, acct *Account, app string) {
+ t.Helper()
+ if _, err := fx.st.CreateAppIn(acct.ID, app, "owner@example.com", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func adClaim(t *testing.T, fx *consoleFixture, acct *Account, app, host string) {
+ t.Helper()
+ b, err := json.Marshal(domains.Claim{Schema: 1, Host: host, Account: acct.Sqid, App: app, Route: "r." + app,
+ CreatedAt: time.Now().UTC().Format(time.RFC3339)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fx.rel.Put(context.Background(), domains.ClaimKey(host), b, ""); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func adHosts(c domainCard) []string {
+ var out []string
+ for _, a := range c.Addresses {
+ out = append(out, a.Host)
+ }
+ return out
+}
+
+func adDomains(cards []domainCard) []string {
+ var out []string
+ for _, c := range cards {
+ out = append(out, c.Domain)
+ }
+ return out
+}
+
+// Review Focus 4: one card per registrable domain, from this account's
+// custom-domain claims on its apps (live or in Trash), with each address's
+// app, Trash flag and the state the app's own Domains tab shows.
+func TestAccountDomainsGroupsByRegistrableDomain(t *testing.T) {
+ fx, acct := adFixture(t, consoleFixtureOptions{})
+ ctx := context.Background()
+ adApp(t, fx, acct, "a")
+ adApp(t, fx, acct, "b")
+ other := planAccount(t, fx, 502)
+ adApp(t, fx, other, "a")
+ adClaim(t, fx, acct, "a", "shop.example")
+ adClaim(t, fx, acct, "a", "www.shop.example")
+ adClaim(t, fx, acct, "b", "docs.shop.example")
+ adClaim(t, fx, acct, "a", "kass.oncarlos.test")
+ adClaim(t, fx, acct, "a", "shop.example.co.uk")
+ adClaim(t, fx, other, "a", "other.example")
+ if err := fx.st.TrashAppRowContext(ctx, acct.ID, "b", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ // A reading for one host, so the rows do not all share one state.
+ now := time.Now()
+ if err := status.WriteBox(ctx, fx.rel, status.Box{
+ GeneratedAt: now, Label: "ada-reporting", Region: "eu-west-1",
+ Metrics: &status.BoxMetrics{Aliases: map[string]status.AliasReading{
+ "www.shop.example": {Route: "r.a", DNSStatus: "elsewhere", DNSCheckedAt: now.Unix()},
+ }},
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ cards, err := fx.srv.accountDomains(ctx, acct)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got, want := adDomains(cards), []string{"example.co.uk", "shop.example"}; !slices.Equal(got, want) {
+ t.Fatalf("cards = %v, want %v", got, want)
+ }
+ uk, shop := cards[0], cards[1]
+ if uk.Registration != nil || uk.Purchase != nil || shop.Registration != nil || shop.Purchase != nil {
+ t.Fatalf("registered elsewhere cards carry a registration or purchase: %+v %+v", uk, shop)
+ }
+ if got, want := adHosts(shop), []string{"shop.example", "www.shop.example", "docs.shop.example"}; !slices.Equal(got, want) {
+ t.Fatalf("shop.example rows = %v, want %v", got, want)
+ }
+ if got, want := adHosts(uk), []string{"shop.example.co.uk"}; !slices.Equal(got, want) {
+ t.Fatalf("example.co.uk rows = %v, want %v", got, want)
+ }
+ wantApp := map[string]struct {
+ app string
+ trash bool
+ }{
+ "shop.example": {"a", false}, "www.shop.example": {"a", false},
+ "docs.shop.example": {"b", true}, "shop.example.co.uk": {"a", false},
+ }
+ states := map[string]string{}
+ for _, app := range []string{"a", "b"} {
+ for _, dv := range fx.srv.settingsDomains(ctx, acct, app, nil) {
+ states[dv.Domain] = dv.State
+ }
+ }
+ distinct := map[string]bool{}
+ for _, c := range cards {
+ for _, a := range c.Addresses {
+ w := wantApp[a.Host]
+ if a.App != w.app || a.AppInTrash != w.trash {
+ t.Errorf("%s: app %q trash %v, want %q %v", a.Host, a.App, a.AppInTrash, w.app, w.trash)
+ }
+ if a.State == "" || a.State != states[a.Host] {
+ t.Errorf("%s: state %q, want the Domains tab's %q", a.Host, a.State, states[a.Host])
+ }
+ distinct[a.State] = true
+ }
+ }
+ if len(distinct) < 2 {
+ t.Errorf("every row has one state %v; the reading for www.shop.example should differ", distinct)
+ }
+}
+
+// A domain bought here with nothing attached still has its card.
+func TestAccountDomainsBoughtWithoutAddresses(t *testing.T) {
+ fx, acct := adFixture(t, consoleFixtureOptions{})
+ ctx := context.Background()
+ other := planAccount(t, fx, 502)
+ if err := fx.st.AddDomainRegistration(DomainRegistration{Domain: "bought.example", AccountID: acct.ID, ZoneID: "z1"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.st.AddDomainRegistration(DomainRegistration{Domain: "theirs.example", AccountID: other.ID, ZoneID: "z2"}); err != nil {
+ t.Fatal(err)
+ }
+ cards, err := fx.srv.accountDomains(ctx, acct)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(cards) != 1 || cards[0].Domain != "bought.example" {
+ t.Fatalf("cards = %v, want bought.example only", adDomains(cards))
+ }
+ if cards[0].Registration == nil || cards[0].Registration.ZoneID != "z1" || len(cards[0].Addresses) != 0 {
+ t.Fatalf("card = %+v, want the registration and no addresses", cards[0])
+ }
+ got, err := fx.srv.accountDomain(ctx, acct, "bought.example")
+ if err != nil || got == nil || got.Registration == nil {
+ t.Fatalf("accountDomain = %+v, %v", got, err)
+ }
+}
+
+// A purchase still in flight is a card, including one finishing unpointed;
+// a finished or failed purchase is not.
+func TestAccountDomainsShowsLivePurchase(t *testing.T) {
+ fx, acct := adFixture(t, consoleFixtureOptions{})
+ ctx := context.Background()
+ adApp(t, fx, acct, "a")
+ adClaim(t, fx, acct, "a", "www.paid.example")
+ for _, p := range []DomainPurchase{
+ {AccountID: acct.ID, Domain: "new.example", Currency: "eur", Amount: 1200},
+ {AccountID: acct.ID, Domain: "paid.example", App: "a", Currency: "eur", Amount: 1200, State: PurchaseRegistering},
+ {AccountID: acct.ID, Domain: "old.example", Currency: "eur", Amount: 1200, State: PurchaseDone},
+ {AccountID: acct.ID, Domain: "lost.example", Currency: "eur", Amount: 1200, State: PurchaseFailed},
+ } {
+ if err := fx.st.CreateDomainPurchase(p); err != nil {
+ t.Fatal(err)
+ }
+ }
+ gone := DomainPurchase{AccountID: acct.ID, Domain: "gone.example", App: "z", Currency: "eur", Amount: 1200, State: PurchaseCaptured}
+ gone.Unpointing = true
+ if err := fx.st.CreateDomainPurchase(gone); err != nil {
+ t.Fatal(err)
+ }
+ cards, err := fx.srv.accountDomains(ctx, acct)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got, want := adDomains(cards), []string{"gone.example", "new.example", "paid.example"}; !slices.Equal(got, want) {
+ t.Fatalf("cards = %v, want %v", got, want)
+ }
+ for _, c := range cards {
+ if c.Purchase == nil || c.Purchase.Domain != c.Domain || c.Registration != nil {
+ t.Errorf("%s: purchase %+v registration %+v", c.Domain, c.Purchase, c.Registration)
+ }
+ }
+ if !cards[0].Purchase.Unpointing || len(cards[0].Addresses) != 0 {
+ t.Errorf("gone.example = %+v, want the unpointing purchase and no addresses", cards[0])
+ }
+ if got := adHosts(cards[2]); !slices.Equal(got, []string{"www.paid.example"}) {
+ t.Errorf("paid.example rows = %v", got)
+ }
+}
+
+// An unreadable claim of this account (or of no known account) is not a
+// missing address: the list fails rather than come back short.
+func TestAccountDomainsUnreadClaimsIsError(t *testing.T) {
+ fx, acct := adFixture(t, consoleFixtureOptions{})
+ ctx := context.Background()
+ adApp(t, fx, acct, "a")
+ adClaim(t, fx, acct, "a", "shop.example")
+ adClaim(t, fx, acct, "a", "docs.other.example")
+ if _, err := fx.srv.accountDomains(ctx, acct); err != nil {
+ t.Fatalf("fixture: %v", err)
+ }
+ _, etag, err := fx.rel.Get(ctx, domains.ClaimKey("docs.other.example"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fx.rel.Put(ctx, domains.ClaimKey("docs.other.example"), []byte(`{not json`), etag); err != nil {
+ t.Fatal(err)
+ }
+ if cards, err := fx.srv.accountDomains(ctx, acct); err == nil {
+ t.Fatalf("an unreadable claim gave cards %v", adDomains(cards))
+ }
+ if card, err := fx.srv.accountDomain(ctx, acct, "other.example"); err == nil {
+ t.Fatalf("an unreadable claim gave card %+v", card)
+ }
+ // A card the unreadable claim cannot belong to is still readable.
+ if card, err := fx.srv.accountDomain(ctx, acct, "shop.example"); err != nil || card == nil {
+ t.Fatalf("shop.example = %+v, %v", card, err)
+ }
+}
+
+// Another account's domain, claimed or bought, is no card of this one.
+func TestAccountDomainOtherAccountIsNil(t *testing.T) {
+ fx, acct := adFixture(t, consoleFixtureOptions{})
+ ctx := context.Background()
+ other := planAccount(t, fx, 502)
+ adApp(t, fx, other, "a")
+ adApp(t, fx, acct, "a")
+ adClaim(t, fx, other, "a", "other.example")
+ if err := fx.st.AddDomainRegistration(DomainRegistration{Domain: "theirs.example", AccountID: other.ID}); err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.st.CreateDomainPurchase(DomainPurchase{AccountID: other.ID, Domain: "buying.example", Currency: "eur", Amount: 1}); err != nil {
+ t.Fatal(err)
+ }
+ for _, d := range []string{"other.example", "theirs.example", "buying.example", "nothing.example"} {
+ card, err := fx.srv.accountDomain(ctx, acct, d)
+ if err != nil || card != nil {
+ t.Errorf("accountDomain(%s) = %+v, %v; want nil, nil", d, card, err)
+ }
+ }
+ // And the owner sees it.
+ card, err := fx.srv.accountDomain(ctx, other, "other.example")
+ if err != nil || card == nil || !slices.Equal(adHosts(*card), []string{"other.example"}) {
+ t.Fatalf("owner's card = %+v, %v", card, err)
+ }
+}
+
+// r2 5: one claims snapshot, plus one per settingsDomains call, which is
+// one per app holding addresses, never one per address.
+func TestAccountDomainsClaimsListBudget(t *testing.T) {
+ var counter *claimListCounter
+ fx, acct := adFixture(t, consoleFixtureOptions{WrapReleases: func(s release.Store) release.Store {
+ counter = &claimListCounter{Store: s}
+ return counter
+ }})
+ ctx := context.Background()
+ for _, app := range []string{"a", "b", "c"} {
+ adApp(t, fx, acct, app)
+ }
+ adClaim(t, fx, acct, "a", "shop.example")
+ adClaim(t, fx, acct, "a", "www.shop.example")
+ adClaim(t, fx, acct, "a", "docs.shop.example")
+ adClaim(t, fx, acct, "b", "blog.example")
+ adClaim(t, fx, acct, "b", "www.blog.example")
+ // App c holds no address and costs nothing.
+
+ counter.lists = 0
+ if _, err := fx.srv.accountDomains(ctx, acct); err != nil {
+ t.Fatal(err)
+ }
+ if counter.lists != 1+2 {
+ t.Errorf("accountDomains listed claims %d times, want 1 + 2 apps", counter.lists)
+ }
+ counter.lists = 0
+ if _, err := fx.srv.accountDomain(ctx, acct, "blog.example"); err != nil {
+ t.Fatal(err)
+ }
+ if counter.lists != 1+1 {
+ t.Errorf("accountDomain listed claims %d times, want 1 + 1 app", counter.lists)
+ }
+}