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

Move settings from user storage to user table

Closes #16
This commit is contained in:
Tim Düsterhus 2013-05-24 17:30:27 +02:00
parent bf55534b9f
commit 8fbb2ae76b
15 changed files with 87 additions and 154 deletions

View File

@ -48,9 +48,10 @@ public function prune() {
*/
public function validateSend() {
// read user data
$this->parameters['userData']['color'] = \chat\util\ChatUtil::readUserData('color');
$this->parameters['userData']['roomID'] = \chat\util\ChatUtil::readUserData('roomID');
$this->parameters['userData']['away'] = \chat\util\ChatUtil::readUserData('away');
$this->parameters['userData']['color1'] = WCF::getUser()->chatColor1;
$this->parameters['userData']['color2'] = WCF::getUser()->chatColor2;
$this->parameters['userData']['roomID'] = WCF::getUser()->chatRoomID;
$this->parameters['userData']['away'] = WCF::getUser()->chatAway;
// read and validate room
$this->parameters['room'] = room\RoomCache::getInstance()->getRoom($this->parameters['userData']['roomID']);
@ -81,7 +82,11 @@ public function validateSend() {
}
}
\chat\util\ChatUtil::writeUserData(array('away' => null));
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatAway' => null,
'chatLastActivity' => TIME_NOW
));
// mark user as back
if ($this->parameters['userData']['away'] !== null) {
@ -93,8 +98,8 @@ public function validateSend() {
'time' => TIME_NOW,
'type' => Message::TYPE_BACK,
'message' => '',
'color1' => $this->parameters['userData']['color'][1],
'color2' => $this->parameters['userData']['color'][2]
'color1' => $this->parameters['userData']['color1'],
'color2' => $this->parameters['userData']['color2']
)
));
$messageAction->executeAction();
@ -131,7 +136,7 @@ public function validateSend() {
/**
* Creates sent message.
*/
public function send() {
public function send() {
$this->objectAction = new MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['room']->roomID,
@ -143,8 +148,8 @@ public function send() {
'message' => $this->parameters['text'],
'enableSmilies' => $this->parameters['enableSmilies'] ? 1 : 0,
'enableHTML' => $this->parameters['enableHTML'] ? 1 : 0,
'color1' => $this->parameters['userData']['color'][1],
'color2' => $this->parameters['userData']['color'][2]
'color1' => $this->parameters['userData']['color1'],
'color2' => $this->parameters['userData']['color2']
)
));
$this->objectAction->executeAction();

View File

@ -126,12 +126,11 @@ public function countUsers() {
$sql = "SELECT
COUNT(*)
FROM
wcf".WCF_N."_user_storage
wcf".WCF_N."_user
WHERE
field = ?
AND fieldValue = ?";
chatRoomID = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('roomID', $this->roomID));
$stmt->execute(array($this->roomID));
return $stmt->fetchColumn();
}
@ -142,21 +141,8 @@ public function countUsers() {
* @return \wcf\data\user\UserProfileList
*/
public function getUsers() {
$sql = "SELECT
userID
FROM
wcf".WCF_N."_user_storage
WHERE
field = ?
AND fieldValue = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('roomID', $this->roomID));
$userIDs = array();
while ($userID = $stmt->fetchColumn()) $userIDs[] = $userID;
$userList = new \wcf\data\user\UserProfileList();
if (!empty($userIDs)) $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($userIDs));
else $userList->getConditionBuilder()->add('1 = 0', array());
$userList->getConditionBuilder()->add('user_table.chatRoomID = ?', array($this->roomID));
$userList->readObjects();
@ -170,35 +156,15 @@ public function getUsers() {
*/
public static function getDeadUsers() {
if (\wcf\system\nodePush\NodePushHandler::getInstance()->isEnabled()) {
$time = TIME_NOW - 120;
$time = TIME_NOW - 180;
}
else {
$time = TIME_NOW;
}
$sql = "SELECT
r.userID
FROM
wcf".WCF_N."_user_storage r
LEFT JOIN
wcf".WCF_N."_user_storage a
ON a.userID = r.userID
AND a.field = ?
WHERE
r.field = ?
AND r.fieldValue IS NOT NULL
AND (
a.fieldValue < ?
OR a.fieldValue IS NULL
)";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute(array('lastActivity', 'roomID', $time - 30));
$userIDs = array();
while ($userID = $stmt->fetchColumn()) $userIDs[] = $userID;
$userList = new \wcf\data\user\UserList();
if (!empty($userIDs)) $userList->getConditionBuilder()->add('user_table.userID IN (?)', array($userIDs));
else $userList->getConditionBuilder()->add('1 = 0', array());
$userList->getConditionBuilder()->add('user_table.chatRoomID IS NOT NULL', array());
$userList->getConditionBuilder()->add('user_table.chatLastActivity < ?', array($time - 30));
$userList->readObjects();

View File

@ -126,8 +126,7 @@ public function updatePosition() {
public function validateGetRoomList() {
if (!MODULE_CHAT) throw new \wcf\system\exception\IllegalLinkException();
$roomID = ChatUtil::readUserData('roomID');
$this->parameters['room'] = RoomCache::getInstance()->getRoom($roomID);
$this->parameters['room'] = RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
}
@ -162,8 +161,7 @@ public function validateLeave() {
unset($this->parameters['user']);
$roomID = ChatUtil::readUserData('roomID');
if (RoomCache::getInstance()->getRoom($roomID) === null) throw new \wcf\system\exception\IllegalLinkException();
if (RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID) === null) throw new \wcf\system\exception\IllegalLinkException();
}
/**
@ -175,13 +173,10 @@ public function leave() {
$this->parameters['user'] = WCF::getUser();
}
$roomID = ChatUtil::readUserData('roomID', $this->parameters['user']);
$room = RoomCache::getInstance()->getRoom($roomID);
$room = RoomCache::getInstance()->getRoom($this->parameters['user']->chatRoomID);
if ($room === null) throw new \wcf\system\exception\UserInputException();
if (CHAT_DISPLAY_JOIN_LEAVE) {
$userData['color'] = ChatUtil::readUserData('color', $this->parameters['user']);
// leave message
$messageAction = new \chat\data\message\MessageAction(array(), 'create', array(
'data' => array(
@ -191,15 +186,18 @@ public function leave() {
'time' => TIME_NOW,
'type' => \chat\data\message\Message::TYPE_LEAVE,
'message' => '',
'color1' => $userData['color'][1],
'color2' => $userData['color'][2]
'color1' => $this->parameters['user']->chatColor1,
'color2' => $this->parameters['user']->chatColor2
)
));
$messageAction->executeAction();
}
// set current room to null
ChatUtil::writeUserData(array('roomID' => null), $this->parameters['user']);
$editor = new \wcf\data\user\UserEditor($this->parameters['user']);
$editor->update(array(
'chatRoomID' => null
));
\wcf\system\nodePush\NodePushHandler::getInstance()->sendMessage('be.bastelstu.chat.join');
}

View File

@ -42,9 +42,17 @@ public function isValid() {
*/
public static function getSuspensionsForUser(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$suspensions = \chat\util\ChatUtil::readUserData('suspensions', $user);
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
if ($suspensions === null) {
// load storage
$ush->loadStorage(array($user->userID));
$data = $ush->getStorage(array($user->userID), 'chatSuspensions');
try {
$suspensions = unserialize($data[$user->userID]);
if (!$suspensions) throw new \wcf\system\exception\SystemException();
}
catch (\wcf\system\exception\SystemException $e) {
$sql = "SELECT
*
FROM
@ -60,7 +68,7 @@ public static function getSuspensionsForUser(\wcf\data\user\User $user = null) {
$suspensions[$suspension->roomID][$suspension->type] = $suspension;
}
\chat\util\ChatUtil::writeUserData(array('suspensions' => $suspensions), $user);
$ush->update($user->userID, 'chatSuspensions', serialize($suspensions));
}
return $suspensions;

View File

@ -23,6 +23,6 @@ class SuspensionEditor extends \wcf\data\DatabaseObjectEditor implements \wcf\da
public static function resetCache() {
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
$ush->resetAll('suspensions');
$ush->resetAll('chatSuspensions');
}
}

View File

@ -109,12 +109,6 @@ public function readData() {
parent::readData();
$this->readRoom();
$this->userData['color'] = \chat\util\ChatUtil::readUserData('color');
\chat\util\ChatUtil::writeUserData(array(
'roomID' => $this->room->roomID,
'away' => null,
'lastActivity' => TIME_NOW
));
if (CHAT_DISPLAY_JOIN_LEAVE) {
$messageAction = new data\message\MessageAction(array(), 'create', array(
@ -125,8 +119,8 @@ public function readData() {
'time' => TIME_NOW,
'type' => \chat\data\message\Message::TYPE_JOIN,
'message' => serialize(array('ipAddress' => \wcf\util\UserUtil::convertIPv6To4(\wcf\util\UserUtil::getIpAddress()))),
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
'color1' => WCF::getUser()->chatColor1,
'color2' => WCF::getUser()->chatColor2
)
));
$messageAction->executeAction();
@ -135,12 +129,20 @@ public function readData() {
$this->newestMessages = data\message\MessageList::getNewestMessages($this->room, CHAT_LASTMESSAGES);
try {
\chat\util\ChatUtil::writeUserData(array('lastSeen' => end($this->newestMessages)->messageID));
$lastSeen = end($this->newestMessages)->messageID;
}
catch (\wcf\system\exception\SystemException $e) {
\chat\util\ChatUtil::writeUserData(array('lastSeen' => 0));
$lastSeen = 0;
}
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatRoomID' => $this->room->roomID,
'chatAway' => null,
'chatLastActivity' => TIME_NOW,
'chatLastSeen' => $lastSeen
));
// get default smilies
if (MODULE_SMILEY) {
$this->smileyCategories = \wcf\data\smiley\SmileyCache::getInstance()->getCategories();

View File

@ -73,7 +73,7 @@ public function readData() {
* Fetches the new messages
*/
public function readMessages() {
$this->messages = data\message\MessageList::getMessagesSince($this->room, \chat\util\ChatUtil::readUserData('lastSeen'));
$this->messages = data\message\MessageList::getMessagesSince($this->room, WCF::getUser()->chatLastSeen);
// update last seen message
$sql = "SELECT
@ -83,9 +83,10 @@ public function readMessages() {
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
\chat\util\ChatUtil::writeUserData(array(
'lastSeen' => $stmt->fetchColumn(),
'lastActivity' => TIME_NOW
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatLastSeen' => $stmt->fetchColumn(),
'chatLastActivity' => TIME_NOW
));
}
@ -93,9 +94,7 @@ public function readMessages() {
* Initializes the room databaseobject.
*/
public function readRoom() {
$roomID = \chat\util\ChatUtil::readUserData('roomID');
$this->room = \chat\data\room\RoomCache::getInstance()->getRoom($roomID);
$this->room = \chat\data\room\RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID);
if (!$this->room) throw new IllegalLinkException();
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
}

View File

@ -15,7 +15,10 @@ class AwayCommand extends \chat\system\command\AbstractCommand {
public function __construct(\chat\system\command\CommandHandler $commandHandler) {
parent::__construct($commandHandler);
\chat\util\ChatUtil::writeUserData(array('away' => $commandHandler->getParameters()));
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
$editor->update(array(
'chatAway' => $commandHandler->getParameters()
));
$this->didInit();
}

View File

@ -28,7 +28,7 @@ public function executeAction() {
$this->suspensionAction = new suspension\SuspensionAction(array(), 'create', array(
'data' => array(
'userID' => $this->user->userID,
'roomID' => ChatUtil::readUserData('roomID'),
'roomID' => WCF::getUser()->chatRoomID,
'type' => suspension\Suspension::TYPE_BAN,
'expires' => $this->expires
)

View File

@ -56,13 +56,19 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
if (isset(self::$colors[$val])) $color[$key] = self::$colors[$val];
else {
if (!$regex->match($val)) throw new \chat\system\command\NotFoundException();
$matches = $regex->getMatches();
$val = $matches[1];
if (strlen($val) == 3) $val = $val[0].$val[0].$val[1].$val[1].$val[2].$val[2];
$color[$key] = hexdec($val);
}
}
\chat\util\ChatUtil::writeUserData(array('color' => $color));
$editor = new \wcf\data\user\UserEditor(\wcf\system\WCF::getUser());
$editor->update(array(
'chatColor1' => $color[1],
'chatColor2' => $color[2]
));
$this->didInit();
}

View File

@ -34,13 +34,13 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
$this->lines[WCF::getLanguage()->get('wcf.user.username')] = "[url='".$profile."']".$this->user->username.'[/url]';
// Away-Status
if (ChatUtil::readUserData('away', $this->user) !== null) {
$this->lines[WCF::getLanguage()->get('wcf.chat.away')] = ChatUtil::readUserData('away', $this->user);
if ($this->user->chatAway !== null) {
$this->lines[WCF::getLanguage()->get('wcf.chat.away')] = $this->user->chatAway;
}
// Room
$room = \chat\data\room\RoomCache::getInstance()->getRoom(ChatUtil::readUserData('roomID', $this->user));
if ($room->roomID && $room->canEnter()) {
$room = \chat\data\room\RoomCache::getInstance()->getRoom($this->user->chatRoomID);
if ($room !== null && $room->canEnter()) {
$this->lines[WCF::getLanguage()->get('chat.general.room')] = $room->getTitle();
}

View File

@ -60,7 +60,7 @@ public function executeAction() {
$this->suspensionAction = new suspension\SuspensionAction(array(), 'create', array(
'data' => array(
'userID' => $this->user->userID,
'roomID' => ChatUtil::readUserData('roomID'),
'roomID' => WCF::getUser()->chatRoomID,
'type' => suspension\Suspension::TYPE_MUTE,
'expires' => $this->expires
)

View File

@ -29,14 +29,6 @@ final class ChatUtil {
*/
const PACKAGE_IDENTIFIER = 'be.bastelstu.chat';
/**
* Which user-storage-keys need serialization.
* The value should always be true.
*
* @var array<boolean>
*/
private static $serialize = array('color' => true, 'suspensions' => true);
/**
* Cached packageID of Tims Chat.
*
@ -55,16 +47,6 @@ public static function getPackageID() {
return self::$packageID;
}
/**
* Returns a random number.
*
* @return integer
*/
public static function /* int */ getRandomNumber() {
return 4; // chosen by a fair dice roll
// guaranteed to be random
}
/**
* Creates a gradient out of two colors represented by an integer.
* The first byte is red, the second byte is green, the third one is blue.
@ -95,51 +77,6 @@ public static function gradient($string, $start, $end) {
return $result;
}
/**
* Reads user data.
*
* @param string $field
* @param \wcf\data\user\User $user
* @return mixed
*/
public static function readUserData($field, \wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$ush = UserStorageHandler::getInstance();
// load storage
$ush->loadStorage(array($user->userID));
$data = $ush->getStorage(array($user->userID), $field);
if ($data[$user->userID] === null) {
switch ($field) {
case 'color':
$data[$user->userID] = array(1 => self::getRandomNumber(), 2 => self::getRandomNumber() * 0xFFFF);
break;
}
if ($data[$user->userID] !== null) static::writeUserData(array($field => $data[$user->userID]));
return $data[$user->userID];
}
if (isset(static::$serialize[$field])) return unserialize($data[$user->userID]);
else return $data[$user->userID];
}
/**
* Writes user data
*
* @param array $data
* @param \wcf\data\user\User $user
*/
public static function writeUserData(array $data, \wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$ush = UserStorageHandler::getInstance();
foreach ($data as $key => $value) {
$ush->update($user->userID, $key, (isset(static::$serialize[$key])) ? serialize($value) : $value);
}
}
/**
* Splits a string into smaller chunks.
* UTF-8 safe version of str_split().

View File

@ -54,6 +54,13 @@ CREATE TABLE chat1_suspension (
KEY (expires)
);
ALTER TABLE wcf1_user ADD COLUMN chatRoomID INT(10) DEFAULT NULL;
ALTER TABLE wcf1_user ADD COLUMN chatColor1 INT(10) NOT NULL DEFAULT 0;
ALTER TABLE wcf1_user ADD COLUMN chatColor2 INT(10) NOT NULL DEFAULT 0;
ALTER TABLE wcf1_user ADD COLUMN chatLastActivity INT(10) NOT NULL DEFAULT 0;
ALTER TABLE wcf1_user ADD COLUMN chatAway TEXT DEFAULT NULL;
ALTER TABLE wcf1_user ADD COLUMN chatLastSeen INT(10) NOT NULL DEFAULT 0;
ALTER TABLE chat1_message ADD FOREIGN KEY (receiver) REFERENCES wcf1_user (userID) ON DELETE CASCADE;
ALTER TABLE chat1_message ADD FOREIGN KEY (roomID) REFERENCES chat1_room (roomID) ON DELETE CASCADE;
ALTER TABLE chat1_message ADD FOREIGN KEY (sender) REFERENCES wcf1_user (userID) ON DELETE SET NULL;
@ -63,6 +70,8 @@ ALTER TABLE chat1_room ADD FOREIGN KEY (owner) REFERENCES wcf1_user (userID) ON
ALTER TABLE chat1_suspension ADD FOREIGN KEY (userID) REFERENCES wcf1_user (userID) ON DELETE CASCADE;
ALTER TABLE chat1_suspension ADD FOREIGN KEY (roomID) REFERENCES chat1_room (roomID) ON DELETE CASCADE;
ALTER TABLE wcf1_user ADD FOREIGN KEY (chatRoomID) REFERENCES chat1_room (roomID) ON DELETE SET NULL;
INSERT INTO chat1_room (title, topic, showOrder) VALUES ('chat.room.title1', 'chat.room.topic1', 1);
INSERT INTO chat1_room (title, topic, showOrder) VALUES ('Testroom 2', 'Topic of Testroom 2', 2);
INSERT INTO chat1_room (title, topic, showOrder) VALUES ('Testroom with a very long', 'The topic of this room is rather loing as well!', 3);

View File

@ -5,7 +5,7 @@
<packagedescription><![CDATA[Chat for WoltLab Community Framework™.]]></packagedescription>
<packagedescription language="de"><![CDATA[Chat für WoltLab Community Framework™.]]></packagedescription>
<isapplication>1</isapplication>
<version>3.0.0 Alpha 28</version><!-- Codename: Codenames are overrated -->
<version>3.0.0 Alpha 31</version><!-- Codename: Codenames are overrated -->
<date>2011-11-26</date>
</packageinformation>