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:
@@ -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