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 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:02:51 +01:00
co-authored by Claude Sonnet 5.5
parent 3b0351c774
commit 8edae3ae56
17 changed files with 2400 additions and 49 deletions
+133 -4
View File
@@ -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<<bit) == 0; bit-- {
n++
}
return n
}
return n
}
func truncateRunes(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n]) + "…"
}
func (s *server) listThreads(w http.ResponseWriter, r *http.Request) {
@@ -153,14 +231,65 @@ func (s *server) listThreads(w http.ResponseWriter, r *http.Request) {
fail(w, http.StatusInternalServerError, "internal error")
return
}
threads, err := s.Store.Threads(r.Context(), s.Device.Cert.Author)
out, err := s.threadViews(r, r.URL.Query().Get("listing"))
if err != nil {
fail(w, http.StatusInternalServerError, "internal error")
return
}
out := make([]threadView, 0, len(threads))
for _, t := range threads {
out = append(out, threadView{t.Listing.ID, t.Thread, t.Messages, t.Hidden, t.Replied, t.FirstAt, t.LastAt})
noStore(w)
writeJSON(w, http.StatusOK, out)
}
type mineView struct {
ID string `json:"id"`
Title string `json:"title"`
Kind string `json:"kind,omitempty"`
Price *listing.Price `json:"price,omitempty"`
ExpiresAt int64 `json:"expires_at"`
Threads int `json:"threads"`
NeedsReply int `json:"needs_reply"`
Hidden int `json:"hidden"`
AddressSaved bool `json:"address_saved"`
}
// mine lists the owner's live listings with what the inbox holds for each.
func (s *server) mine(w http.ResponseWriter, r *http.Request) {
if _, err := s.Inbox.Collect(r.Context()); err != nil {
fail(w, http.StatusInternalServerError, "internal error")
return
}
ls, err := s.Store.ByAuthor(r.Context(), s.Device.Cert.Author)
if err != nil {
fail(w, http.StatusInternalServerError, "internal error")
return
}
threads, err := s.threadViews(r, "")
if err != nil {
fail(w, http.StatusInternalServerError, "internal error")
return
}
out := make([]mineView, 0, len(ls))
for _, l := range ls {
v := mineView{ID: l.ID, Title: l.Title, Kind: l.Kind, Price: l.Price, ExpiresAt: l.ExpiresAt}
for _, t := range threads {
if t.Listing != l.ID {
continue
}
v.Threads++
if t.NeedsReply {
v.NeedsReply++
}
if t.Hidden > 0 {
v.Hidden++
}
}
if _, err := s.Store.GetAddress(r.Context(), message.Ref{Author: l.Author, ID: l.ID}); err == nil {
v.AddressSaved = true
} else if !errors.Is(err, store.ErrNotFound) {
fail(w, http.StatusInternalServerError, "internal error")
return
}
out = append(out, v)
}
noStore(w)
writeJSON(w, http.StatusOK, out)