mirror of
https://github.com/wbbaddons/Tims-Chat.git
synced 2024-12-22 21:40:08 +00:00
Improve suspended status in JSON-answer, include benchmark
This commit is contained in:
parent
c3ecf87746
commit
d45c8e3a63
@ -43,9 +43,11 @@ public function __toString() {
|
|||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function canEnter() {
|
public function canEnter(\wcf\data\user\User $user = null) {
|
||||||
$ph = \wcf\system\chat\permission\ChatPermissionHandler::getInstance();
|
if ($user === null) $user = WCF::getUser();
|
||||||
$suspensions = ChatSuspension::getSuspensionsForUser();
|
|
||||||
|
$ph = new \wcf\system\chat\permission\ChatPermissionHandler($user);
|
||||||
|
$suspensions = ChatSuspension::getSuspensionsForUser($user);
|
||||||
|
|
||||||
$canEnter = $ph->getPermission($this, 'user.canEnter');
|
$canEnter = $ph->getPermission($this, 'user.canEnter');
|
||||||
// room suspension
|
// room suspension
|
||||||
@ -70,9 +72,11 @@ public function canEnter() {
|
|||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function canWrite() {
|
public function canWrite(\wcf\data\user\User $user = null) {
|
||||||
$ph = \wcf\system\chat\permission\ChatPermissionHandler::getInstance();
|
if ($user === null) $user = WCF::getUser();
|
||||||
$suspensions = ChatSuspension::getSuspensionsForUser();
|
|
||||||
|
$ph = new \wcf\system\chat\permission\ChatPermissionHandler($user);
|
||||||
|
$suspensions = ChatSuspension::getSuspensionsForUser($user);
|
||||||
|
|
||||||
$canWrite = $ph->getPermission($this, 'user.canWrite');
|
$canWrite = $ph->getPermission($this, 'user.canWrite');
|
||||||
// room suspension
|
// room suspension
|
||||||
@ -171,8 +175,7 @@ public function getUsers() {
|
|||||||
|
|
||||||
$sql = "SELECT
|
$sql = "SELECT
|
||||||
u.*,
|
u.*,
|
||||||
st.fieldValue AS awayStatus,
|
st.fieldValue AS awayStatus
|
||||||
su.suspensionID AS suspended
|
|
||||||
FROM
|
FROM
|
||||||
wcf".WCF_N."_user u
|
wcf".WCF_N."_user u
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
@ -182,20 +185,12 @@ public function getUsers() {
|
|||||||
AND st.field = ?
|
AND st.field = ?
|
||||||
AND st.packageID = ?
|
AND st.packageID = ?
|
||||||
)
|
)
|
||||||
LEFT JOIN
|
|
||||||
wcf".WCF_N."_chat_suspension su
|
|
||||||
ON (
|
|
||||||
u.userID = su.userID
|
|
||||||
AND ( su.roomID IS NULL
|
|
||||||
OR su.roomID = ?)
|
|
||||||
AND time > ?
|
|
||||||
)
|
|
||||||
WHERE
|
WHERE
|
||||||
u.userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').")
|
u.userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').")
|
||||||
ORDER BY
|
ORDER BY
|
||||||
u.username ASC";
|
u.username ASC";
|
||||||
$stmt = WCF::getDB()->prepareStatement($sql);
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
||||||
array_unshift($userIDs, 'away', $packageID, $this->roomID, TIME_NOW);
|
array_unshift($userIDs, 'away', $packageID);
|
||||||
$stmt->execute($userIDs);
|
$stmt->execute($userIDs);
|
||||||
|
|
||||||
return $stmt->fetchObjects('\wcf\data\user\User');
|
return $stmt->fetchObjects('\wcf\data\user\User');
|
||||||
|
@ -115,7 +115,7 @@ public function readMessages() {
|
|||||||
|
|
||||||
// update last seen message
|
// update last seen message
|
||||||
$sql = "SELECT
|
$sql = "SELECT
|
||||||
max(messageID) as messageID
|
MAX(messageID) as messageID
|
||||||
FROM
|
FROM
|
||||||
wcf".WCF_N."_chat_message";
|
wcf".WCF_N."_chat_message";
|
||||||
$stmt = WCF::getDB()->prepareStatement($sql);
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
||||||
@ -162,7 +162,24 @@ public function show() {
|
|||||||
'userID' => $user->userID,
|
'userID' => $user->userID,
|
||||||
'username' => $user->username,
|
'username' => $user->username,
|
||||||
'awayStatus' => $user->awayStatus,
|
'awayStatus' => $user->awayStatus,
|
||||||
'suspended' => $user->suspended
|
'suspended' => !$this->room->canWrite($user)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ENABLE_BENCHMARK) {
|
||||||
|
$b = \wcf\system\benchmark\Benchmark::getInstance();
|
||||||
|
$items = array();
|
||||||
|
if (ENABLE_DEBUG_MODE) {
|
||||||
|
foreach ($b->getItems() as $item) {
|
||||||
|
$items[] = array('text' => $item['text'], 'use' => $item['use']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$json['benchmark'] = array(
|
||||||
|
'time' => $b->getExecutionTime(),
|
||||||
|
'queryTime' => $b->getQueryExecutionTime(),
|
||||||
|
'queryPercent' => $b->getQueryExecutionTime() / $b->getExecutionTime(),
|
||||||
|
'items' => $items
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,39 +14,41 @@
|
|||||||
* @package be.bastelstu.wcf.chat
|
* @package be.bastelstu.wcf.chat
|
||||||
* @subpackage system.chat.permissions
|
* @subpackage system.chat.permissions
|
||||||
*/
|
*/
|
||||||
class ChatPermissionHandler extends \wcf\system\SingletonFactory {
|
class ChatPermissionHandler {
|
||||||
protected $chatPermissions = array();
|
protected $chatPermissions = array();
|
||||||
|
protected $user = null, $userProfile = null;
|
||||||
|
|
||||||
|
public function __construct(\wcf\data\user\User $user = null) {
|
||||||
|
if ($user === null) $user = WCF::getUser();
|
||||||
|
$this->user = $user;
|
||||||
|
$this->userProfile = new \wcf\data\user\UserProfile($this->user);
|
||||||
|
|
||||||
/**
|
|
||||||
* @see \wcf\system\SingletonFactory::init()
|
|
||||||
*/
|
|
||||||
protected function init() {
|
|
||||||
$packageID = \wcf\util\ChatUtil::getPackageID();
|
$packageID = \wcf\util\ChatUtil::getPackageID();
|
||||||
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
|
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
|
||||||
|
|
||||||
// get groups permissions
|
// get groups permissions
|
||||||
$groups = implode(',', WCF::getUser()->getGroupIDs());
|
$groups = implode(',', $user->getGroupIDs());
|
||||||
$groupsFileName = \wcf\util\StringUtil::getHash(implode('-', WCF::getUser()->getGroupIDs()));
|
$groupsFileName = \wcf\util\StringUtil::getHash(implode('-', $user->getGroupIDs()));
|
||||||
CacheHandler::getInstance()->addResource('chatPermission-'.$groups, WCF_DIR.'cache/cache.chatPermission-'.$groupsFileName.'.php', '\wcf\system\cache\builder\ChatPermissionCacheBuilder');
|
CacheHandler::getInstance()->addResource('chatPermission-'.$groups, WCF_DIR.'cache/cache.chatPermission-'.$groupsFileName.'.php', '\wcf\system\cache\builder\ChatPermissionCacheBuilder');
|
||||||
$this->chatPermissions = CacheHandler::getInstance()->get('chatPermission-'.$groups);
|
$this->chatPermissions = CacheHandler::getInstance()->get('chatPermission-'.$groups);
|
||||||
|
|
||||||
// get user permissions
|
// get user permissions
|
||||||
if (WCF::getUser()->userID) {
|
if ($user->userID) {
|
||||||
// get data from storage
|
// get data from storage
|
||||||
$ush->loadStorage(array(WCF::getUser()->userID), $packageID);
|
$ush->loadStorage(array($user->userID), $packageID);
|
||||||
|
|
||||||
// get ids
|
// get ids
|
||||||
$data = $ush->getStorage(array(WCF::getUser()->userID), 'chatUserPermissions', $packageID);
|
$data = $ush->getStorage(array($user->userID), 'chatUserPermissions', $packageID);
|
||||||
|
|
||||||
// cache does not exist or is outdated
|
// cache does not exist or is outdated
|
||||||
if ($data[WCF::getUser()->userID] === null) {
|
if ($data[$user->userID] === null) {
|
||||||
$userPermissions = array();
|
$userPermissions = array();
|
||||||
|
|
||||||
$conditionBuilder = new \wcf\system\database\util\PreparedStatementConditionBuilder();
|
$conditionBuilder = new \wcf\system\database\util\PreparedStatementConditionBuilder();
|
||||||
$conditionBuilder->add('acl_option.packageID IN (?)', array(PackageDependencyHandler::getInstance()->getDependencies()));
|
$conditionBuilder->add('acl_option.packageID IN (?)', array(PackageDependencyHandler::getInstance()->getDependencies()));
|
||||||
$conditionBuilder->add('acl_option.objectTypeID = ?', array(ACLHandler::getInstance()->getObjectTypeID('be.bastelstu.wcf.chat.room')));
|
$conditionBuilder->add('acl_option.objectTypeID = ?', array(ACLHandler::getInstance()->getObjectTypeID('be.bastelstu.wcf.chat.room')));
|
||||||
$conditionBuilder->add('option_to_user.optionID = acl_option.optionID');
|
$conditionBuilder->add('option_to_user.optionID = acl_option.optionID');
|
||||||
$conditionBuilder->add('option_to_user.userID = ?', array(WCF::getUser()->userID));
|
$conditionBuilder->add('option_to_user.userID = ?', array($user->userID));
|
||||||
$sql = "SELECT option_to_user.objectID AS roomID, option_to_user.optionValue,
|
$sql = "SELECT option_to_user.objectID AS roomID, option_to_user.optionValue,
|
||||||
acl_option.optionName AS permission
|
acl_option.optionName AS permission
|
||||||
FROM wcf".WCF_N."_acl_option acl_option,
|
FROM wcf".WCF_N."_acl_option acl_option,
|
||||||
@ -59,10 +61,10 @@ protected function init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update cache
|
// update cache
|
||||||
$ush->update(WCF::getUser()->userID, 'chatUserPermissions', serialize($userPermissions), $packageID);
|
$ush->update($user->userID, 'chatUserPermissions', serialize($userPermissions), $packageID);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$userPermissions = unserialize($data[WCF::getUser()->userID]);
|
$userPermissions = unserialize($data[$user->userID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($userPermissions as $roomID => $permissions) {
|
foreach ($userPermissions as $roomID => $permissions) {
|
||||||
@ -83,7 +85,8 @@ protected function init() {
|
|||||||
public function getPermission(\wcf\data\chat\room\ChatRoom $room, $permission) {
|
public function getPermission(\wcf\data\chat\room\ChatRoom $room, $permission) {
|
||||||
if (!isset($this->chatPermissions[$room->roomID][$permission])) {
|
if (!isset($this->chatPermissions[$room->roomID][$permission])) {
|
||||||
$permission = str_replace(array('user.', 'mod.'), array('user.chat.', 'mod.chat.'), $permission);
|
$permission = str_replace(array('user.', 'mod.'), array('user.chat.', 'mod.chat.'), $permission);
|
||||||
return WCF::getSession()->getPermission($permission);
|
|
||||||
|
return $this->userProfile->getPermission($permission);
|
||||||
}
|
}
|
||||||
return (boolean) $this->chatPermissions[$room->roomID][$permission];
|
return (boolean) $this->chatPermissions[$room->roomID][$permission];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user