From: Marcel Werk Date: Mon, 19 Feb 2024 16:29:14 +0000 (+0100) Subject: Made `ConversationHandler` less error-prone X-Git-Tag: 6.1.0_Alpha_1~14^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e23349a09a6c32f5aa978d93365303c2744594d1;p=GitHub%2FWoltLab%2Fcom.woltlab.wcf.conversation.git Made `ConversationHandler` less error-prone The number of unread conversations is often shown in the templates. If this is accidentally queried for a guest due to individual template changes, it leads directly to an error message. --- diff --git a/files/lib/system/conversation/ConversationHandler.class.php b/files/lib/system/conversation/ConversationHandler.class.php index 210622f..da0befc 100644 --- a/files/lib/system/conversation/ConversationHandler.class.php +++ b/files/lib/system/conversation/ConversationHandler.class.php @@ -13,37 +13,37 @@ use wcf\system\WCF; /** * Handles the number of conversations and unread conversations of the active user. * - * @author Marcel Werk - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License + * @author Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License */ -class ConversationHandler extends SingletonFactory +final class ConversationHandler extends SingletonFactory { /** * number of unread conversations * @var int[] */ - protected $unreadConversationCount = []; + private array $unreadConversationCount = []; /** * number of conversations * @var int[] */ - protected $conversationCount = []; + private array $conversationCount = []; /** * Returns the number of unread conversations for given user. - * - * @param int $userID - * @param bool $skipCache - * @return int */ - public function getUnreadConversationCount($userID = null, $skipCache = false) + public function getUnreadConversationCount(?int $userID = null, bool $skipCache = false): int { if ($userID === null) { $userID = WCF::getUser()->userID; } + if (!$userID) { + return 0; + } + if (!isset($this->unreadConversationCount[$userID]) || $skipCache) { $this->unreadConversationCount[$userID] = 0; @@ -87,16 +87,17 @@ class ConversationHandler extends SingletonFactory /** * Returns the number of conversations for given user. - * - * @param int $userID - * @return int */ - public function getConversationCount($userID = null) + public function getConversationCount(?int $userID = null): int { if ($userID === null) { $userID = WCF::getUser()->userID; } + if (!$userID) { + return 0; + } + if (!isset($this->conversationCount[$userID])) { $this->conversationCount[$userID] = 0; @@ -151,7 +152,7 @@ class ConversationHandler extends SingletonFactory */ public function enforceFloodControl( bool $isReply = false - ) { + ): void { if (!$isReply) { // 1. Check for the maximum conversations per 24 hours. $limit = WCF::getSession()->getPermission('user.conversation.maxStartedConversationsPer24Hours');