chat: read receipts (delivered + seen) and persisted active bucket
Read receipts on chat messages — single check for delivered (recipient
polled), double check for seen (recipient opened the thread). Pending
state shows a clock until the relay acks the post.
Relay (relay/):
* bootstrap.php: ALTER TABLE chat_messages ADD COLUMN seen + a
(sender_id, id) index for the sender-side state poll. Migration is
idempotent via PRAGMA table_info.
* routes/chat.php: two new routes
- GET /api/chat/sent?since=N — returns the receipt state (id,
recipient_id, fetched, seen, created_at) for messages the caller
sent. No body — sender already has the plaintext locally.
- POST /api/chat/seen { peer } — marks every message FROM peer TO
caller as seen=1. Idempotent. Frontend calls this when opening
a thread; sender's next /sent poll picks the new state up.
Plugin backend (public/plugins/chat/backend.php):
* schema migration: chat_messages gets `delivered` and `seen` columns
(idempotent), only meaningful for direction='out'.
* /api/chat/send: stores the relay's autoincrement id as `relay_id`
on the local outgoing row so the receipt poll can match against it.
* /api/chat/poll: after the normal incoming pull, also calls
GET /api/chat/sent?since=last_sent_relay_id and updates local
outgoing rows with delivered/seen via UPDATE WHERE relay_id. The
cursor (last_sent_relay_id state value) only advances past rows
whose state is final (seen=1); rows still in flight stay re-pollable.
Response now carries `receipts: <count>` so the frontend can refresh
the open thread when state changes without new incoming.
* /api/chat/read: after the local is_read=1 update, fires a POST to
the relay's /api/chat/seen so the sender gets the double-check.
Silent on failure — the user already saw the messages locally.
* /api/chat/messages: SELECT now includes delivered, seen.
Plugin frontend (public/plugins/chat/chat.js):
* renderThread() appends a receipt icon to outgoing bubbles:
- seen=1 → ph-checks (accent, double check)
- delivered=1, seen=0 → ph-check (muted, single check)
- both 0 → ph-clock (dim, pending)
Incoming messages get nothing.
* pollOnce() refreshes the open thread when receipts > 0 even with
no new incoming, so the icons update in real time while the user
is looking at the thread.
CSS (public/plugins/chat/chat.css):
* .chat-bubble-time becomes flex with gap so timestamp + receipt
icon sit on one line.
* .chat-receipt.pending / .delivered / .seen color tiers.
board: persist active bucket across reloads
* state.js: state.activeBucket initializes from
localStorage.getItem('hyprcrd-active-bucket') || 'all'. New
setActiveBucket(value) helper writes through to localStorage; new
ensureActiveBucketValid() helper falls back to 'all' if the
persisted id no longer exists in state.buckets.
* buckets.js: loadBuckets() calls ensureActiveBucketValid() right
after the API fetch and before renderBar(). All four production
assignments (delete-keep, delete-all, popover entry click, regular
tab click) replaced with setActiveBucket() so the persisted value
stays in sync. Tests still mutate state.activeBucket directly so
they don't touch localStorage.
chore: bump version to 0.9.6
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9b4869812f
commit
285e3b4abe
8 files changed
+223
-16
No files matched your search
@@ -180,6 +180,16 @@ $db->exec('CREATE TABLE IF NOT EXISTS chat_messages (
|
||||
FOREIGN KEY (sender_id) REFERENCES tenants(id) ON DELETE SET NULL
|
||||
)');
|
||||
$db->exec('CREATE INDEX IF NOT EXISTS idx_chat_recipient ON chat_messages(recipient_id, fetched, id)');
|
||||
// Read receipts: `fetched` (already present) is "delivered" — set when the
|
||||
// recipient polls /api/chat. `seen` is "seen" — set when the recipient
|
||||
// opens the thread with the sender via POST /api/chat/seen. The sender
|
||||
// learns both states by polling GET /api/chat/sent.
|
||||
$chatCols = $db->query('PRAGMA table_info(chat_messages)')->fetchAll();
|
||||
$chatColNames = array_column($chatCols, 'name');
|
||||
if (!in_array('seen', $chatColNames)) {
|
||||
$db->exec('ALTER TABLE chat_messages ADD COLUMN seen INTEGER NOT NULL DEFAULT 0');
|
||||
}
|
||||
$db->exec('CREATE INDEX IF NOT EXISTS idx_chat_sender_state ON chat_messages(sender_id, id)');
|
||||
|
||||
// Collaborative buckets: shared among multiple tenants. Each bucket has one
|
||||
// owner and a membership set. Buckets persist (not ephemeral) since they are
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
<?php
|
||||
// Chat routes (client API only):
|
||||
// POST /api/chat — send a chat message to {recipient}
|
||||
// GET /api/chat?since=N — pull all chat messages addressed to me with id > N
|
||||
// POST /api/chat — send a chat message to {recipient}
|
||||
// GET /api/chat?since=N — pull all messages addressed to me (id > N)
|
||||
// GET /api/chat/sent?since=N — pull state updates (delivered/seen) for
|
||||
// *my* sent messages with id > N. The
|
||||
// sender uses this to learn read receipts
|
||||
// without needing a backchannel.
|
||||
// POST /api/chat/seen {peer} — mark every message FROM peer TO me as
|
||||
// seen. The sender's next /api/chat/sent
|
||||
// poll picks the new state up.
|
||||
//
|
||||
// Same decrypt-reencrypt hop as messages.php: arrives encrypted with the
|
||||
// senders client secret, stored at-rest under the inter-tenant key, returned
|
||||
@@ -80,3 +87,46 @@ if ($route === '/api/chat' && $method === 'GET') {
|
||||
}
|
||||
jsonResponse(['messages' => $out, 'max_id' => $maxId]);
|
||||
}
|
||||
|
||||
// ── GET /api/chat/sent ─────────────────────────────────────────
|
||||
// Returns the receipt state (fetched/seen) for messages the caller has
|
||||
// sent. Body is intentionally NOT included — the sender already has the
|
||||
// plaintext locally; the relay only echoes the state flags so the sender
|
||||
// can render check / double-check on the right messages.
|
||||
|
||||
if ($route === '/api/chat/sent' && $method === 'GET') {
|
||||
if (!$clientTenant) jsonResponse(['error' => 'unauthorized'], 401);
|
||||
$since = (int)($_GET['since'] ?? 0);
|
||||
$stmt = $db->prepare('
|
||||
SELECT id, recipient_id, fetched, seen, created_at
|
||||
FROM chat_messages
|
||||
WHERE sender_id = :s AND id > :since
|
||||
ORDER BY id ASC
|
||||
LIMIT 1000
|
||||
');
|
||||
$stmt->execute(['s' => $clientTenant['id'], 'since' => $since]);
|
||||
$rows = $stmt->fetchAll();
|
||||
$maxId = $since;
|
||||
foreach ($rows as $r) {
|
||||
if ((int)$r['id'] > $maxId) $maxId = (int)$r['id'];
|
||||
}
|
||||
jsonResponse(['receipts' => $rows, 'max_id' => $maxId]);
|
||||
}
|
||||
|
||||
// ── POST /api/chat/seen ────────────────────────────────────────
|
||||
// Body: { peer: "<sender_username>" }. Marks every message that the
|
||||
// peer sent to the caller as seen=1. Idempotent — safe to call on
|
||||
// every thread open.
|
||||
|
||||
if ($route === '/api/chat/seen' && $method === 'POST') {
|
||||
if (!$clientTenant) jsonResponse(['error' => 'unauthorized'], 401);
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?: [];
|
||||
$peer = trim((string)($input['peer'] ?? ''));
|
||||
if ($peer === '') jsonResponse(['error' => 'peer required'], 400);
|
||||
$db->prepare('
|
||||
UPDATE chat_messages
|
||||
SET seen = 1
|
||||
WHERE recipient_id = :r AND sender_username = :p AND seen = 0
|
||||
')->execute(['r' => $clientTenant['id'], 'p' => $peer]);
|
||||
jsonResponse(['ok' => true]);
|
||||
}
|
||||
Reference in new issue
Block a user