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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user