From 54e5e450c9b064efd5bc45c4c91bc60e75c9faa9 Mon Sep 17 00:00:00 2001 From: RevertIT <2redact@gmail.com> Date: Wed, 3 Apr 2024 00:29:58 +0200 Subject: [PATCH] Add placeholder for statistics modal when no messages are found Remove redundant checks Fix encoding issue Change let to const --- .../Chat/ChatHandler/AbstractChatHandler.php | 12 +----- inc/plugins/rt/src/Chat/ChatHandler/Read.php | 4 +- inc/plugins/rt/src/Chat/Core.php | 2 +- inc/plugins/rt/src/Chat/Hooks/Frontend.php | 43 +++++++++++-------- jscripts/rt_chat.js | 2 +- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/inc/plugins/rt/src/Chat/ChatHandler/AbstractChatHandler.php b/inc/plugins/rt/src/Chat/ChatHandler/AbstractChatHandler.php index dcff3b8..c1db112 100644 --- a/inc/plugins/rt/src/Chat/ChatHandler/AbstractChatHandler.php +++ b/inc/plugins/rt/src/Chat/ChatHandler/AbstractChatHandler.php @@ -91,18 +91,8 @@ protected function getError(): array|bool */ protected function renderTemplate(int $messageId, int $uid, int $touid = 0, string $message, int $dateline): bool|array { - if (empty($messageId)) - { - return false; - } - $user = get_user($uid); - if (empty($user)) - { - return false; - } - // Parse bbcodes $parser_options = [ "allow_html" => 0, @@ -129,7 +119,7 @@ protected function renderTemplate(int $messageId, int $uid, int $touid = 0, stri $row['date'] = my_date('relative', $dateline); $row['avatar'] = !empty($this->mybb->user['avatar']) ? htmlspecialchars_uni($this->mybb->user['avatar']) : "{$this->mybb->settings['bburl']}/images/default_avatar.png"; $row['username'] = isset($user['uid'], $user['username'], $user['usergroup'], $user['displaygroup']) ? build_profile_link(format_name($user['username'], $user['usergroup'], $user['displaygroup']), $user['uid']) : $this->lang->na; - $row['original_message'] = base64_encode(htmlspecialchars_uni($message)); + $row['original_message'] = base64_encode($message); $row['message'] = $this->parser->parse_message($message, $parser_options); // Action bar diff --git a/inc/plugins/rt/src/Chat/ChatHandler/Read.php b/inc/plugins/rt/src/Chat/ChatHandler/Read.php index e33ccf1..1e8b4dc 100644 --- a/inc/plugins/rt/src/Chat/ChatHandler/Read.php +++ b/inc/plugins/rt/src/Chat/ChatHandler/Read.php @@ -115,7 +115,7 @@ public function getMessages(array $loadedMessages = []): mixed $row['dateline'] = $row['dateline'] ?? TIME_NOW; $row['avatar'] = !empty($row['avatar']) ? htmlspecialchars_uni($row['avatar']) : "{$this->mybb->settings['bburl']}/images/default_avatar.png"; $row['username'] = isset($row['uid'], $row['username'], $row['usergroup'], $row['displaygroup']) ? build_profile_link(format_name($row['username'], $row['usergroup'], $row['displaygroup']), $row['uid']) : $this->lang->na; - $row['original_message'] = isset($row['message']) ? base64_encode(htmlspecialchars_uni($row['message'])) : null; + $row['original_message'] = isset($row['message']) ? base64_encode($row['message']) : null; $row['message'] = isset($row['message']) ? $this->parser->parse_message($row['message'], $parser_options) : null; $rt_chat_whisper = ''; @@ -229,7 +229,7 @@ public function getMessageBeforeId(int $messageId): array $row['dateline'] = $row['dateline'] ?? TIME_NOW; $row['avatar'] = !empty($row['avatar']) ? htmlspecialchars_uni($row['avatar']) : "{$this->mybb->settings['bburl']}/images/default_avatar.png"; $row['username'] = isset($row['uid'], $row['username'], $row['usergroup'], $row['displaygroup']) ? build_profile_link(format_name($row['username'], $row['usergroup'], $row['displaygroup']), $row['uid']) : $this->lang->na; - $row['original_message'] = isset($row['message']) ? base64_encode(htmlspecialchars_uni($row['message'])) : null; + $row['original_message'] = isset($row['message']) ? base64_encode($row['message']) : null; $row['message'] = isset($row['message']) ? $this->parser->parse_message($row['message'], $parser_options) : null; $rt_chat_whisper = ''; diff --git a/inc/plugins/rt/src/Chat/Core.php b/inc/plugins/rt/src/Chat/Core.php index d3dd87e..0ae270a 100644 --- a/inc/plugins/rt/src/Chat/Core.php +++ b/inc/plugins/rt/src/Chat/Core.php @@ -23,7 +23,7 @@ class Core 'description' => 'RT Chat is a modern and responsive MyBB chat plugin which utilizes MyBB cache system when retrieving messages via ajax.', 'author' => 'RevertIT', 'authorsite' => 'https://github.com/RevertIT/', - 'version' => '1.4', + 'version' => '1.5', 'compatibility' => '18*', 'codename' => 'rt_chat', 'prefix' => 'rt_chat', diff --git a/inc/plugins/rt/src/Chat/Hooks/Frontend.php b/inc/plugins/rt/src/Chat/Hooks/Frontend.php index 0ee47ed..a584eb2 100644 --- a/inc/plugins/rt/src/Chat/Hooks/Frontend.php +++ b/inc/plugins/rt/src/Chat/Hooks/Frontend.php @@ -126,23 +126,32 @@ public function misc_start(): void if ($mybb->get_input('action') === 'statistics') { $top5 = $rt_cache->query(" - SELECT - COUNT(message) AS total_messages, - c.uid, - u.username, - u.usergroup, - u.displaygroup - FROM - ".TABLE_PREFIX."rtchat c - LEFT JOIN ".TABLE_PREFIX."users u ON - u.uid = c.uid - GROUP BY - u.uid - ORDER BY - total_messages - DESC - LIMIT 10; - ")->cache('top_10_posters', 1800)->execute(); + SELECT + COUNT(message) AS total_messages, + c.uid, + u.username, + u.usergroup, + u.displaygroup + FROM + ".TABLE_PREFIX."rtchat c + LEFT JOIN ".TABLE_PREFIX."users u ON + u.uid = c.uid + GROUP BY + u.uid + ORDER BY + total_messages + DESC + LIMIT 10; + ")->cache('top_10_posters', 1800)->execute(); + + // Placeholder when no messages found in chat + if (empty($top5)) + { + $top5[] = [ + 'username' => $lang->na, + 'total_messages' => 0 + ]; + } $rt_chat_statistics_row = ''; foreach ($top5 as $row) diff --git a/jscripts/rt_chat.js b/jscripts/rt_chat.js index 642a913..2ad52fc 100644 --- a/jscripts/rt_chat.js +++ b/jscripts/rt_chat.js @@ -10,7 +10,7 @@ * @license http://opensource.org/licenses/mit-license.php MIT license */ -let RT_Chat = +const RT_Chat = { oldestMessageId: null, isActive: true,