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:
@@ -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))
|
||||
|
||||
+133
-4
@@ -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)
|
||||
|
||||
@@ -155,6 +155,82 @@ func TestMessagingOverHTTP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMineAndThreadSummaries(t *testing.T) {
|
||||
f := newFixture(t, token)
|
||||
l := f.publish(t)
|
||||
|
||||
// Nothing yet: the listing is there, with nothing in its inbox.
|
||||
var mine []mineView
|
||||
code, body := f.do(t, "GET", "/api/v1/mine", "", true)
|
||||
if code != 200 || json.Unmarshal(body, &mine) != nil || len(mine) != 1 {
|
||||
t.Fatalf("mine: %d %s", code, body)
|
||||
}
|
||||
if mine[0].ID != l.ID || mine[0].Title != l.Title || mine[0].Threads != 0 || mine[0].AddressSaved {
|
||||
t.Fatalf("empty listing: %+v", mine[0])
|
||||
}
|
||||
|
||||
// One readable request, one with a link (hidden).
|
||||
ok, bad := newThread(t), newThread(t)
|
||||
f.send(t, buyerHello(t, l, ok, "Ist die Wohnung noch frei?"))
|
||||
f.send(t, buyerHello(t, l, bad, "Schau auf https://evil.example"))
|
||||
f.do(t, "PUT", "/api/v1/listings/"+l.ID+"/address", `{"address":"Musterweg 7, 22765 Hamburg"}`, true)
|
||||
|
||||
code, body = f.do(t, "GET", "/api/v1/mine", "", true)
|
||||
if code != 200 || json.Unmarshal(body, &mine) != nil {
|
||||
t.Fatalf("mine: %d %s", code, body)
|
||||
}
|
||||
if m := mine[0]; m.Threads != 2 || m.NeedsReply != 1 || m.Hidden != 1 || !m.AddressSaved {
|
||||
t.Fatalf("counts: %+v", m)
|
||||
}
|
||||
|
||||
byThread := func() map[string]threadView {
|
||||
var ts []threadView
|
||||
code, body := f.do(t, "GET", "/api/v1/inbox?listing="+l.ID, "", true)
|
||||
if code != 200 || json.Unmarshal(body, &ts) != nil {
|
||||
t.Fatalf("inbox: %d %s", code, body)
|
||||
}
|
||||
out := map[string]threadView{}
|
||||
for _, v := range ts {
|
||||
out[v.Thread] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
ts := byThread()
|
||||
a, b := ts[ok.PublicHex()], ts[bad.PublicHex()]
|
||||
if !a.NeedsReply || a.LastDir != "in" || a.Preview != "Ist die Wohnung noch frei?" || a.Effort < msgDifficulty {
|
||||
t.Fatalf("readable request: %+v", a)
|
||||
}
|
||||
if b.NeedsReply || b.Preview != "" || b.Hidden != 1 {
|
||||
t.Fatalf("hidden request must not leak its text or ask for a reply: %+v", b)
|
||||
}
|
||||
|
||||
// Answer with an address: the request no longer needs a reply and shows the release.
|
||||
path := "/api/v1/inbox/" + l.ID + "/" + ok.PublicHex()
|
||||
if code, body := f.do(t, "POST", path, `{"type":"grant","note":"Samstag 10 Uhr","valid_days":7}`, true); code != 200 {
|
||||
t.Fatalf("grant: %d %s", code, body)
|
||||
}
|
||||
a = byThread()[ok.PublicHex()]
|
||||
if a.NeedsReply || a.LastDir != "out" || a.GrantUntil == 0 || a.Preview != "" {
|
||||
t.Fatalf("after the grant: %+v", a)
|
||||
}
|
||||
|
||||
// Another listing's filter returns nothing; the token is required.
|
||||
if code, body := f.do(t, "GET", "/api/v1/inbox?listing="+strings.Repeat("0", 32), "", true); code != 200 || strings.TrimSpace(string(body)) != "[]" {
|
||||
t.Fatalf("filter: %d %s", code, body)
|
||||
}
|
||||
if code, _ := f.do(t, "GET", "/api/v1/mine", "", false); code != 401 {
|
||||
t.Fatalf("mine without a token: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexZeroBits(t *testing.T) {
|
||||
for h, want := range map[string]int{"": 0, "f": 0, "8": 0, "7": 1, "1": 3, "0": 4, "00f": 8, "0a": 4, "004": 9, "0000": 16, "zz": 0} {
|
||||
if got := hexZeroBits(h); got != want {
|
||||
t.Errorf("hexZeroBits(%q) = %d, want %d", h, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHiddenFirstMessageTextIsWithheld(t *testing.T) {
|
||||
f := newFixture(t, token)
|
||||
l := f.publish(t)
|
||||
|
||||
Reference in New Issue
Block a user