From 8cd3d190e6b9fef29a49ea53459df493b38b0560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Thu, 25 Sep 2025 23:00:41 +0200 Subject: [PATCH] Fix scroll behavior and prevent history loading when the conversation is empty --- app/Widgets/Chat/Chat.php | 9 +++------ app/Widgets/Chat/chat.js | 13 ++++++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/Widgets/Chat/Chat.php b/app/Widgets/Chat/Chat.php index 283341bd4..e8514aee6 100644 --- a/app/Widgets/Chat/Chat.php +++ b/app/Widgets/Chat/Chat.php @@ -1012,7 +1012,8 @@ class Chat extends \Movim\Widget\Base ->first(); if ($contextMessage) { - $this->ajaxGetHistory($jid, $contextMessage->published, muc: $contextMessage->isMuc(), prepend: false, tryMam: false, clear: true); + $this->rpc('MovimTpl.fill', '#' . cleanupId($jid) . '-conversation', ''); + $this->ajaxGetHistory($jid, $contextMessage->published, muc: $contextMessage->isMuc(), prepend: false, tryMam: false); $this->rpc('Chat.scrollAndBlinkMessageMid', $mid); $this->rpc('MovimUtils.addClass', '#chat_widget .contained', 'history'); } @@ -1024,7 +1025,7 @@ class Chat extends \Movim\Widget\Base * @param string jid * @param string time */ - public function ajaxGetHistory(string $jid, ?string $date = null, bool $muc = false, bool $prepend = true, bool $tryMam = true, ?bool $clear = false) + public function ajaxGetHistory(string $jid, ?string $date = null, bool $muc = false, bool $prepend = true, bool $tryMam = true) { if (!validateJid($jid)) return; @@ -1048,10 +1049,6 @@ class Chat extends \Movim\Widget\Base $this->prepareMessage($message); } - if ($clear) { - $this->rpc('MovimTpl.fill', '#' . cleanupId($jid) . '-conversation', ''); - } - $this->rpc('Chat.appendMessagesWrapper', $this->_wrapper, $prepend); $this->_wrapper = []; } diff --git a/app/Widgets/Chat/chat.js b/app/Widgets/Chat/chat.js index 810f9bdcf..637309e06 100644 --- a/app/Widgets/Chat/chat.js +++ b/app/Widgets/Chat/chat.js @@ -658,10 +658,11 @@ var Chat = { }, setScrollBehaviour: function () { var discussion = Chat.getDiscussion(); - if (discussion == null) return; + if (!discussion) return; discussion.onscroll = function () { - if (this.scrollTop < 1) { + // Don't get more history the conversation is already empty + if (discussion.querySelector('.conversation').innerHTML != '' && this.scrollTop < 100) { Chat.getHistory(true); } @@ -672,7 +673,7 @@ var Chat = { }, setScroll: function () { var discussion = Chat.getDiscussion(); - if (discussion == null) return; + if (!discussion) return; Chat.lastHeight = discussion.scrollHeight; Chat.lastScroll = discussion.scrollTop + discussion.clientHeight; @@ -700,7 +701,7 @@ var Chat = { }, scrollTotally: function () { var discussion = Chat.getDiscussion(); - if (discussion == null) return; + if (!discussion) return; discussion.scrollTop = discussion.scrollHeight; }, @@ -722,7 +723,7 @@ var Chat = { }, scrollToSeparator: function () { var discussion = Chat.getDiscussion(); - if (discussion == null) return; + if (!discussion) return; var separator = discussion.querySelector('.separator'); if (separator) { @@ -1273,6 +1274,8 @@ var Chat = { appendDate: function (date, prepend) { var list = document.querySelector('#chat_widget > div ul.conversation'); + if (!list) return; + dateNode = Chat.date.cloneNode(true); dateNode.querySelector('p').innerHTML = date; dateNode.id = MovimUtils.cleanupId(date);