Announce incoming messages on the node's terminal

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>
This commit is contained in:
2026-11-17 21:29:49 +01:00
co-authored by Claude Sonnet 5.5
parent 20ea9b32f7
commit a81595e9ee
8 changed files with 316 additions and 16 deletions
+42 -9
View File
@@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"sort"
"sync"
"time"
"git.stinnesbeck.com/nils/allmende/internal/identity"
@@ -52,6 +53,24 @@ type Inbox struct {
// conversations; 0 means DefaultMaxNewThreads.
MaxNewThreads int
Clock func() time.Time
// OnArrivals, if set, is called after a Collect that filed visible new
// messages, once per call, with one entry per message. It runs on the
// collecting goroutine, so it should return quickly. It never receives
// message text. Set it before the Inbox is used.
OnArrivals func([]Arrival)
mu sync.Mutex // one Collect at a time, so a message is filed and announced once
}
// Arrival describes a message that was just filed for one of the owner's
// listings. Hidden messages (those that broke the first-message rules) are not
// reported.
type Arrival struct {
Listing message.Ref
Thread string
// New is true if the message started a conversation.
New bool
}
func (i *Inbox) now() time.Time {
@@ -91,6 +110,14 @@ type threadKey struct {
// hours. Surplus first messages stay queued, unread, until there is room
// (strongest proof-of-work first, then oldest) or they expire.
func (i *Inbox) Collect(ctx context.Context) (int, error) {
i.mu.Lock()
defer i.mu.Unlock()
var arrivals []Arrival
defer func() {
if i.OnArrivals != nil && len(arrivals) > 0 {
i.OnArrivals(arrivals)
}
}()
envs, err := i.Store.OwnerMessages(ctx, i.Device.Cert.Author)
if err != nil {
return 0, err
@@ -136,22 +163,26 @@ func (i *Inbox) Collect(ctx context.Context) (int, error) {
}
added := 0
admit := func(cs []candidate) error {
admit := func(cs []candidate, starts bool) error {
sort.Slice(cs, func(a, b int) bool {
if cs[a].plain.SentAt != cs[b].plain.SentAt {
return cs[a].plain.SentAt < cs[b].plain.SentAt
}
return cs[a].plain.N < cs[b].plain.N
})
for _, c := range cs {
if err := i.file(ctx, c); err != nil {
for k, c := range cs {
hidden, err := i.file(ctx, c)
if err != nil {
return err
}
added++
if !hidden {
arrivals = append(arrivals, Arrival{Listing: c.env.Listing, Thread: c.plain.Thread, New: starts && k == 0})
}
}
return nil
}
if err := admit(known); err != nil {
if err := admit(known, false); err != nil {
return added, err
}
@@ -191,7 +222,7 @@ func (i *Inbox) Collect(ctx context.Context) (int, error) {
return keys[a].thread < keys[b].thread
})
for k := 0; k < len(keys) && k < room; k++ {
if err := admit(fresh[keys[k]]); err != nil {
if err := admit(fresh[keys[k]], true); err != nil {
return added, err
}
}
@@ -199,16 +230,18 @@ func (i *Inbox) Collect(ctx context.Context) (int, error) {
return added, nil
}
func (i *Inbox) file(ctx context.Context, c candidate) error {
// file stores one incoming message and reports whether it was hidden.
func (i *Inbox) file(ctx context.Context, c candidate) (hidden bool, err error) {
replied, err := i.Store.Replied(ctx, c.env.Listing, c.plain.Thread)
if err != nil {
return err
return false, err
}
hidden := !replied && c.plain.CheckBuyerFirst() != nil
return i.Store.AddInbox(ctx, store.InboxMessage{
hidden = !replied && c.plain.CheckBuyerFirst() != nil
err = i.Store.AddInbox(ctx, store.InboxMessage{
Hash: c.hash, Listing: c.env.Listing, Thread: c.plain.Thread, Dir: store.InboxIn,
Plain: c.plain, Hidden: hidden, ReceivedAt: i.now().Unix(),
}, c.env.ExpiresAt)
return hidden, err
}
// Reply is what the owner sends back.