fix(messages): polling fallback was using OLDEST message instead of NEWEST - caused '6 sty' date bug
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

Root cause: data.messages is sorted newest-first (DESC), but code took last element (oldest). Now properly filters for new messages only and updates with the newest.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-03-27 15:02:17 +01:00
parent c0d9e6f874
commit 577d3df206
2 changed files with 33 additions and 14 deletions

View File

@ -1492,21 +1492,40 @@
if (!state.currentConversationId) return;
var convId = state.currentConversationId;
var msgs = state.messages[convId];
var lastId = msgs && msgs.length ? msgs[msgs.length - 1].id : 0;
api('/api/conversations/' + convId + '/messages?after_id=' + lastId)
if (!msgs || !msgs.length) return;
// Find the newest message ID we already have
var newestId = 0;
msgs.forEach(function (m) { if (m.id > newestId) newestId = m.id; });
// Fetch only the latest page — API returns newest first
api('/api/conversations/' + convId + '/messages')
.then(function (data) {
if (data.messages && data.messages.length > 0) {
data.messages.forEach(function (msg) {
if (!msgs.some(function (m) { return m.id === msg.id; })) {
ChatView.appendMessage(msg);
state.messages[convId].push(msg);
}
if (!data.messages || !data.messages.length) return;
// Find messages newer than what we have
var newMsgs = data.messages.filter(function (msg) {
return msg.id > newestId;
});
if (newMsgs.length > 0) {
// Sort oldest first for appending
newMsgs.sort(function (a, b) { return a.id - b.id; });
newMsgs.forEach(function (msg) {
ChatView.appendMessage(msg);
state.messages[convId].push(msg);
});
// Update conversation list
var lastMsg = data.messages[data.messages.length - 1];
// Update conversation list with the NEWEST message
var latestMsg = newMsgs[newMsgs.length - 1];
ConversationList.updateConversation(convId, {
last_message: lastMsg,
updated_at: lastMsg.created_at
last_message: {
id: latestMsg.id,
content_preview: stripHtml(latestMsg.content || '').substring(0, 100),
sender_name: latestMsg.sender ? latestMsg.sender.name : '',
created_at: latestMsg.created_at,
},
updated_at: latestMsg.created_at,
});
}
})

View File

@ -5,7 +5,7 @@
{% block head_extra %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/quill.snow.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/conversations.css') }}?v=2">
<link rel="stylesheet" href="{{ url_for('static', filename='css/conversations.css') }}?v=3">
<script src="{{ url_for('static', filename='js/vendor/quill.js') }}"></script>
<style>
footer { display: none !important; }
@ -245,7 +245,7 @@ window.__CSRF_TOKEN__ = '{{ csrf_token() }}';
// Load conversations.js after data is set
(function() {
var s = document.createElement('script');
s.src = '{{ url_for("static", filename="js/conversations.js") }}?v=2';
s.src = '{{ url_for("static", filename="js/conversations.js") }}?v=3';
document.body.appendChild(s);
})();
{% endblock %}