1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00

Show user count in room list

Closes #27
This commit is contained in:
Tim Düsterhus 2013-08-19 22:14:59 +02:00
parent 8b8bf4649c
commit 46d86de030
4 changed files with 27 additions and 28 deletions

View File

@ -651,7 +651,7 @@ Fetch the roomlist from the server and update it in the GUI.
for room in data.returnValues
li = $ '<li></li>'
li.addClass 'active' if room.active
$("""<a href="#{room.link}">#{room.title}</a>""").addClass('timsChatRoom').data('roomID', room.roomID).appendTo li
$("""<a href="#{room.link}">#{room.title} <span class="badge">#{WCF.String.formatNumeric room.userCount}</span></a>""").addClass('timsChatRoom').data('roomID', room.roomID).appendTo li
$('#timsChatRoomList ul').append li
if window.history?.replaceState?

View File

@ -23,6 +23,13 @@ class Room extends \chat\data\CHATDatabaseObject implements \wcf\system\request\
*/
protected static $databaseTableIndexName = 'roomID';
/**
* cached users
*
* @var array<\wcf\data\user\UserProfile>
*/
protected static $users = null;
/**
* @see \wcf\data\chat\room\ChatRoom::getTitle();
*/
@ -133,36 +140,27 @@ public function getTopic() {
return \wcf\system\WCF::getLanguage()->get($this->topic);
}
/**
* Returns the number of users currently active in this room.
*
* @return integer
*/
public function countUsers() {
$sql = "SELECT
COUNT(*)
FROM
wcf".WCF_N."_user
WHERE
chatRoomID = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array($this->roomID));
return $stmt->fetchColumn();
}
/**
* Returns the users that are currently active in this room.
*
* @return \wcf\data\user\UserProfileList
* @return array<\wcf\data\user\UserProfile>
*/
public function getUsers() {
$userList = new \wcf\data\user\UserProfileList();
$userList->getConditionBuilder()->add('user_table.chatRoomID = ?', array($this->roomID));
if (self::$users === null) {
$userList = new \wcf\data\user\UserProfileList();
$userList->getConditionBuilder()->add('user_table.chatRoomID IS NOT NULL', array());
$userList->readObjects();
$users = $userList->getObjects();
foreach ($users as $user) {
if (!isset(self::$users[$user->chatRoomID])) self::$users[$user->chatRoomID] = array();
self::$users[$user->chatRoomID][] = $user;
}
}
if (!isset(self::$users[$this->roomID])) self::$users[$this->roomID] = array();
$userList->readObjects();
return $userList;
return self::$users[$this->roomID];
}
/**

View File

@ -149,7 +149,8 @@ public function getRoomList() {
'object' => $room
)),
'roomID' => $room->roomID,
'active' => $room->roomID == $this->parameters['room']->roomID
'active' => $room->roomID == $this->parameters['room']->roomID,
'userCount' => count($room->getUsers())
);
}

View File

@ -46,7 +46,7 @@ class NewMessagesPage extends \wcf\page\AbstractPage {
/**
* All the users that are currently in the room $this->room.
*
* @var array<\wcf\data\user\User>
* @var array<\wcf\data\user\UserProfile>
*/
public $users = array();
@ -114,7 +114,7 @@ public function show() {
$json['messages'][] = $message->jsonify(true);
}
\wcf\system\user\storage\UserStorageHandler::getInstance()->loadStorage(array_keys($this->users->getObjects()));
\wcf\system\user\storage\UserStorageHandler::getInstance()->loadStorage(array_keys($this->users));
foreach ($this->users as $user) {
$json['users'][] = array(