2011-12-26 16:01:24 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\page;
|
|
|
|
use \chat\data;
|
2012-10-20 14:20:52 +00:00
|
|
|
use \wcf\system\exception\IllegalLinkException;
|
2011-12-26 16:01:24 +00:00
|
|
|
use \wcf\system\WCF;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads new messages.
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
2013-01-19 19:36:40 +00:00
|
|
|
* @copyright 2010-2013 Tim Düsterhus
|
2011-12-26 16:01:24 +00:00
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
2013-01-19 19:36:40 +00:00
|
|
|
* @package be.bastelstu.chat
|
2011-12-26 16:01:24 +00:00
|
|
|
* @subpackage page
|
|
|
|
*/
|
2013-01-27 21:41:36 +00:00
|
|
|
class NewMessagesPage extends \wcf\page\AbstractPage {
|
2012-08-08 14:51:24 +00:00
|
|
|
/**
|
|
|
|
* @see wcf\page\AbstractPage::$loginRequired
|
|
|
|
*/
|
|
|
|
public $loginRequired = true;
|
|
|
|
|
2012-05-31 14:57:20 +00:00
|
|
|
/**
|
|
|
|
* The new and unseen messages.
|
|
|
|
*
|
|
|
|
* @var array<\wcf\data\chat\message\ChatMessage>
|
|
|
|
*/
|
2011-12-26 16:01:24 +00:00
|
|
|
public $messages = array();
|
2012-05-31 14:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \wcf\page\AbstractPage::$neededModules
|
|
|
|
*/
|
2013-05-23 20:14:07 +00:00
|
|
|
public $neededModules = array('MODULE_CHAT');
|
2012-05-31 14:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \wcf\page\AbstractPage::$neededPermissions
|
|
|
|
*/
|
2012-09-07 20:29:02 +00:00
|
|
|
public $neededPermissions = array();
|
2012-05-31 14:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The room the user joined.
|
|
|
|
*
|
2013-01-19 19:36:40 +00:00
|
|
|
* @var \chat\data\room\Room
|
2012-05-31 14:57:20 +00:00
|
|
|
*/
|
2011-12-26 16:01:24 +00:00
|
|
|
public $room = null;
|
2012-05-31 14:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All the users that are currently in the room $this->room.
|
|
|
|
*
|
2013-08-19 20:14:59 +00:00
|
|
|
* @var array<\wcf\data\user\UserProfile>
|
2012-05-31 14:57:20 +00:00
|
|
|
*/
|
2012-01-12 19:04:28 +00:00
|
|
|
public $users = array();
|
2012-05-31 14:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \wcf\page\AbstractPage::$useTemplate
|
|
|
|
*/
|
2011-12-26 16:01:24 +00:00
|
|
|
public $useTemplate = false;
|
|
|
|
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\page\Page::readData()
|
2011-12-26 16:01:24 +00:00
|
|
|
*/
|
|
|
|
public function readData() {
|
|
|
|
parent::readData();
|
|
|
|
|
2012-01-14 11:50:57 +00:00
|
|
|
$this->readRoom();
|
|
|
|
$this->readMessages();
|
2012-03-01 21:11:20 +00:00
|
|
|
$this->users = $this->room->getUsers();
|
2012-04-18 19:01:38 +00:00
|
|
|
|
2013-05-03 18:02:18 +00:00
|
|
|
$roomAction = new data\room\RoomAction(array(), 'removeDeadUsers');
|
|
|
|
$roomAction->executeAction();
|
2012-01-14 11:50:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-15 19:37:20 +00:00
|
|
|
/**
|
|
|
|
* Fetches the new messages
|
|
|
|
*/
|
2012-01-14 11:50:57 +00:00
|
|
|
public function readMessages() {
|
2013-05-24 22:50:50 +00:00
|
|
|
$this->messages = data\message\ViewableMessageList::getMessagesSince($this->room, WCF::getUser()->chatLastSeen);
|
2012-01-14 11:50:57 +00:00
|
|
|
|
|
|
|
// update last seen message
|
|
|
|
$sql = "SELECT
|
2012-11-23 19:33:27 +00:00
|
|
|
MAX(messageID)
|
2012-10-19 15:44:30 +00:00
|
|
|
FROM
|
2013-01-26 21:46:54 +00:00
|
|
|
chat".WCF_N."_message";
|
2012-01-14 11:50:57 +00:00
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
2011-12-26 18:00:32 +00:00
|
|
|
$stmt->execute();
|
2012-04-15 19:37:20 +00:00
|
|
|
|
2013-05-24 15:30:27 +00:00
|
|
|
$editor = new \wcf\data\user\UserEditor(WCF::getUser());
|
|
|
|
$editor->update(array(
|
|
|
|
'chatLastSeen' => $stmt->fetchColumn(),
|
|
|
|
'chatLastActivity' => TIME_NOW
|
2012-04-15 19:37:20 +00:00
|
|
|
));
|
2012-01-14 11:50:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-15 19:37:20 +00:00
|
|
|
/**
|
|
|
|
* Initializes the room databaseobject.
|
|
|
|
*/
|
2012-01-14 11:50:57 +00:00
|
|
|
public function readRoom() {
|
2013-05-24 15:30:27 +00:00
|
|
|
$this->room = \chat\data\room\RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID);
|
2013-05-24 00:21:49 +00:00
|
|
|
if (!$this->room) throw new IllegalLinkException();
|
2012-01-14 11:50:57 +00:00
|
|
|
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
|
|
|
|
}
|
|
|
|
|
2011-12-26 16:01:24 +00:00
|
|
|
/**
|
|
|
|
* @see \wcf\page\IPage::show()
|
|
|
|
*/
|
|
|
|
public function show() {
|
|
|
|
parent::show();
|
|
|
|
|
|
|
|
@header('Content-type: application/json');
|
2013-07-09 20:22:09 +00:00
|
|
|
\wcf\util\HeaderUtil::sendNoCacheHeaders();
|
2012-03-04 16:16:25 +00:00
|
|
|
|
2011-12-27 11:44:36 +00:00
|
|
|
$json = array('users' => array(), 'messages' => array());
|
|
|
|
|
2011-12-26 16:01:24 +00:00
|
|
|
foreach ($this->messages as $message) {
|
2011-12-27 11:44:36 +00:00
|
|
|
$json['messages'][] = $message->jsonify(true);
|
2011-12-26 16:01:24 +00:00
|
|
|
}
|
2013-04-21 18:21:51 +00:00
|
|
|
|
2013-08-19 20:14:59 +00:00
|
|
|
\wcf\system\user\storage\UserStorageHandler::getInstance()->loadStorage(array_keys($this->users));
|
2013-04-21 18:21:51 +00:00
|
|
|
|
2012-01-12 19:04:28 +00:00
|
|
|
foreach ($this->users as $user) {
|
|
|
|
$json['users'][] = array(
|
2012-10-19 15:49:56 +00:00
|
|
|
'userID' => (int) $user->userID,
|
2012-03-22 17:45:36 +00:00
|
|
|
'username' => $user->username,
|
2013-05-28 21:46:22 +00:00
|
|
|
'awayStatus' => $user->chatAway,
|
2013-04-21 18:21:51 +00:00
|
|
|
'suspended' => (boolean) !$this->room->canWrite($user->getDecoratedObject()),
|
|
|
|
'avatar' => array(
|
|
|
|
16 => $user->getAvatar()->getImageTag(16),
|
|
|
|
24 => $user->getAvatar()->getImageTag(24),
|
|
|
|
32 => $user->getAvatar()->getImageTag(32),
|
|
|
|
48 => $user->getAvatar()->getImageTag(48)
|
2013-05-10 15:51:48 +00:00
|
|
|
),
|
2013-05-13 16:28:09 +00:00
|
|
|
'link' => \wcf\system\request\LinkHandler::getInstance()->getLink('User', array(
|
2013-05-26 15:39:54 +00:00
|
|
|
'object' => $user->getDecoratedObject()
|
2013-05-10 15:51:48 +00:00
|
|
|
))
|
2012-10-19 15:44:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ENABLE_BENCHMARK) {
|
|
|
|
$b = \wcf\system\benchmark\Benchmark::getInstance();
|
|
|
|
$items = array();
|
|
|
|
if (ENABLE_DEBUG_MODE) {
|
|
|
|
foreach ($b->getItems() as $item) {
|
2013-05-24 22:50:50 +00:00
|
|
|
$items[] = array(
|
|
|
|
'text' => $item['text'],
|
|
|
|
'use' => $item['use'],
|
|
|
|
'trace' => $item['trace']
|
|
|
|
);
|
2012-10-19 15:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$json['benchmark'] = array(
|
|
|
|
'time' => $b->getExecutionTime(),
|
|
|
|
'queryTime' => $b->getQueryExecutionTime(),
|
|
|
|
'queryPercent' => $b->getQueryExecutionTime() / $b->getExecutionTime(),
|
|
|
|
'items' => $items
|
2012-01-12 19:04:28 +00:00
|
|
|
);
|
|
|
|
}
|
2012-01-14 11:50:57 +00:00
|
|
|
|
2011-12-27 11:44:36 +00:00
|
|
|
echo \wcf\util\JSON::encode($json);
|
2011-12-26 16:01:24 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|