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:
@@ -112,6 +112,9 @@ func New(cfg Config) http.Handler {
|
||||
mux.HandleFunc("GET /api/v1/inbox/{id}/{thread}", s.admin(s.getThread))
|
||||
mux.HandleFunc("POST /api/v1/inbox/{id}/{thread}", s.admin(s.reply))
|
||||
mux.HandleFunc("DELETE /api/v1/inbox/{id}/{thread}", s.admin(s.deleteThread))
|
||||
mux.HandleFunc("PUT /api/v1/inbox/{id}/{thread}/block", s.admin(s.blockThread))
|
||||
mux.HandleFunc("DELETE /api/v1/inbox/{id}/{thread}/block", s.admin(s.unblockThread))
|
||||
mux.HandleFunc("GET /api/v1/blocked", s.admin(s.listBlocked))
|
||||
mux.HandleFunc("PUT /api/v1/listings/{id}/address", s.admin(s.putAddress))
|
||||
mux.HandleFunc("GET /api/v1/listings/{id}/address", s.admin(s.getAddress))
|
||||
mux.HandleFunc("DELETE /api/v1/listings/{id}/address", s.admin(s.deleteAddress))
|
||||
|
||||
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -399,6 +400,50 @@ func (s *server) deleteThread(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *server) blockThread(w http.ResponseWriter, r *http.Request) {
|
||||
s.blockOrUnblock(w, r, s.Inbox.Block)
|
||||
}
|
||||
|
||||
func (s *server) unblockThread(w http.ResponseWriter, r *http.Request) {
|
||||
s.blockOrUnblock(w, r, s.Inbox.Unblock)
|
||||
}
|
||||
|
||||
func (s *server) blockOrUnblock(w http.ResponseWriter, r *http.Request, do func(context.Context, message.Ref, string) error) {
|
||||
ref, thread, ok := s.threadRef(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch err := do(r.Context(), ref, thread); {
|
||||
case err == nil:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case errors.Is(err, inbox.ErrNoThread), errors.Is(err, inbox.ErrNotOwner):
|
||||
fail(w, http.StatusNotFound, "no such conversation")
|
||||
default:
|
||||
fail(w, http.StatusInternalServerError, "internal error")
|
||||
}
|
||||
}
|
||||
|
||||
type blockedView struct {
|
||||
Listing string `json:"listing"`
|
||||
Thread string `json:"thread"`
|
||||
BlockedAt int64 `json:"blocked_at"`
|
||||
}
|
||||
|
||||
// listBlocked lists the conversations the owner blocked, so a block can be lifted.
|
||||
func (s *server) listBlocked(w http.ResponseWriter, r *http.Request) {
|
||||
bs, err := s.Store.BlockedThreads(r.Context(), s.Device.Cert.Author)
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "internal error")
|
||||
return
|
||||
}
|
||||
out := make([]blockedView, 0, len(bs))
|
||||
for _, b := range bs {
|
||||
out = append(out, blockedView{Listing: b.Listing.ID, Thread: b.Thread, BlockedAt: b.BlockedAt})
|
||||
}
|
||||
noStore(w)
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// threadRef reads {id} and {thread} from the path. The listing is always the
|
||||
// node's own: the inbox holds nobody else's conversations.
|
||||
func (s *server) threadRef(w http.ResponseWriter, r *http.Request) (message.Ref, string, bool) {
|
||||
|
||||
@@ -428,3 +428,36 @@ func TestStatusReportsMessageDifficulty(t *testing.T) {
|
||||
t.Fatalf("message_difficulty = %v", st["message_difficulty"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlockingAConversationOverTheAPI(t *testing.T) {
|
||||
f := newFixture(t, token)
|
||||
l := f.publish(t)
|
||||
tk := newThread(t)
|
||||
f.send(t, buyerHello(t, l, tk, "Hallo"))
|
||||
f.do(t, "GET", "/api/v1/inbox?listing="+l.ID, "", true) // collects
|
||||
|
||||
path := "/api/v1/inbox/" + l.ID + "/" + tk.PublicHex()
|
||||
if code, _ := f.do(t, "PUT", path+"/block", "", false); code != 401 {
|
||||
t.Fatalf("blocking without the token: %d", code)
|
||||
}
|
||||
if code, body := f.do(t, "PUT", path+"/block", "", true); code != 204 {
|
||||
t.Fatalf("block: %d %s", code, body)
|
||||
}
|
||||
if code, _ := f.do(t, "GET", path, "", true); code != 404 {
|
||||
t.Fatalf("a blocked conversation can still be read: %d", code)
|
||||
}
|
||||
var bl []blockedView
|
||||
code, body := f.do(t, "GET", "/api/v1/blocked", "", true)
|
||||
if code != 200 || json.Unmarshal(body, &bl) != nil || len(bl) != 1 || bl[0].Thread != tk.PublicHex() || bl[0].Listing != l.ID {
|
||||
t.Fatalf("blocked list: %d %s", code, body)
|
||||
}
|
||||
if code, _ := f.do(t, "PUT", "/api/v1/inbox/"+l.ID+"/"+strings.Repeat("0", 64)+"/block", "", true); code != 404 {
|
||||
t.Fatalf("blocking an unknown thread: %d", code)
|
||||
}
|
||||
if code, body := f.do(t, "DELETE", path+"/block", "", true); code != 204 {
|
||||
t.Fatalf("unblock: %d %s", code, body)
|
||||
}
|
||||
if _, body := f.do(t, "GET", "/api/v1/blocked", "", true); string(bytes.TrimSpace(body)) != "[]" {
|
||||
t.Fatalf("blocked list after unblocking: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
+50
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,3 +237,67 @@ func (s *Store) DeleteAddress(ctx context.Context, ref message.Ref) error {
|
||||
`DELETE FROM addresses WHERE author = ? AND listing_id = ?`, ref.Author, ref.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
// BlockThread erases a conversation and remembers its thread key, so the
|
||||
// envelopes the buyer keeps sending are dropped unread. It reports whether the
|
||||
// conversation existed; blocking an unknown thread does nothing.
|
||||
func (s *Store) BlockThread(ctx context.Context, ref message.Ref, thread string, now int64) (bool, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
res, err := tx.ExecContext(ctx,
|
||||
`DELETE FROM inbox WHERE author = ? AND listing_id = ? AND thread = ?`, ref.Author, ref.ID, thread)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT OR IGNORE INTO inbox_blocked (author, listing_id, thread, blocked_at) VALUES (?, ?, ?, ?)`,
|
||||
ref.Author, ref.ID, thread, now); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit()
|
||||
}
|
||||
|
||||
// UnblockThread lifts a block. What was sent while blocked is gone.
|
||||
func (s *Store) UnblockThread(ctx context.Context, ref message.Ref, thread string) (bool, error) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM inbox_blocked WHERE author = ? AND listing_id = ? AND thread = ?`, ref.Author, ref.ID, thread)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// Blocked is one blocked conversation.
|
||||
type Blocked struct {
|
||||
Listing message.Ref
|
||||
Thread string
|
||||
BlockedAt int64
|
||||
}
|
||||
|
||||
// BlockedThreads lists the author's blocked conversations, newest first.
|
||||
func (s *Store) BlockedThreads(ctx context.Context, author string) ([]Blocked, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT listing_id, thread, blocked_at FROM inbox_blocked WHERE author = ?
|
||||
ORDER BY blocked_at DESC, thread`, author)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Blocked
|
||||
for rows.Next() {
|
||||
b := Blocked{Listing: message.Ref{Author: author}}
|
||||
if err := rows.Scan(&b.Listing.ID, &b.Thread, &b.BlockedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
@@ -141,6 +141,15 @@ CREATE TABLE IF NOT EXISTS inbox_seen (
|
||||
hash TEXT PRIMARY KEY,
|
||||
expires_at INTEGER NOT NULL
|
||||
) WITHOUT ROWID;
|
||||
-- Conversations the owner blocked: further envelopes of the thread are dropped
|
||||
-- unread. Rows go when their listing does (see Expire).
|
||||
CREATE TABLE IF NOT EXISTS inbox_blocked (
|
||||
author TEXT NOT NULL,
|
||||
listing_id TEXT NOT NULL,
|
||||
thread TEXT NOT NULL,
|
||||
blocked_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (author, listing_id, thread)
|
||||
) WITHOUT ROWID;
|
||||
-- The exact address of a listing, only ever read by its owner's node.
|
||||
CREATE TABLE IF NOT EXISTS addresses (
|
||||
author TEXT NOT NULL,
|
||||
@@ -516,6 +525,11 @@ func (s *Store) Expire(ctx context.Context) (int64, error) {
|
||||
if _, err := s.db.ExecContext(ctx, `DELETE FROM inbox_seen WHERE expires_at <= ?`, now); err != nil {
|
||||
return n + m + k, err
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
DELETE FROM inbox_blocked WHERE NOT EXISTS (
|
||||
SELECT 1 FROM listings l WHERE l.author = inbox_blocked.author AND l.id = inbox_blocked.listing_id)`); err != nil {
|
||||
return n + m + k, err
|
||||
}
|
||||
return n + m + k, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user