v2.0.9 — sync toast split, relay group chat UI, find/replace flaky fix
- sync: the post-sync toast lumped relay pulls into the CalDAV stats and always labelled the banner "CalDAV". Split into two separate toasts (CalDAV cloud-arrow + Relay broadcast icon), each only shown when its numbers are non-zero. Banners now stack vertically so both can appear together on a sync that pulled on both sides. - relay UI: the session chat view was 1:1-only. Group rooms (which the client API already supports) now surface as first-class threads in the sidebar — sorted chronologically with 1:1 peers, tagged "ROOM", with a users-three icon. Sending posts to `room_uid` instead of `recipient`; history shows the sender name above each incoming message so it's clear who's talking in the room. Relay backend: `/api/chat/threads` returns rooms alongside 1:1 threads; `/api/chat/history` accepts `?room_uid=`; `/api/chat/send` accepts `room_uid` in the body. - tests: bump "replace all updates card bodies" poll budget from 4s to 10s and poll both updated cards. Flaky on cold runs because a fresh PHP worker + SQLite WAL warm-up plus two sequential PATCH calls can just barely exceed 4 seconds. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4aca21812e
commit
87ebcd156b
6 files changed
+175
-51
No files matched your search
+88
-13
@@ -23,14 +23,16 @@ if ($route === '/api/chat/threads' && $method === 'GET' && $sessionTenant) {
|
||||
$interKey = getInterTenantKey($db, $MASTER_KEY);
|
||||
if (!$interKey) jsonResponse(['error' => 'relay super-shared key not set'], 500);
|
||||
$myId = $sessionTenant['id'];
|
||||
// 1:1 threads: my 1:1 messages grouped by peer username. Group sends
|
||||
// (room_uid IS NOT NULL) are excluded so they don't surface here.
|
||||
$stmt = $db->prepare('
|
||||
SELECT sender_username AS peer, MAX(created_at) AS last_at, COUNT(*) AS count
|
||||
FROM chat_messages WHERE recipient_id = :r
|
||||
FROM chat_messages WHERE recipient_id = :r AND room_uid IS NULL
|
||||
GROUP BY sender_username
|
||||
UNION
|
||||
SELECT t.username AS peer, MAX(cm.created_at) AS last_at, COUNT(*) AS count
|
||||
FROM chat_messages cm JOIN tenants t ON t.id = cm.recipient_id
|
||||
WHERE cm.sender_id = :s AND cm.recipient_id != :s2
|
||||
WHERE cm.sender_id = :s AND cm.recipient_id != :s2 AND cm.room_uid IS NULL
|
||||
GROUP BY t.username
|
||||
');
|
||||
$stmt->execute(['r' => $myId, 's' => $myId, 's2' => $myId]);
|
||||
@@ -42,29 +44,85 @@ if ($route === '/api/chat/threads' && $method === 'GET' && $sessionTenant) {
|
||||
$threads[$peer]['count'] += (int)$row['count'];
|
||||
if ($row['last_at'] > $threads[$peer]['last_at']) $threads[$peer]['last_at'] = $row['last_at'];
|
||||
}
|
||||
usort($threads, fn($a, $b) => strcmp($b['last_at'], $a['last_at']));
|
||||
// Add unread count per peer
|
||||
foreach ($threads as &$t) {
|
||||
$stmt2 = $db->prepare('SELECT COUNT(*) FROM chat_messages WHERE recipient_id = :r AND sender_username = :p AND seen = 0');
|
||||
$stmt2 = $db->prepare('SELECT COUNT(*) FROM chat_messages WHERE recipient_id = :r AND sender_username = :p AND seen = 0 AND room_uid IS NULL');
|
||||
$stmt2->execute(['r' => $myId, 'p' => $t['peer']]);
|
||||
$t['unread'] = (int)$stmt2->fetchColumn();
|
||||
}
|
||||
jsonResponse(array_values($threads));
|
||||
unset($t);
|
||||
// Group rooms the caller is a member of. The UI renders these as
|
||||
// first-class threads alongside 1:1 conversations; room_uid marks them
|
||||
// so the client can key off the right identifier.
|
||||
$roomStmt = $db->prepare('
|
||||
SELECT r.uid, r.name, r.owner_username,
|
||||
(SELECT MAX(created_at) FROM chat_messages cm WHERE cm.room_uid = r.uid) AS last_at,
|
||||
(SELECT COUNT(*) FROM chat_messages cm WHERE cm.room_uid = r.uid) AS count
|
||||
FROM chat_rooms r
|
||||
INNER JOIN chat_room_members m ON m.room_uid = r.uid
|
||||
WHERE m.tenant_id = :tid
|
||||
ORDER BY last_at DESC
|
||||
');
|
||||
$roomStmt->execute(['tid' => $myId]);
|
||||
$roomThreads = [];
|
||||
foreach ($roomStmt->fetchAll() as $r) {
|
||||
$roomThreads[] = [
|
||||
'room_uid' => $r['uid'],
|
||||
'name' => $r['name'],
|
||||
'owner' => $r['owner_username'],
|
||||
'last_at' => $r['last_at'] ?? '',
|
||||
'count' => (int)$r['count'],
|
||||
'unread' => 0, // rooms have no per-recipient seen state yet
|
||||
];
|
||||
}
|
||||
$combined = array_merge(array_values($threads), $roomThreads);
|
||||
usort($combined, fn($a, $b) => strcmp($b['last_at'] ?? '', $a['last_at'] ?? ''));
|
||||
jsonResponse($combined);
|
||||
}
|
||||
|
||||
if ($route === '/api/chat/history' && $method === 'GET' && $sessionTenant) {
|
||||
$peer = trim($_GET['peer'] ?? '');
|
||||
if (!$peer) jsonResponse(['error' => 'peer required'], 400);
|
||||
$peer = trim($_GET['peer'] ?? '');
|
||||
$roomUid = trim($_GET['room_uid'] ?? '');
|
||||
if ($peer === '' && $roomUid === '') jsonResponse(['error' => 'peer or room_uid required'], 400);
|
||||
$interKey = getInterTenantKey($db, $MASTER_KEY);
|
||||
if (!$interKey) jsonResponse(['error' => 'relay super-shared key not set'], 500);
|
||||
$myId = $sessionTenant['id'];
|
||||
|
||||
if ($roomUid !== '') {
|
||||
// Group room: caller must be a current member.
|
||||
$check = $db->prepare('SELECT 1 FROM chat_room_members WHERE room_uid = :r AND tenant_id = :t');
|
||||
$check->execute(['r' => $roomUid, 't' => $myId]);
|
||||
if (!$check->fetchColumn()) jsonResponse(['error' => 'not a member of this room'], 403);
|
||||
$stmt = $db->prepare('
|
||||
SELECT id, sender_id, sender_username, body, created_at
|
||||
FROM chat_messages
|
||||
WHERE room_uid = :r
|
||||
ORDER BY created_at ASC LIMIT 500
|
||||
');
|
||||
$stmt->execute(['r' => $roomUid]);
|
||||
$rows = $stmt->fetchAll();
|
||||
$out = [];
|
||||
foreach ($rows as $row) {
|
||||
$out[] = [
|
||||
'id' => (int)$row['id'],
|
||||
'from' => $row['sender_username'],
|
||||
'mine' => (int)$row['sender_id'] === $myId,
|
||||
'body' => clientDecrypt($row['body'] ?? '', $interKey),
|
||||
'created_at' => $row['created_at'],
|
||||
'seen' => 1, // group rooms don't track per-recipient seen
|
||||
];
|
||||
}
|
||||
jsonResponse($out);
|
||||
}
|
||||
|
||||
// 1:1 fallback
|
||||
$peerTenant = getTenantByUsername($db, $peer);
|
||||
if (!$peerTenant) jsonResponse(['error' => 'unknown peer'], 404);
|
||||
if (!$peerTenant) jsonResponse(['error' => 'peer not found'], 404);
|
||||
$peerId = $peerTenant['id'];
|
||||
$stmt = $db->prepare('
|
||||
SELECT id, sender_id, sender_username, body, created_at, seen
|
||||
FROM chat_messages
|
||||
WHERE (recipient_id = :r AND sender_id = :p) OR (recipient_id = :p2 AND sender_id = :s)
|
||||
WHERE room_uid IS NULL
|
||||
AND ((recipient_id = :r AND sender_id = :p) OR (recipient_id = :p2 AND sender_id = :s))
|
||||
ORDER BY created_at ASC LIMIT 500
|
||||
');
|
||||
$stmt->execute(['r' => $myId, 'p' => $peerId, 'p2' => $peerId, 's' => $myId]);
|
||||
@@ -90,13 +148,30 @@ if ($route === '/api/chat/send' && $method === 'POST' && $sessionTenant) {
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?: [];
|
||||
$body = trim((string)($input['body'] ?? ''));
|
||||
$recipientName = trim((string)($input['recipient'] ?? ''));
|
||||
$roomUid = trim((string)($input['room_uid'] ?? ''));
|
||||
if ($body === '') jsonResponse(['error' => 'body required'], 400);
|
||||
if ($recipientName === '') jsonResponse(['error' => 'recipient required'], 400);
|
||||
$recipient = getTenantByUsername($db, $recipientName);
|
||||
if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
|
||||
if ($recipientName === '' && $roomUid === '') jsonResponse(['error' => 'recipient or room_uid required'], 400);
|
||||
$interKey = getInterTenantKey($db, $MASTER_KEY);
|
||||
if (!$interKey) jsonResponse(['error' => 'relay super-shared key not set'], 500);
|
||||
$stored = clientEncrypt($body, $interKey);
|
||||
|
||||
if ($roomUid !== '') {
|
||||
// Group post: verify membership. `recipient_id` NOT NULL because of
|
||||
// the FK, so we park the sender's own id there — GET filters on
|
||||
// room_uid, never recipient_id, for group rows.
|
||||
$check = $db->prepare('SELECT 1 FROM chat_room_members WHERE room_uid = :r AND tenant_id = :t');
|
||||
$check->execute(['r' => $roomUid, 't' => $sessionTenant['id']]);
|
||||
if (!$check->fetchColumn()) jsonResponse(['error' => 'not a member of this room'], 403);
|
||||
$db->prepare('INSERT INTO chat_messages (recipient_id, sender_id, sender_username, room_uid, body, persistent) VALUES (:me, :s, :sn, :ru, :b, 1)')
|
||||
->execute([
|
||||
'me' => $sessionTenant['id'], 's' => $sessionTenant['id'],
|
||||
'sn' => $sessionTenant['username'], 'ru' => $roomUid, 'b' => $stored,
|
||||
]);
|
||||
jsonResponse(['ok' => true, 'id' => (int)$db->lastInsertId(), 'room_uid' => $roomUid]);
|
||||
}
|
||||
|
||||
$recipient = getTenantByUsername($db, $recipientName);
|
||||
if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
|
||||
$db->prepare('INSERT INTO chat_messages (recipient_id, sender_id, sender_username, body) VALUES (:r, :s, :sn, :b)')
|
||||
->execute(['r' => $recipient['id'], 's' => $sessionTenant['id'], 'sn' => $sessionTenant['username'], 'b' => $stored]);
|
||||
jsonResponse(['ok' => true, 'id' => (int)$db->lastInsertId()]);
|
||||
|
||||
Reference in new issue
Block a user