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

Add RoomCache class

This commit is contained in:
Tim Düsterhus 2013-05-24 02:21:49 +02:00
parent 41765c53be
commit 2d73af2b8c
15 changed files with 94 additions and 38 deletions

View File

@ -1,5 +1,5 @@
<?php
namespace be\bastelstu\wcf\chat;
namespace be\bastelstu\chat;
/**
* Handles installation of Tims Chat.

View File

@ -26,7 +26,7 @@ final class Update {
private $styles = null;
public function __construct() {
$this->rooms = \chat\data\room\Room::getCache();
$this->rooms = \chat\data\room\RoomCache::getInstance()->getRooms();
$this->styles = \wcf\system\style\StyleHandler::getInstance()->getAvailableStyles();
}

View File

@ -53,11 +53,10 @@ public function validateSend() {
$this->parameters['userData']['away'] = \chat\util\ChatUtil::readUserData('away');
// read and validate room
$cache = room\Room::getCache();
if (!isset($cache[$this->parameters['userData']['roomID']])) throw new \wcf\system\exception\IllegalLinkException();
$this->parameters['room'] = $cache[$this->parameters['userData']['roomID']];
if (!$this->parameters['room']->canEnter() || !$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
$this->parameters['room'] = room\RoomCache::getInstance()->getRoom($this->parameters['userData']['roomID']);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
if (!$this->parameters['room']->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
if (!$this->parameters['room']->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
// read parameters
$this->readString('text');

View File

@ -126,19 +126,16 @@ public function updatePosition() {
public function validateGetRoomList() {
if (!MODULE_CHAT) throw new \wcf\system\exception\IllegalLinkException();
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID');
if (!isset($rooms[$roomID])) {
throw new \wcf\system\exception\IllegalLinkException();
}
$this->parameters['room'] = $rooms[$roomID];
$this->parameters['room'] = RoomCache::getInstance()->getRoom($roomID);
if ($this->parameters['room'] === null) throw new \wcf\system\exception\IllegalLinkException();
}
/**
* Returns the available rooms.
*/
public function getRoomList() {
$rooms = Room::getCache();
$rooms = RoomCache::getInstance()->getRooms();
$result = array();
foreach ($rooms as $room) {
@ -165,9 +162,8 @@ public function validateLeave() {
unset($this->parameters['user']);
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID');
if (!isset($rooms[$roomID])) throw new \wcf\system\exception\IllegalLinkException();
if (RoomCache::getInstance()->getRoom($roomID) === null) throw new \wcf\system\exception\IllegalLinkException();
}
/**
@ -179,11 +175,9 @@ public function leave() {
$this->parameters['user'] = WCF::getUser();
}
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID', $this->parameters['user']);
if (!isset($rooms[$roomID])) throw new \wcf\system\exception\UserInputException();
$activeRoom = $rooms[$roomID];
$room = RoomCache::getInstance()->getRoom($roomID);
if ($room === null) throw new \wcf\system\exception\UserInputException();
if (CHAT_DISPLAY_JOIN_LEAVE) {
$userData['color'] = ChatUtil::readUserData('color', $this->parameters['user']);
@ -191,7 +185,7 @@ public function leave() {
// leave message
$messageAction = new \chat\data\message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $activeRoom->roomID,
'roomID' => $room->roomID,
'sender' => $this->parameters['user']->userID,
'username' => $this->parameters['user']->username,
'time' => TIME_NOW,
@ -235,7 +229,7 @@ public function validateGetDashboardRoomList() {
* Returns dashboard roomlist.
*/
public function getDashboardRoomList() {
$rooms = Room::getCache();
$rooms = RoomCache::getInstance()->getRooms();
foreach ($rooms as $key => $room) {
if (!$room->canEnter()) unset($rooms[$key]);

View File

@ -0,0 +1,49 @@
<?php
namespace chat\data\room;
/**
* Manages the room cache.
*
* @author Tim Düsterhus
* @copyright 2010-2013 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package be.bastelstu.chat
* @subpackage data.room
*/
class RoomCache extends \wcf\system\SingletonFactory {
/**
* list of cached rooms
* @var array<\chat\data\room\Room>
*/
protected $rooms = array();
/**
* @see wcf\system\SingletonFactory::init()
*/
protected function init() {
$this->rooms = \chat\system\cache\builder\RoomCacheBuilder::getInstance()->getData();
}
/**
* Returns a specific room.
*
* @param integer $roomID
* @return \chat\data\room\Room
*/
public function getRoom($roomID) {
if (isset($this->rooms[$roomID])) {
return $this->rooms[$roomID];
}
return null;
}
/**
* Returns all rooms.
*
* @return array<\chat\data\room\Room>
*/
public function getRooms() {
return $this->rooms;
}
}

View File

@ -174,7 +174,7 @@ public function readParameters() {
* Reads room data.
*/
public function readRoom() {
$this->rooms = data\room\Room::getCache();
$this->rooms = data\room\RoomCache::getInstance()->getRooms();
if ($this->roomID === 0) {
// no room given

View File

@ -77,11 +77,9 @@ public function readParameters() {
public function readData() {
parent::readData();
$cache = data\room\Room::getCache();
if (!isset($cache[$this->roomID])) throw new IllegalLinkException();
$this->room = $cache[$this->roomID];
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
$this->room = data\room\RoomCache::getInstance()->getRoom($this->roomID);
if (!$this->room) throw new IllegalLinkException();
if (!$this->room->canEnter()) throw new PermissionDeniedException();
// TODO: actually read the correct messages
$this->messages = data\message\MessageList::getNewestMessages($this->room, 150);

View File

@ -94,10 +94,9 @@ public function readMessages() {
*/
public function readRoom() {
$roomID = \chat\util\ChatUtil::readUserData('roomID');
$cache = data\room\Room::getCache();
if (!isset($cache[$roomID])) throw new IllegalLinkException();
$this->room = $cache[$roomID];
$this->room = \chat\data\room\RoomCache::getInstance()->getRoom($roomID);
if (!$this->room) throw new IllegalLinkException();
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
}

View File

@ -39,7 +39,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
}
// Room
$room = new \chat\data\room\Room(ChatUtil::readUserData('roomID', $this->user));
$room = \chat\data\room\RoomCache::getInstance()->getRoom(ChatUtil::readUserData('roomID', $this->user));
if ($room->roomID && $room->canEnter()) {
$this->lines[WCF::getLanguage()->get('chat.general.room')] = $room->getTitle();
}
@ -52,7 +52,11 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
if (!$typeSuspension->isValid()) continue;
$dateTime = DateUtil::getDateTimeByTimestamp($typeSuspension->expires);
$this->lines[$typeSuspension->type.'-'.$typeSuspension->roomID] = str_replace('%time%', DateUtil::format($dateTime, DateUtil::TIME_FORMAT), str_replace('%date%', DateUtil::format($dateTime, DateUtil::DATE_FORMAT), WCF::getLanguage()->get('wcf.date.dateTimeFormat')));
$name = WCF::getLanguage()->getDynamicVariable('chat.information.suspension', array(
'suspension' => $typeSuspension,
'room' => \chat\data\room\RoomCache::getInstance()->getRoom($typeSuspension->roomID)
));
$this->lines[$name] = str_replace('%time%', DateUtil::format($dateTime, DateUtil::TIME_FORMAT), str_replace('%date%', DateUtil::format($dateTime, DateUtil::DATE_FORMAT), WCF::getLanguage()->get('wcf.date.dateTimeFormat')));
}
}

View File

@ -22,7 +22,7 @@ public function getType() {
* @see \chat\system\command\ICommand::getMessage()
*/
public function getMessage() {
$rooms = \chat\data\room\Room::getCache();
$rooms = \chat\data\room\RoomCache::getInstance()->getRooms();
foreach ($rooms as $room) {
$users = $room->getUsers();

View File

@ -27,7 +27,7 @@ public function init(\wcf\data\dashboard\box\DashboardBox $box, \wcf\page\IPage
if (!MODULE_CHAT) return;
if (!\wcf\system\WCF::getUser()->userID) return;
$this->rooms = data\room\Room::getCache();
$this->rooms = data\room\RoomCache::getInstance()->getRooms();
foreach ($this->rooms as $key => $room) {
if (!$room->canEnter()) unset($this->rooms[$key]);

View File

@ -11,6 +11,11 @@
* @subpackage system.menu.page
*/
class ChatPageMenuItemProvider extends \wcf\system\menu\page\DefaultPageMenuItemProvider {
/**
* room that the menu item points to
*
* @var \chat\data\room\Room
*/
protected $room = null;
/**
@ -22,9 +27,9 @@ public function isVisible() {
// guests are not supported
if (!\wcf\system\WCF::getUser()->userID) return false;
$cache = \chat\data\room\Room::getCache();
$rooms = \chat\data\room\RoomCache::getInstance()->getRooms();
foreach ($cache as $this->room) {
foreach ($rooms as $this->room) {
if ($this->room->canEnter()) {
return true;
}

View File

@ -21,7 +21,8 @@ public function cache(\wcf\data\user\online\UserOnline $user) {}
* @see \wcf\system\user\online\location\IUserOnlineLocation::get()
*/
public function get(\wcf\data\user\online\UserOnline $user, $languageVariable = '') {
$cache = data\room\Room::getCache();
$rooms = data\room\RoomCache::getInstance()->getRooms();
if (isset($cache[$user->objectID])) {
if ($cache[$user->objectID]->canEnter()) {
return \wcf\system\WCF::getLanguage()->getDynamicVariable($languageVariable, array(

View File

@ -90,6 +90,7 @@
<item name="chat.general.information"><![CDATA[Information]]></item>
<item name="chat.general.information.chatUpdate"><![CDATA[Der Chat wurde aktualisiert. Bitte lade die Seite neu, da es sonst zu Fehlern kommen kann.]]></item>
<item name="chat.information.suspension"><![CDATA[{lang}chat.suspension.{$suspension->type}{/lang} ({if $room}{$room}{else}{lang}chat.room.global{/lang}{/if})]]></item>
</category>
<category name="chat.header">
@ -116,8 +117,14 @@
<item name="chat.message.color.success"><![CDATA[Die Farbe wurde erfolgreich geändert.]]></item>
</category>
<category name="chat.suspension">
<item name="chat.suspension.1"><![CDATA[Knebel]]></item>
<item name="chat.suspension.2"><![CDATA[Bann]]></item>
</category>
<!-- I18N Values -->
<category name="chat.room">
<item name="chat.room.global"><![CDATA[Global]]></item>
<item name="chat.room.title1"><![CDATA[Hauptchat]]></item>
<item name="chat.room.topic1"><![CDATA[Vielen Dank, dass Sie sich für Tims Chat entschieden haben. Bitte besuchen Sie das ACP um diesen Text anzupassen.]]></item>
<item name="chat.room.titleTemp"><![CDATA[Raum_{$roomID}]]></item>

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 26</version><!-- Codename: Codenames are overrated -->
<version>3.0.0 Alpha 28</version><!-- Codename: Codenames are overrated -->
<date>2011-11-26</date>
</packageinformation>