1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-24 22:00:09 +00:00
Tims-Chat/file/lib/page/NewMessagesPage.class.php

173 lines
4.2 KiB
PHP
Raw Normal View History

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
*/
class NewMessagesPage extends \wcf\page\AbstractPage {
2012-08-08 14:51:24 +00:00
/**
* @see wcf\page\AbstractPage::$loginRequired
*/
public $loginRequired = true;
/**
* The new and unseen messages.
*
* @var array<\wcf\data\chat\message\ChatMessage>
*/
2011-12-26 16:01:24 +00:00
public $messages = array();
/**
* @see \wcf\page\AbstractPage::$neededModules
*/
2011-12-26 16:01:24 +00:00
public $neededModules = array('CHAT_ACTIVE');
/**
* @see \wcf\page\AbstractPage::$neededPermissions
*/
public $neededPermissions = array();
/**
* The room the user joined.
*
2013-01-19 19:36:40 +00:00
* @var \chat\data\room\Room
*/
2011-12-26 16:01:24 +00:00
public $room = null;
/**
* All the users that are currently in the room $this->room.
*
* @var array<\wcf\data\user\User>
*/
2012-01-12 19:04:28 +00:00
public $users = array();
/**
* @see \wcf\page\AbstractPage::$useTemplate
*/
2011-12-26 16:01:24 +00:00
public $useTemplate = false;
/**
* @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();
$this->users = $this->room->getUsers();
2012-04-18 19:01:38 +00:00
2013-01-19 19:36:40 +00:00
$deadUsers = \chat\util\ChatUtil::getDiedUsers();
2012-04-18 19:01:38 +00:00
foreach ($deadUsers as $deadUser) {
if (!$deadUser) continue;
$user = new \wcf\data\user\User($deadUser['userID']);
if (CHAT_DISPLAY_JOIN_LEAVE) {
2013-01-19 19:36:40 +00:00
$userData['color'] = \chat\util\ChatUtil::readUserData('color', $user);
2012-04-18 19:01:38 +00:00
2013-01-19 19:36:40 +00:00
$messageAction = new data\message\MessageAction(array(), 'create', array(
2012-04-18 19:01:38 +00:00
'data' => array(
'roomID' => $deadUser['roomID'],
'sender' => $user->userID,
'username' => $user->username,
'time' => TIME_NOW,
2013-01-27 20:49:25 +00:00
'type' => data\message\Message::TYPE_LEAVE,
2012-04-18 19:01:38 +00:00
'message' => '',
'color1' => $userData['color'][1],
'color2' => $userData['color'][2]
)
));
$messageAction->executeAction();
}
2013-01-19 19:36:40 +00:00
\chat\util\ChatUtil::writeUserData(array('roomID' => null), $user);
2012-04-18 19:01:38 +00:00
}
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-01-19 19:36:40 +00:00
$this->messages = data\message\MessageList::getMessagesSince($this->room, \chat\util\ChatUtil::readUserData('lastSeen'));
2012-01-14 11:50:57 +00:00
// update last seen message
$sql = "SELECT
2012-11-23 19:33:27 +00:00
MAX(messageID)
FROM
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-01-19 19:36:40 +00:00
\chat\util\ChatUtil::writeUserData(array(
2012-11-23 19:33:27 +00:00
'lastSeen' => $stmt->fetchColumn(),
2012-04-15 19:37:20 +00:00
'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() {
2013-01-19 19:36:40 +00:00
$roomID = \chat\util\ChatUtil::readUserData('roomID');
$cache = data\room\Room::getCache();
2012-10-20 14:20:52 +00:00
if (!isset($cache[$roomID])) throw new IllegalLinkException();
2012-01-12 19:04:28 +00:00
2012-09-07 20:37:25 +00:00
$this->room = $cache[$roomID];
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');
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();
}
$json = array('users' => array(), 'messages' => array());
2011-12-26 16:01:24 +00:00
foreach ($this->messages as $message) {
$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' => (int) $user->userID,
2012-03-22 17:45:36 +00:00
'username' => $user->username,
2012-05-19 19:37:15 +00:00
'awayStatus' => $user->awayStatus,
'suspended' => (boolean) !$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
2012-01-12 19:04:28 +00:00
);
}
2012-01-14 11:50:57 +00:00
echo \wcf\util\JSON::encode($json);
2011-12-26 16:01:24 +00:00
exit;
}
}