messaging: owners can block a conversation

A blocked thread key is dropped unread (not announced, not counted against
the daily new-conversation limit). API, UI button with confirmation, and a
list with unblock.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 20:50:38 +01:00
co-authored by Claude Sonnet 5.5
parent a54094ea35
commit 13c82f8d59
14 changed files with 433 additions and 3 deletions
+50 -1
View File
@@ -133,6 +133,15 @@ func (i *Inbox) Collect(ctx context.Context) (int, error) {
return 0, err
}
blockedList, err := i.Store.BlockedThreads(ctx, i.Device.Cert.Author)
if err != nil {
return 0, err
}
blocked := map[threadKey]bool{}
for _, b := range blockedList {
blocked[threadKey{b.Listing, b.Thread}] = true
}
var known []candidate
fresh := map[threadKey][]candidate{}
for k, e := range envs {
@@ -148,9 +157,16 @@ func (i *Inbox) Collect(ctx context.Context) (int, error) {
}
continue
}
key := threadKey{e.Listing, p.Thread}
if blocked[key] {
// The owner blocked this conversation: drop it unread, count nothing.
if err := i.Store.MarkSeen(ctx, h, e.ExpiresAt); err != nil {
return 0, err
}
continue
}
work, _ := e.Work()
c := candidate{hash: h, env: e, plain: p, work: work}
key := threadKey{e.Listing, p.Thread}
exists, err := i.Store.HasThread(ctx, e.Listing, p.Thread)
if err != nil {
return 0, err
@@ -244,6 +260,39 @@ func (i *Inbox) file(ctx context.Context, c candidate) (hidden bool, err error)
return hidden, err
}
// Block erases a conversation and drops everything its thread key sends from
// now on, without notice to the buyer. A buyer can start over with a new
// thread key, which costs a first message (proof-of-work, link ban, the daily
// new-conversation limit) again.
func (i *Inbox) Block(ctx context.Context, ref message.Ref, thread string) error {
if ref.Author != i.Device.Cert.Author {
return ErrNotOwner
}
ok, err := i.Store.BlockThread(ctx, ref, thread, i.now().Unix())
if err != nil {
return err
}
if !ok {
return ErrNoThread
}
return nil
}
// Unblock lifts a block. Envelopes that arrived while blocked are gone.
func (i *Inbox) Unblock(ctx context.Context, ref message.Ref, thread string) error {
if ref.Author != i.Device.Cert.Author {
return ErrNotOwner
}
ok, err := i.Store.UnblockThread(ctx, ref, thread)
if err != nil {
return err
}
if !ok {
return ErrNoThread
}
return nil
}
// Reply is what the owner sends back.
type Reply struct {
Type string // message.TypeText or message.TypeGrant
+63
View File
@@ -414,3 +414,66 @@ func TestAddressesAndThreadsCanBeErased(t *testing.T) {
t.Fatalf("an erased conversation came back (%d)", n)
}
}
func TestABlockedConversationStaysSilentAndFreesNothingElse(t *testing.T) {
f := newFixture(t, 1) // one new conversation per day
l := f.listing()
ref := message.Ref{Author: l.Author, ID: l.ID}
var announced int
f.in.OnArrivals = func(a []Arrival) { announced += len(a) }
spam := f.newThread()
f.buyer(l, spam, 1, "Hallo", msgDifficulty)
f.collect()
if err := f.in.Block(ctx, ref, spam.PublicHex()); err != nil {
t.Fatal(err)
}
if th, _ := f.st.Threads(ctx, l.Author); len(th) != 0 {
t.Fatal("the blocked conversation is still listed")
}
announced = 0
// The buyer keeps writing in the same thread: nothing is filed or announced.
f.buyer(l, spam, 2, "Hallo?", msgDifficulty)
f.buyer(l, spam, 3, "Antwortet bitte", msgDifficulty)
if n := f.collect(); n != 0 || announced != 0 {
t.Fatalf("a blocked thread got through: filed %d, announced %d", n, announced)
}
if th, _ := f.st.Threads(ctx, l.Author); len(th) != 0 {
t.Fatal("a blocked thread reappeared")
}
// Blocking is per thread: someone else (here under a new daily budget) is heard.
f.now = f.now.Add(25 * time.Hour)
other := f.newThread()
f.buyer(l, other, 1, "Guten Tag", msgDifficulty)
if n := f.collect(); n != 1 {
t.Fatalf("another buyer was not heard: %d", n)
}
// The block can be lifted; the buyer's later messages arrive again.
if err := f.in.Unblock(ctx, ref, spam.PublicHex()); err != nil {
t.Fatal(err)
}
f.now = f.now.Add(25 * time.Hour) // it starts a new conversation, which the daily limit applies to
f.buyer(l, spam, 4, "Noch da?", msgDifficulty)
f.collect()
if th, _ := f.st.Threads(ctx, l.Author); len(th) != 2 {
t.Fatalf("after unblocking, %d conversations, want 2", len(th))
}
}
func TestBlockingNeedsAnExistingConversationOfYours(t *testing.T) {
f := newFixture(t, 0)
l := f.listing()
tk := f.newThread()
if err := f.in.Block(ctx, message.Ref{Author: l.Author, ID: l.ID}, tk.PublicHex()); !errors.Is(err, ErrNoThread) {
t.Fatalf("blocking an unknown thread: %v", err)
}
if err := f.in.Block(ctx, message.Ref{Author: "someone-else", ID: l.ID}, tk.PublicHex()); !errors.Is(err, ErrNotOwner) {
t.Fatalf("blocking another author's listing: %v", err)
}
if err := f.in.Unblock(ctx, message.Ref{Author: l.Author, ID: l.ID}, tk.PublicHex()); !errors.Is(err, ErrNoThread) {
t.Fatalf("unblocking a thread that is not blocked: %v", err)
}
}