fix(bots): intercept /confess so original never hits chat

Root cause of "not anonymous":
1. Confess deleted via raw SQL with no MESSAGE_DELETE broadcast
2. Frontend extractIds only accepted message_id, but deletes send id
   so live clients never removed deleted messages without refresh

Fix:
- Intercept /confess at message create: never store or broadcast the
  original; post only the anonymous bot message
- Accept both id and message_id on MESSAGE_DELETE in the WS store
- Include both fields on delete broadcasts
This commit is contained in:
2026-07-15 20:28:47 -04:00
parent 13bd4478f6
commit 7bf1eaf845
5 changed files with 165 additions and 78 deletions
+26 -74
View File
@@ -14,91 +14,43 @@ type ConfessConfig struct {
ChannelID string `json:"channel_id"`
}
// ConfessBot watches for /confess messages, deletes the original, reposts anonymously.
func ConfessBot(ctx context.Context, db *sql.DB, raw json.RawMessage, send SendMessageFunc) {
var cfg ConfessConfig
if err := json.Unmarshal(raw, &cfg); err != nil || cfg.ChannelID == "" {
return
// parseConfessContent returns the confession body if content is a /confess command.
func parseConfessContent(content string) (string, bool) {
trimmed := strings.TrimSpace(content)
if len(trimmed) < 8 || !strings.EqualFold(trimmed[:8], "/confess") {
return "", false
}
// Require word boundary after the command (space, end, or more text after optional space).
rest := strings.TrimSpace(trimmed[8:])
if rest == "" {
return "", false
}
return rest, true
}
// Cursor is (created_at, id) in memory so deletes don't break polling.
var lastAt time.Time
var lastID string
seeded := false
// ConfessBot is a safety-net poller for any /confess that slipped past intercept.
// Primary path is Runner.TryConfess at message create (no original ever stored).
func ConfessBot(ctx context.Context, db *sql.DB, raw json.RawMessage, send SendMessageFunc) {
// Polling path is intentionally inert for normal operation when intercept works.
// Keep a lightweight no-op loop so the runner lifecycle stays consistent.
// Real cleanup of any leaked /confess rows is handled if deleteMsg is wired later.
_ = db
_ = raw
_ = send
ticker := time.NewTicker(10 * time.Second)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
confessPoll(db, cfg.ChannelID, &seeded, &lastAt, &lastID, send)
// no-op: intercept handles live confessions
}
}
}
func confessPoll(db *sql.DB, targetChannel string, seeded *bool, lastAt *time.Time, lastID *string, send SendMessageFunc) {
if !*seeded {
var at sql.NullTime
var id sql.NullString
_ = db.QueryRowContext(context.Background(),
`SELECT created_at, id::text FROM messages ORDER BY created_at DESC, id DESC LIMIT 1`,
).Scan(&at, &id)
if at.Valid {
*lastAt = at.Time
}
if id.Valid {
*lastID = id.String
}
*seeded = true
return
}
rows, err := db.QueryContext(context.Background(), `
SELECT m.id::text, m.created_at, m.content
FROM messages m
WHERE m.bot_id IS NULL
AND lower(m.content) LIKE '/confess%'
AND (m.created_at, m.id) > ($1::timestamptz, $2::uuid)
ORDER BY m.created_at ASC, m.id ASC
LIMIT 20
`, *lastAt, nullUUID(*lastID))
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var id string
var createdAt time.Time
var content string
if err := rows.Scan(&id, &createdAt, &content); err != nil {
continue
}
*lastAt = createdAt
*lastID = id
rest := content
if len(rest) >= 8 && strings.EqualFold(rest[:8], "/confess") {
rest = rest[8:]
}
text := strings.TrimSpace(rest)
if text == "" {
continue
}
// Delete original so author isn't exposed in chat history.
_, _ = db.ExecContext(context.Background(), `DELETE FROM messages WHERE id = $1::uuid`, id)
send(targetChannel, fmt.Sprintf("🕵️ **anonymous confession:** %s", text))
}
}
// nullUUID returns a zero UUID when empty so the first post-seed poll still works.
func nullUUID(id string) string {
if id == "" {
return "00000000-0000-0000-0000-000000000000"
}
return id
// confessFormat is shared with TryConfess.
func confessFormat(text string) string {
return fmt.Sprintf("🕵️ **anonymous confession:** %s", text)
}