The node now checks for new messages every 10 s in the background (before, only when the web UI asked), and Inbox.Collect reports newly filed visible messages through OnArrivals. The CLI logs one line per conversation with the listing title and a link to it, never the buyer's text. Hidden messages are not announced, a backlog is capped at 5 lines, and a lock stops a background check and a UI request from filing the same message twice. The e2e checks that node A announces the message before its API is called. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"sort"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/inbox"
|
|
"git.stinnesbeck.com/nils/allmende/internal/store"
|
|
)
|
|
|
|
const (
|
|
// inboxCheckEvery is how often the node looks for new messages when the
|
|
// web UI is not open. (The UI triggers a check itself whenever it asks.)
|
|
inboxCheckEvery = 10 * time.Second
|
|
// maxNotices caps the lines one check writes, so a backlog after a long
|
|
// time offline does not flood the terminal.
|
|
maxNotices = 5
|
|
)
|
|
|
|
// watchInbox files incoming messages in the background, so the owner is told
|
|
// about them on the terminal even with no browser open. The notices come from
|
|
// the Inbox's OnArrivals callback, which also fires for checks made by the UI.
|
|
func watchInbox(ctx context.Context, log *slog.Logger, in *inbox.Inbox) {
|
|
tick := time.NewTicker(inboxCheckEvery)
|
|
defer tick.Stop()
|
|
failing := false
|
|
for {
|
|
if _, err := in.Collect(ctx); err != nil && ctx.Err() == nil {
|
|
if !failing { // say it once, not every ten seconds
|
|
log.Warn("could not check for new messages", "err", err)
|
|
}
|
|
failing = true
|
|
} else {
|
|
failing = false
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-tick.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
// arrivalNotice returns the callback that tells the owner about new messages.
|
|
// It names the listing and links to the conversation but never prints what
|
|
// the buyer wrote: that is untrusted text and belongs in the browser.
|
|
func arrivalNotice(ctx context.Context, log *slog.Logger, st *store.Store, httpAddr string) func([]inbox.Arrival) {
|
|
type conv struct {
|
|
listing, thread string
|
|
author string
|
|
count int
|
|
started bool
|
|
}
|
|
return func(arrivals []inbox.Arrival) {
|
|
byConv := map[string]*conv{}
|
|
var order []string
|
|
for _, a := range arrivals {
|
|
key := a.Listing.ID + "/" + a.Thread
|
|
c := byConv[key]
|
|
if c == nil {
|
|
c = &conv{listing: a.Listing.ID, thread: a.Thread, author: a.Listing.Author}
|
|
byConv[key] = c
|
|
order = append(order, key)
|
|
}
|
|
c.count++
|
|
c.started = c.started || a.New
|
|
}
|
|
sort.Strings(order)
|
|
for n, key := range order {
|
|
if n == maxNotices {
|
|
log.Info("more conversations have new messages", "more", len(order)-maxNotices,
|
|
"open", uiURL(httpAddr, "/mine"))
|
|
break
|
|
}
|
|
c := byConv[key]
|
|
title := ""
|
|
if l, err := st.Get(ctx, c.author, c.listing); err == nil && l != nil {
|
|
title = l.Title
|
|
}
|
|
msg := "new message"
|
|
if c.started {
|
|
msg = "new conversation"
|
|
}
|
|
attrs := []any{"listing", title}
|
|
if c.count > 1 {
|
|
attrs = append(attrs, "messages", c.count)
|
|
}
|
|
attrs = append(attrs,
|
|
"conversation", shortID(c.thread),
|
|
"open", uiURL(httpAddr, fmt.Sprintf("/mine/%s/%s", c.listing, c.thread)))
|
|
log.Info(msg, attrs...)
|
|
}
|
|
}
|
|
}
|
|
|
|
func shortID(hex string) string {
|
|
if len(hex) > 8 {
|
|
return hex[:8]
|
|
}
|
|
return hex
|
|
}
|