2011-12-26 16:01:24 +00:00
|
|
|
<?php
|
|
|
|
namespace wcf\page;
|
|
|
|
use \wcf\data\chat;
|
|
|
|
use \wcf\system\WCF;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads new messages.
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
2012-01-28 16:50:33 +00:00
|
|
|
* @copyright 2010-2012 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>
|
2012-03-12 16:18:15 +00:00
|
|
|
* @package be.bastelstu.wcf.chat
|
2011-12-26 16:01:24 +00:00
|
|
|
* @subpackage page
|
|
|
|
*/
|
|
|
|
class ChatMessagePage extends AbstractPage {
|
|
|
|
public $messages = array();
|
|
|
|
public $neededModules = array('CHAT_ACTIVE');
|
2012-03-23 16:45:26 +00:00
|
|
|
public $neededPermissions = array('user.chat.canEnter');
|
2011-12-26 16:01:24 +00:00
|
|
|
public $room = null;
|
|
|
|
public $roomID = 0;
|
2012-01-12 19:04:28 +00:00
|
|
|
public $users = array();
|
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
|
|
|
|
|
|
|
$deadUsers = \wcf\util\ChatUtil::getDiedUsers();
|
|
|
|
foreach ($deadUsers as $deadUser) {
|
|
|
|
if (!$deadUser) continue;
|
|
|
|
|
|
|
|
$user = new \wcf\data\user\User($deadUser['userID']);
|
|
|
|
if (CHAT_DISPLAY_JOIN_LEAVE) {
|
|
|
|
$userData['color'] = \wcf\util\ChatUtil::readUserData('color', $user);
|
|
|
|
|
|
|
|
$messageAction = new chat\message\ChatMessageAction(array(), 'create', array(
|
|
|
|
'data' => array(
|
|
|
|
'roomID' => $deadUser['roomID'],
|
|
|
|
'sender' => $user->userID,
|
|
|
|
'username' => $user->username,
|
|
|
|
'time' => TIME_NOW,
|
|
|
|
'type' => chat\message\ChatMessage::TYPE_LEAVE,
|
|
|
|
'message' => '',
|
|
|
|
'color1' => $userData['color'][1],
|
|
|
|
'color2' => $userData['color'][2]
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$messageAction->executeAction();
|
|
|
|
}
|
|
|
|
\wcf\util\ChatUtil::writeUserData(array('roomID' => null), $user);
|
|
|
|
}
|
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() {
|
2011-12-26 16:01:24 +00:00
|
|
|
$this->messages = chat\message\ChatMessageList::getMessagesSince($this->room, \wcf\util\ChatUtil::readUserData('lastSeen'));
|
2012-01-14 11:50:57 +00:00
|
|
|
|
|
|
|
// update last seen message
|
|
|
|
$sql = "SELECT
|
|
|
|
max(messageID) as messageID
|
|
|
|
FROM
|
|
|
|
wcf".WCF_N."_chat_message";
|
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
2011-12-26 18:00:32 +00:00
|
|
|
$stmt->execute();
|
|
|
|
$row = $stmt->fetchArray();
|
2012-04-15 19:37:20 +00:00
|
|
|
|
|
|
|
\wcf\util\ChatUtil::writeUserData(array(
|
|
|
|
'lastSeen' => $row['messageID'],
|
|
|
|
'lastActivity' => TIME_NOW
|
|
|
|
));
|
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() {
|
|
|
|
$this->roomID = \wcf\util\ChatUtil::readUserData('roomID');
|
2012-01-12 19:04:28 +00:00
|
|
|
|
2012-01-14 11:50:57 +00:00
|
|
|
$this->room = chat\room\ChatRoom::getCache()->search($this->roomID);
|
|
|
|
if (!$this->room) throw new \wcf\system\exception\IllegalLinkException();
|
|
|
|
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() {
|
|
|
|
// guests are not supported
|
|
|
|
if (!WCF::getUser()->userID) {
|
|
|
|
throw new \wcf\system\exception\PermissionDeniedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::show();
|
|
|
|
|
|
|
|
@header('Content-type: application/json');
|
2012-03-04 16:16:25 +00:00
|
|
|
// enable gzip compression
|
|
|
|
if (HTTP_ENABLE_GZIP && HTTP_GZIP_LEVEL > 0 && HTTP_GZIP_LEVEL < 10 && !defined('HTTP_DISABLE_GZIP')) {
|
|
|
|
\wcf\util\HeaderUtil::compressOutput();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2012-01-12 19:04:28 +00:00
|
|
|
foreach ($this->users as $user) {
|
|
|
|
$json['users'][] = array(
|
|
|
|
'userID' => $user->userID,
|
2012-03-22 17:45:36 +00:00
|
|
|
'username' => $user->username,
|
|
|
|
'awayStatus' => $user->awayStatus
|
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;
|
|
|
|
}
|
|
|
|
}
|