1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00

Moving user fetching into ChatRoom

Untested commit incoming
This commit is contained in:
Tim Düsterhus 2012-03-01 22:11:20 +01:00
parent 4e605993df
commit c1ccfd2998
2 changed files with 69 additions and 41 deletions

View File

@ -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');
}
/**

View File

@ -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()
*/