From 8edae3ae56f0197d10715b62e5f6b1d08b5bd9ce Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Tue, 17 Nov 2026 20:02:51 +0100 Subject: [PATCH] Add owner inbox UI with address release Web: "My listings" (listings, requests of a listing, conversation) in three panes on wide screens and one at a time on phones, and a sheet or dialog to release the exact address. Sign-in uses a link printed by `node run` (token in the URL fragment, kept in sessionStorage only). API: GET /api/v1/mine (own listings with request counts and whether an address is saved); inbox rows gain last_dir, needs_reply, preview, effort and grant_until, and can be filtered by listing. Docs: PROTOCOL.md and README describe the owner UI and login. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 +- cmd/node/login_test.go | 17 + cmd/node/run.go | 21 + docs/PROTOCOL.md | 23 +- internal/api/api.go | 1 + internal/api/messages.go | 137 +++++- internal/api/messages_test.go | 76 ++++ web/dist/app.js | 781 ++++++++++++++++++++++++++++++++-- web/dist/style.css | 89 +++- web/src/format.ts | 9 + web/src/i18n.ts | 146 +++++++ web/src/main.ts | 25 +- web/src/owner.ts | 518 ++++++++++++++++++++++ web/src/ownerApi.ts | 173 ++++++++ web/static/style.css | 89 +++- web/test/helpers.ts | 24 ++ web/test/owner.test.ts | 316 ++++++++++++++ 17 files changed, 2400 insertions(+), 49 deletions(-) create mode 100644 cmd/node/login_test.go create mode 100644 web/src/owner.ts create mode 100644 web/src/ownerApi.ts create mode 100644 web/test/owner.test.ts diff --git a/README.md b/README.md index 0ca78ef..a74c824 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ pre-built and embedded; Node.js is only needed to change it Buyers can write to an owner straight from a listing, without an account. Messages are end-to-end encrypted in the browser and relayed through the network, so a node only ever sees ciphertext. The owner reads and answers -through the token-protected API (`GET /api/v1/inbox`, see the protocol doc) and -can release the exact address as a signed message; an owner UI is not built yet. +in the browser (`node run` prints a sign-in link; open it to get "My listings") +and can release the exact address as a signed message. A second node on the same network finds this one automatically (mDNS); across networks, pass `--bootstrap /ip4//tcp//p2p/`. diff --git a/cmd/node/login_test.go b/cmd/node/login_test.go new file mode 100644 index 0000000..263faf0 --- /dev/null +++ b/cmd/node/login_test.go @@ -0,0 +1,17 @@ +package main + +import "testing" + +func TestLoginURL(t *testing.T) { + for addr, want := range map[string]string{ + "127.0.0.1:8080": "http://127.0.0.1:8080/#/login/tok", + "0.0.0.0:8080": "http://127.0.0.1:8080/#/login/tok", // a wildcard cannot be opened + "[::]:9000": "http://127.0.0.1:9000/#/login/tok", + "[::1]:8080": "http://[::1]:8080/#/login/tok", + "192.0.2.7:80": "http://192.0.2.7:80/#/login/tok", + } { + if got := loginURL(addr, "tok"); got != want { + t.Errorf("loginURL(%q) = %q, want %q", addr, got, want) + } + } +} diff --git a/cmd/node/run.go b/cmd/node/run.go index 28b2ce8..0eba632 100644 --- a/cmd/node/run.go +++ b/cmd/node/run.go @@ -136,6 +136,13 @@ func cmdRun(args []string) error { log.Warn("the HTTP API is reachable from other machines; put it behind TLS if it is public") } + if token != "" { + // Printed to the terminal, not logged: the token must not end up in a + // log file. It sits in the URL fragment, which a browser never sends to + // a server; the page moves it into sessionStorage and removes it. + fmt.Fprintf(os.Stderr, "\nOwner login (answer your inbox in the browser):\n %s\n\n", loginURL(ln.Addr().String(), token)) + } + go announce(ctx, log, node, st, dev.Cert.Author) <-ctx.Done() @@ -176,3 +183,17 @@ func announce(ctx context.Context, log *slog.Logger, node *p2p.Node, st *store.S timer.Reset(announceInterval) } } + +// loginURL is the link that signs the owner in to the web UI. +func loginURL(listenAddr, token string) string { + host, port, err := net.SplitHostPort(listenAddr) + if err != nil { + return "http://" + listenAddr + "/#/login/" + token + } + // A wildcard address (0.0.0.0, ::) is not something a browser can open. + if ip := net.ParseIP(host); ip == nil || ip.IsUnspecified() { + host = "127.0.0.1" + } + // JoinHostPort brackets IPv6 addresses. + return "http://" + net.JoinHostPort(host, port) + "/#/login/" + token +} diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 13145f1..5f2477a 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -307,8 +307,24 @@ text rules below are enforced by endpoints. `#/c///` (the secret stays in the URL fragment, which is never sent to a server) that polls every 20 s, shows only verified owner replies and counts the rest as rejected; `#/messages` lists the - conversations stored in this browser. There is **no owner UI yet**; the owner - uses the admin API. + conversations stored in this browser. +- **Owner UI** (`#/mine`): your listings (requests needing a reply, whether an + address is saved), the requests of one listing (status, preview, effort, + filters), a conversation, and a sheet to **release the address** (prefilled + from the saved one; address, phone, a free-text note, validity 1 to 30 days). + Three panes side by side on a wide screen, one at a time on a phone. It uses + the admin API above plus `GET /api/v1/mine` (own listings with request counts + and `address_saved`) and `GET /api/v1/inbox?listing=`, whose rows also + carry `last_dir`, `needs_reply`, a text `preview` (empty for hidden messages), + `effort` (proof-of-work bits of the first message) and `grant_until`. +- **Owner login.** The page itself never contains the admin token (every node + serves the same page, including public gateways). `node run` prints a link + `http://127.0.0.1:8080/#/login/` to the terminal, not to its log. The + token is in the URL fragment, which a browser never sends to a server; the + page moves it into `sessionStorage` (so closing the tab signs out), removes it + from the address bar and sends it only to the node that served the page. A + paste field is the fallback; a token the node refuses (for example after + `recover`) is forgotten. ### Limits of this design @@ -338,6 +354,7 @@ token written to `admin.token` at init, mode 0600). | `POST /api/v1/listings` | token | publish a draft; the node adds identity, time, PoW and signature | | `PUT /api/v1/listings/{id}` | token | replace own listing (`seq + 1`); the region cannot change | | `DELETE /api/v1/listings/{id}` | token | publish a tombstone | +| `GET /api/v1/mine` | token | your live listings with request counts and whether an address is saved (messaging, see above) | Reads send `Access-Control-Allow-Origin: *` (the data is public). Writes are not cross-origin. Images are not supported yet. `status` also reports the @@ -379,7 +396,7 @@ Proof-of-work per listing and per revocation, plus subscribable blocklists ## Open -An owner UI for the inbox, abuse reporting for messages, reciprocity rules, anti- +Abuse reporting for messages, reciprocity rules, anti- discrimination (AGG) content rules, blocklists, peer scoring and rate limits, incremental sync and anti-entropy, DHT discovery, UI publishing, image hosting, recovering listings onto a fresh machine with an empty database, a helper to diff --git a/internal/api/api.go b/internal/api/api.go index 82223ac..b921e7c 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -78,6 +78,7 @@ func New(cfg Config) http.Handler { mux.HandleFunc("GET /api/v1/messages", s.getMailbox) // ...and the owner's inbox and private address, behind the admin token. if cfg.Inbox != nil { + mux.HandleFunc("GET /api/v1/mine", s.admin(s.mine)) mux.HandleFunc("GET /api/v1/inbox", s.admin(s.listThreads)) mux.HandleFunc("GET /api/v1/inbox/{id}/{thread}", s.admin(s.getThread)) mux.HandleFunc("POST /api/v1/inbox/{id}/{thread}", s.admin(s.reply)) diff --git a/internal/api/messages.go b/internal/api/messages.go index 46e153f..6853f51 100644 --- a/internal/api/messages.go +++ b/internal/api/messages.go @@ -146,6 +146,84 @@ type threadView struct { Replied bool `json:"replied"` FirstAt int64 `json:"first_at"` LastAt int64 `json:"last_at"` + // LastDir is who wrote last: "in" (the buyer) or "out" (the owner). + LastDir string `json:"last_dir"` + // NeedsReply: the buyer wrote last and the message is readable. + NeedsReply bool `json:"needs_reply"` + // Preview is the start of the latest message (empty when it is hidden). + Preview string `json:"preview,omitempty"` + // Effort is the proof-of-work, in bits, of the buyer's first envelope. + Effort int `json:"effort"` + // GrantUntil is when the latest address release ends; 0 if none was sent. + GrantUntil int64 `json:"grant_until,omitempty"` +} + +const previewRunes = 120 + +// threadViews summarises the owner's conversations, optionally of one listing. +func (s *server) threadViews(r *http.Request, listingID string) ([]threadView, error) { + threads, err := s.Store.Threads(r.Context(), s.Device.Cert.Author) + if err != nil { + return nil, err + } + out := make([]threadView, 0, len(threads)) + for _, t := range threads { + if listingID != "" && t.Listing.ID != listingID { + continue + } + v := threadView{Listing: t.Listing.ID, Thread: t.Thread, Messages: t.Messages, Hidden: t.Hidden, + Replied: t.Replied, FirstAt: t.FirstAt, LastAt: t.LastAt} + msgs, err := s.Store.ThreadMessages(r.Context(), t.Listing, t.Thread) + if err != nil { + return nil, err + } + firstIn := true + for _, m := range msgs { + if m.Dir == store.InboxIn && firstIn { + v.Effort, firstIn = hexZeroBits(m.Hash), false + } + v.LastDir = m.Dir + v.NeedsReply = m.Dir == store.InboxIn && !m.Hidden + v.Preview = "" + switch { + case m.Hidden: + case m.Plain.Type == message.TypeGrant: + v.GrantUntil = m.Plain.ValidUntil + default: + v.Preview = truncateRunes(m.Plain.Text, previewRunes) + } + } + out = append(out, v) + } + return out, nil +} + +// hexZeroBits counts the leading zero bits of a hex-encoded hash. +func hexZeroBits(h string) int { + n := 0 + for _, c := range h { + d := strings.IndexRune("0123456789abcdef", c) + if d < 0 { + return n + } + if d == 0 { + n += 4 + continue + } + for bit := 3; bit >= 0 && d&(1<