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:
+42
-9
@@ -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.
|
||||
|
||||
@@ -159,6 +159,57 @@ func TestCollectFilesConversationsOnce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrivalsAreAnnouncedOnceAndWithoutText(t *testing.T) {
|
||||
f := newFixture(t, 0)
|
||||
var calls [][]Arrival
|
||||
f.in.OnArrivals = func(a []Arrival) { calls = append(calls, a) }
|
||||
l := f.listing()
|
||||
ref := message.Ref{Author: l.Author, ID: l.ID}
|
||||
a, b, spam := f.newThread(), f.newThread(), f.newThread()
|
||||
|
||||
if f.collect(); len(calls) != 0 {
|
||||
t.Fatalf("nothing arrived but %d calls were made", len(calls))
|
||||
}
|
||||
|
||||
f.buyer(l, a, 1, "Ist die Wohnung noch frei?", msgDifficulty)
|
||||
f.buyer(l, a, 2, "Ich könnte morgen vorbeikommen.", msgDifficulty)
|
||||
f.buyer(l, b, 1, "Guten Tag", msgDifficulty)
|
||||
f.buyer(l, spam, 1, "Schaut euch https://example.com/x an", msgDifficulty) // hidden
|
||||
f.collect()
|
||||
if len(calls) != 1 {
|
||||
t.Fatalf("one collection must make one call, got %d", len(calls))
|
||||
}
|
||||
got := calls[0]
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("got %d arrivals, want 3 (the hidden one is not announced): %+v", len(got), got)
|
||||
}
|
||||
starts := 0
|
||||
for _, x := range got {
|
||||
if x.Listing != ref {
|
||||
t.Errorf("wrong listing %+v", x.Listing)
|
||||
}
|
||||
if x.Thread == spam.PublicHex() {
|
||||
t.Error("a hidden message was announced")
|
||||
}
|
||||
if x.New {
|
||||
starts++
|
||||
}
|
||||
}
|
||||
if starts != 2 {
|
||||
t.Errorf("%d conversations started, want 2 (a and b)", starts)
|
||||
}
|
||||
|
||||
// Nothing new: no repeat. A further message in a known conversation is not a start.
|
||||
if f.collect(); len(calls) != 1 {
|
||||
t.Fatalf("a second collection announced again: %d calls", len(calls))
|
||||
}
|
||||
f.buyer(l, a, 3, "Noch da?", msgDifficulty)
|
||||
f.collect()
|
||||
if len(calls) != 2 || len(calls[1]) != 1 || calls[1][0].New || calls[1][0].Thread != a.PublicHex() {
|
||||
t.Fatalf("follow-up not announced as a plain message in a: %+v", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinksInFirstMessagesAreHiddenUntilTheOwnerReplies(t *testing.T) {
|
||||
f := newFixture(t, 0)
|
||||
l := f.listing()
|
||||
|
||||
Reference in New Issue
Block a user