From c1ccfd2998031235b178dcfb7f77cc5c290b4852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 1 Mar 2012 22:11:20 +0100 Subject: [PATCH] Moving user fetching into ChatRoom Untested commit incoming --- file/lib/data/chat/room/ChatRoom.class.php | 77 +++++++++++++++++++--- file/lib/page/ChatMessagePage.class.php | 33 +--------- 2 files changed, 69 insertions(+), 41 deletions(-) diff --git a/file/lib/data/chat/room/ChatRoom.class.php b/file/lib/data/chat/room/ChatRoom.class.php index ee66354..1b8095b 100644 --- a/file/lib/data/chat/room/ChatRoom.class.php +++ b/file/lib/data/chat/room/ChatRoom.class.php @@ -29,6 +29,36 @@ class ChatRoom extends \wcf\data\DatabaseObject implements \wcf\system\request\I */ protected static $cache = null; + /** + * @see \wcf\data\chat\room\ChatRoom::getTitle(); + */ + public function __toString() { + return $this->getTitle(); + } + + /** + * Returns the number of users currently active in this room. + * + * @return integer + */ + public function countUsers() { + $packageID = \wcf\system\package\PackageDependencyHandler::getPackageID('timwolla.wcf.chat'); + + $sql = "SELECT + count(*) as count + FROM + wcf".WCF_N."_user_storage + WHERE + field = 'roomID' + AND packageID = ".intval($packageID)." + AND fieldValue = ".intval($this->roomID); + $stmt = WCF::getDB()->prepareStatement($sql); + $stmt->execute(); + $row = $stmt->fetchArray(); + + return $row['count']; + } + /** * Loads the room cache. */ @@ -37,7 +67,7 @@ public static function getCache() { CacheHandler::getInstance()->addResource( 'chatrooms', WCF_DIR.'cache/cache.chatrooms.php', - 'wcf\system\cache\builder\ChatRoomCacheBuilder' + '\wcf\system\cache\builder\ChatRoomCacheBuilder' ); self::$cache = CacheHandler::getInstance()->get('chatrooms'); } @@ -46,10 +76,12 @@ public static function getCache() { } /** - * @see \wcf\data\chat\room\ChatRoom::getTitle(); + * Returns the ID of this chat-room. + * + * @see \wcf\system\request\IRouteController */ - public function __toString() { - return $this->getTitle(); + public function getID() { + return $this->roomID; } /** @@ -62,12 +94,39 @@ public function getTitle() { } /** - * Returns the ID of this chat-room. - * - * @see \wcf\system\request\RRouteHandler + * Returns the users that are currently active in this room. + * + * @return array<\wcf\data\user\User> */ - public function getID() { - return $this->roomID; + public function getUsers() { + $packageID = \wcf\system\package\PackageDependencyHandler::getPackageID('timwolla.wcf.chat'); + + $sql = "SELECT + userID + FROM + wcf".WCF_N."_user_storage + WHERE + field = 'roomID' + AND packageID = ".intval($packageID)." + AND fieldValue = ".intval($this->roomID); + $stmt = WCF::getDB()->prepareStatement($sql); + $stmt->execute(); + $userIDs = array(); + while ($row = $stmt->fetchArray()) $userIDs[] = $row['userID']; + + if (!count($userIDs)) return; + + $sql = "SELECT + * + FROM + wcf".WCF_N."_user + WHERE + userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').") + ORDER BY + username ASC"; + $stmt = WCF::getDB()->prepareStatement($sql); + $stmt->execute($userIDs); + return $stmt->fetchObjects('\wcf\data\user\User'); } /** diff --git a/file/lib/page/ChatMessagePage.class.php b/file/lib/page/ChatMessagePage.class.php index de5d9b3..81ac24a 100644 --- a/file/lib/page/ChatMessagePage.class.php +++ b/file/lib/page/ChatMessagePage.class.php @@ -29,7 +29,7 @@ public function readData() { $this->readRoom(); $this->readMessages(); - $this->readUsers(); + $this->users = $this->room->getUsers(); } public function readMessages() { @@ -54,37 +54,6 @@ public function readRoom() { if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException(); } - public function readUsers() { - $packageID = \wcf\system\package\PackageDependencyHandler::getPackageID('timwolla.wcf.chat'); - - $sql = "SELECT - userID - FROM - wcf".WCF_N."_user_storage - WHERE - field = 'roomID' - AND packageID = ".intval($packageID)." - AND fieldValue = ".intval($this->roomID); - $stmt = WCF::getDB()->prepareStatement($sql); - $stmt->execute(); - $userIDs = array(); - while ($row = $stmt->fetchArray()) $userIDs[] = $row['userID']; - - if (!count($userIDs)) return; - - $sql = "SELECT - * - FROM - wcf".WCF_N."_user - WHERE - userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').") - ORDER BY - username ASC"; - $stmt = WCF::getDB()->prepareStatement($sql); - $stmt->execute($userIDs); - $this->users = $stmt->fetchObjects('\wcf\data\user\User'); - } - /** * @see \wcf\page\IPage::show() */