aboutsummaryrefslogtreecommitdiff
path: root/api/chat/poll.php
diff options
context:
space:
mode:
Diffstat (limited to 'api/chat/poll.php')
-rw-r--r--api/chat/poll.php13
1 files changed, 10 insertions, 3 deletions
diff --git a/api/chat/poll.php b/api/chat/poll.php
index 601beca..b4aa970 100644
--- a/api/chat/poll.php
+++ b/api/chat/poll.php
@@ -14,14 +14,21 @@ $all = chat_read($thread);
$messages = $since > 0
? array_values(array_filter($all, fn($m) => (int)($m['epoch'] ?? 0) > $since))
: $all;
-$lastAll = $all ? end($all) : null;
+
+$window = array_slice($messages, -50);
+$truncated = count($messages) > count($window);
+// The cursor is the last message the client is actually being handed. Returning the
+// thread's real tail after a truncation would skip everything in between for good.
+$lastSent = $window ? $window[count($window) - 1] : null;
+$lastAll = $all ? $all[count($all) - 1] : null;
json_response([
'success' => true,
'thread' => $thread,
- 'messages' => array_slice($messages, -50),
+ 'messages' => $window,
+ 'truncated' => $truncated,
// `awaiting` replaces `unread`: the server keeps no read state, so an unread
// counter could never clear. Who spoke last is knowable.
'awaiting' => (bool)($lastAll && ($lastAll['author'] ?? '') !== 'hermes'),
- 'cursor' => $lastAll ? (int)($lastAll['epoch'] ?? 0) : 0,
+ 'cursor' => $lastSent ? (int)($lastSent['epoch'] ?? 0) : ($lastAll ? (int)($lastAll['epoch'] ?? 0) : 0),
]);