From 1db07b5f58823c463d0aca94b283963b093f5850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Sun, 7 Sep 2025 12:32:56 +0200 Subject: [PATCH] Refactor the message loading flow --- app/Message.php | 8 ++-- app/Widgets/Chat/Chat.php | 91 +++++++++++++------------------------ app/Widgets/Chat/_chat.tpl | 2 +- app/Widgets/Chat/chat.js | 91 ++++++++++++++++++------------------- app/Widgets/Chats/Chats.php | 5 +- 5 files changed, 85 insertions(+), 112 deletions(-) diff --git a/app/Message.php b/app/Message.php index bdde491c1..959f8bb18 100644 --- a/app/Message.php +++ b/app/Message.php @@ -51,12 +51,12 @@ class Message extends Model ]; public const MESSAGE_TYPE_MUC = [ 'groupchat', - 'muji_propose', - 'muji_retract', - 'muc_owner', 'muc_admin', + 'muc_member', 'muc_outcast', - 'muc_member' + 'muc_owner', + 'muji_propose', + 'muji_retract', ]; public static function boot() diff --git a/app/Widgets/Chat/Chat.php b/app/Widgets/Chat/Chat.php index 14689437a..6cd112188 100644 --- a/app/Widgets/Chat/Chat.php +++ b/app/Widgets/Chat/Chat.php @@ -155,7 +155,7 @@ class Chat extends \Movim\Widget\Base ->where('mucjid', $this->user->id) ->first(); - $this->prepareMessages($jid, ($presence), true); + $this->getMessages($jid, muc: ($presence), seenOnly: true); } } @@ -176,20 +176,10 @@ class Chat extends \Movim\Widget\Base $rawbody = $message->getInlinedBodyAttribute(true) ?? $message->body; if ( - $message->isEmpty() && !in_array($message->type, [ - 'jingle_end', - 'jingle_finish', - 'jingle_incoming', - 'jingle_outgoing', - 'jingle_reject', - 'jingle_retract', - 'muc_admin', - 'muc_member', - 'muc_outcast', - 'muc_owner', - 'muji_propose', - 'muji_retract', - ]) + $message->isEmpty() && !in_array( + $message->type, + array_merge(Message::MESSAGE_TYPE, Message::MESSAGE_TYPE_MUC) + ) ) { return; } @@ -379,24 +369,19 @@ class Chat extends \Movim\Widget\Base public function ajaxInit() { $view = $this->tpl(); - $date = $view->draw('_chat_date'); - $separator = $view->draw('_chat_separator'); - $this->rpc('Chat.resetCurrentDateTime'); - $this->rpc('Chat.setGeneralElements', $date, $separator); $this->rpc( - 'Chat.setConfig', - $this->_pagination, - $this->__('message.error'), - $this->__('chat.action_impossible_encrypted') + 'Chat.init', + $view->draw('_chat_date'), + $view->draw('_chat_separator'), + [ + 'pagination' => $this->_pagination, + 'delivery_error' => $this->__('message.error'), + 'action_impossible_encrypted_error' => $this->__('chat.action_impossible_encrypted') + ] ); } - public function ajaxClearCounter(string $jid) - { - $this->prepareMessages($jid, false, true, false); - } - /** * Get the header */ @@ -449,8 +434,8 @@ class Chat extends \Movim\Widget\Base } $this->rpc('Chat.setObservers'); - $this->rpc('Chat.resetCurrentDateTime'); - $this->prepareMessages($jid); + $this->rpc('Chat.resetOldestMessageDateTime'); + $this->getMessages($jid); $this->rpc('Notif.current', 'chat|' . $jid); $this->rpc('Chat.scrollToSeparator'); @@ -495,8 +480,8 @@ class Chat extends \Movim\Widget\Base } $this->rpc('Chat.setObservers'); - $this->rpc('Chat.resetCurrentDateTime'); // TODO, not call there all the time ?! - $this->prepareMessages($room, true); + $this->rpc('Chat.resetOldestMessageDateTime'); // TODO, not call there all the time ?! + $this->getMessages($room, muc: true); $this->rpc('Notif.current', 'chat|' . $room); $this->rpc('Chat.scrollToSeparator'); @@ -1008,7 +993,7 @@ class Chat extends \Movim\Widget\Base } /** - * @brief Get a specific message contect + * @brief Get a specific message context */ public function ajaxGetMessageContext(string $jid, int $mid) { @@ -1026,31 +1011,10 @@ class Chat extends \Movim\Widget\Base ->first(); if ($contextMessage) { - $messages = \App\Message::jid($jid) - ->where('published', '>=', $contextMessage->published); - - $messages = $contextMessage->isMuc() - ? $messages->whereIn('type', Message::MESSAGE_TYPE_MUC)->whereNull('subject') - : $messages->whereIn('type', Message::MESSAGE_TYPE); - - $messages = $messages->orderBy('published', 'asc') - ->withCount('reactions') - ->take($this->_pagination) - ->get(); - - if ($messages->count() > 0) { - $messages = $messages->reverse(); - - foreach ($messages as $message) { - $this->prepareMessage($message); - } - - $this->rpc('MovimTpl.fill', '#' . cleanupId($jid) . '-conversation', ''); - $this->rpc('MovimUtils.addClass', '#scroll_now.button.action', 'show'); - $this->rpc('Chat.appendMessagesWrapper', $this->_wrapper, true); - $this->rpc('Chat.scrollAndBlinkMessageMid', $mid); - $this->_wrapper = []; - } + $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', '#scroll_now.button.action', 'show'); } } @@ -1238,7 +1202,14 @@ class Chat extends \Movim\Widget\Base return $view->draw('_chat'); } - public function prepareMessages($jid, $muc = false, $seenOnly = false, $event = true) + public function ajaxClearAndGetMessages(string $jid, $muc = false) + { + $this->rpc('MovimTpl.fill', '#' . cleanupId($jid) . '-conversation', ''); + $this->getMessages($jid, $muc); + $this->rpc('MovimUtils.removeClass', '#scroll_now.button.action', 'show'); + } + + public function getMessages(string $jid, $muc = false, $seenOnly = false, $event = true) { if (!validateJid($jid)) { return; @@ -1336,7 +1307,7 @@ class Chat extends \Movim\Widget\Base } } - public function prepareMessage(&$message, $jid = null) + public function prepareMessage(&$message, $jid = null): array { if ($jid != $message->jidto && $jid != $message->jidfrom && $jid != null) { return $this->_wrapper; diff --git a/app/Widgets/Chat/_chat.tpl b/app/Widgets/Chat/_chat.tpl index 4c1e01a39..e7a6c0656 100644 --- a/app/Widgets/Chat/_chat.tpl +++ b/app/Widgets/Chat/_chat.tpl @@ -32,7 +32,7 @@
presence && $conference->presence->mucrole == 'visitor'"}disabled{/if}"> - + clock_arrow_down