1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-26 22:20:08 +00:00
Tims-Chat/file/lib/data/room/RoomAction.class.php

366 lines
10 KiB
PHP
Raw Normal View History

2012-02-04 20:06:44 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\data\room;
use \chat\data\message;
2013-04-26 22:38:53 +00:00
use \chat\util\ChatUtil;
use \wcf\system\exception;
2012-03-03 20:36:52 +00:00
use \wcf\system\WCF;
2012-02-04 20:06:44 +00:00
/**
* Executes chatroom-related actions.
*
* @author Tim Düsterhus
2014-02-27 22:05:09 +00:00
* @copyright 2010-2014 Tim Düsterhus
2012-02-04 20:06:44 +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
* @subpackage data.room
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
class RoomAction extends \wcf\data\AbstractDatabaseObjectAction implements \wcf\data\ISortableAction {
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$className
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $className = '\chat\data\room\RoomEditor';
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $permissionsDelete = array('admin.chat.canDeleteRoom');
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $permissionsUpdate = array('admin.chat.canEditRoom');
2012-03-03 20:36:52 +00:00
2013-03-11 21:33:17 +00:00
/**
* Resets cache if any of the listed actions is invoked
* @var array<string>
*/
protected $resetCache = array('create', 'delete', 'toggle', 'update', 'updatePosition', 'prune');
2012-03-03 20:36:52 +00:00
/**
2012-03-15 19:11:01 +00:00
* Fixes create to append new rooms.
2012-03-03 20:36:52 +00:00
*/
public function create() {
$room = parent::create();
WCF::getDB()->beginTransaction();
2013-05-23 22:08:33 +00:00
$sql = "SELECT MAX(showOrder)
2012-05-19 18:33:25 +00:00
FROM ".call_user_func(array($this->className, 'getDatabaseTableName'))."
2012-03-03 20:40:43 +00:00
FOR UPDATE";
2012-03-03 20:36:52 +00:00
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
2012-05-19 18:33:25 +00:00
2013-01-19 19:36:40 +00:00
$editor = new RoomEditor($room);
2013-05-23 22:08:33 +00:00
$editor->update(array('showOrder' => ($stmt->fetchColumn() + 1)));
2012-03-03 20:36:52 +00:00
WCF::getDB()->commitTransaction();
return $room;
}
2012-05-19 18:33:25 +00:00
/**
* Deletes temporary rooms that are unused.
*
* @return integer Number of deleted rooms
*/
public function prune() {
$sql = "SELECT
".call_user_func(array($this->className, 'getDatabaseTableIndexName'))."
FROM
".call_user_func(array($this->className, 'getDatabaseTableName'))."
WHERE
permanent = ?
AND roomID NOT IN (
SELECT
fieldValue AS roomID
FROM
wcf".WCF_N."_user_storage
WHERE
2013-02-01 22:25:24 +00:00
field = ?
AND fieldValue IS NOT NULL
2012-05-19 18:33:25 +00:00
)";
$stmt = \wcf\system\WCF::getDB()->prepareStatement($sql);
$stmt->execute(array(0, 'roomID'));
2012-05-19 18:33:25 +00:00
$objectIDs = array();
2013-05-23 23:21:46 +00:00
while ($objectID = $stmt->fetchColumn()) $objectIDs[] = $objectID;
2012-05-19 18:33:25 +00:00
return call_user_func(array($this->className, 'deleteAll'), $objectIDs);
}
2012-03-03 20:36:52 +00:00
/**
2013-04-20 15:19:17 +00:00
* @see wcf\data\ISortableAction::validateUpdatePosition()
2012-03-03 20:36:52 +00:00
*/
public function validateUpdatePosition() {
// validate permissions
if (is_array($this->permissionsUpdate) && count($this->permissionsUpdate)) {
2012-08-07 21:56:57 +00:00
WCF::getSession()->checkPermissions($this->permissionsUpdate);
2012-03-03 20:36:52 +00:00
}
else {
throw new exception\PermissionDeniedException();
2012-03-03 20:36:52 +00:00
}
if (!isset($this->parameters['data']['structure'])) {
throw new exception\UserInputException('structure');
2012-03-03 20:36:52 +00:00
}
}
/**
2013-04-20 15:19:17 +00:00
* @see wcf\data\ISortableAction::updatePosition()
2012-03-03 20:36:52 +00:00
*/
public function updatePosition() {
2013-01-19 19:36:40 +00:00
$roomList = new RoomList();
2012-03-03 20:36:52 +00:00
$roomList->readObjects();
2013-04-05 20:28:12 +00:00
$i = 0;
2012-03-03 20:36:52 +00:00
WCF::getDB()->beginTransaction();
foreach ($this->parameters['data']['structure'][0] as $roomID) {
2012-03-03 22:16:24 +00:00
$room = $roomList->search($roomID);
if ($room === null) continue;
2013-04-05 20:28:12 +00:00
2013-01-19 19:36:40 +00:00
$editor = new RoomEditor($room);
2013-05-23 22:08:33 +00:00
$editor->update(array('showOrder' => $i++));
2012-03-03 20:36:52 +00:00
}
WCF::getDB()->commitTransaction();
}
2013-04-21 16:27:55 +00:00
/**
* Validates parameters and permissions.
*/
public function validateGetRoomList() {
if (!MODULE_CHAT) throw new exception\IllegalLinkException();
2013-04-26 22:38:53 +00:00
$this->parameters['room'] = RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID);
2013-04-21 16:27:55 +00:00
}
/**
* Returns the available rooms.
*/
public function getRoomList() {
2013-05-24 00:21:49 +00:00
$rooms = RoomCache::getInstance()->getRooms();
2013-04-21 16:27:55 +00:00
$result = array();
foreach ($rooms as $room) {
if (!$room->canEnter()) continue;
$result[] = array(
'title' => (string) $room,
'topic' => $room->getTopic(),
2013-04-21 16:27:55 +00:00
'link' => \wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
'application' => 'chat',
'object' => $room
)),
'roomID' => (int) $room->roomID,
'active' => $this->parameters['room'] && $room->roomID == $this->parameters['room']->roomID,
'userCount' => count($room->getUsers()),
'permissions' => array(
'canBan' => (boolean) $room->canBan(),
'canMute' => (boolean) $room->canMute()
)
2013-04-21 16:27:55 +00:00
);
}
return $result;
}
2013-04-26 22:38:53 +00:00
/**
* Validates parameters and permissions.
*/
public function validateJoin() {
if (!MODULE_CHAT) throw new exception\IllegalLinkException();
unset($this->parameters['user']);
$this->readInteger('roomID');
$room = RoomCache::getInstance()->getRoom($this->parameters['roomID']);
if ($room === null) throw new exception\UserInputException('roomID');
if (!$room->canEnter()) throw new exception\PermissionDeniedException();
}
/**
* Joins the room.
*/
public function join() {
// user cannot be set during an AJAX request but may be set by the chat itself
if (!isset($this->parameters['user'])) {
$this->parameters['user'] = WCF::getUser();
}
$room = RoomCache::getInstance()->getRoom($this->parameters['roomID']);
if ($room === null) throw new exception\UserInputException();
if (CHAT_DISPLAY_JOIN_LEAVE) {
if ($this->parameters['user']->chatRoomID) {
// leave message
$messageAction = new message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['user']->chatRoomID,
'sender' => $this->parameters['user']->userID,
'username' => $this->parameters['user']->username,
'time' => TIME_NOW,
'type' => message\Message::TYPE_LEAVE,
'message' => '',
'color1' => $this->parameters['user']->chatColor1,
'color2' => $this->parameters['user']->chatColor2,
'additionalData' => serialize(array('room' => $room))
)
));
$messageAction->executeAction();
}
$ipAddress = '';
2013-09-13 21:04:37 +00:00
if (LOG_IP_ADDRESS && $this->parameters['user']->userID == WCF::getUser()->userID) $ipAddress = \wcf\util\UserUtil::convertIPv6To4(\wcf\util\UserUtil::getIpAddress());
// join message
$messageAction = new message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $room->roomID,
'sender' => $this->parameters['user']->userID,
'username' => $this->parameters['user']->username,
'time' => TIME_NOW,
'type' => message\Message::TYPE_JOIN,
'message' => '',
'color1' => $this->parameters['user']->chatColor1,
'color2' => $this->parameters['user']->chatColor2,
'additionalData' => serialize(array('ipAddress' => $ipAddress))
)
));
$messageAction->executeAction();
}
$newestMessages = message\ViewableMessageList::getNewestMessages($room, CHAT_LASTMESSAGES + CHAT_DISPLAY_JOIN_LEAVE);
2013-06-23 15:59:14 +00:00
// update last seen message
$sql = "SELECT
MAX(messageID)
FROM
chat".WCF_N."_message";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
$editor = new \wcf\data\user\UserEditor($this->parameters['user']);
$editor->update(array(
'chatRoomID' => $room->roomID,
'chatAway' => null,
'chatLastActivity' => TIME_NOW,
2013-07-07 20:32:04 +00:00
'chatLastSeen' => $stmt->fetchColumn() ?: 0
));
// add activity points
$microtime = microtime(true) * 1000;
$result = $microtime & 0xFFFFFFFF;
if ($result > 0x7FFFFFFF) $result -= 0x80000000;
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.join', $result, WCF::getUser()->userID);
2014-03-02 16:11:53 +00:00
// send push message about join
\wcf\system\nodePush\NodePushHandler::getInstance()->sendMessage('be.bastelstu.chat.join');
$messages = array();
foreach ($newestMessages as $message) $messages[] = $message->jsonify(true);
return array(
'title' => (string) $room,
'topic' => $room->getTopic(),
'link' => \wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
'application' => 'chat',
'object' => $room
)),
2014-03-02 17:11:54 +00:00
'roomID' => (int) $room->roomID,
'userCount' => count($room->getUsers()),
'permissions' => array(
'canBan' => (boolean) $room->canBan(),
'canMute' => (boolean) $room->canMute()
),
'messages' => $messages,
);
}
2013-04-26 22:38:53 +00:00
/**
* Validates parameters and permissions.
*/
public function validateLeave() {
if (!MODULE_CHAT) throw new exception\IllegalLinkException();
2013-04-26 22:38:53 +00:00
unset($this->parameters['user']);
if (RoomCache::getInstance()->getRoom(WCF::getUser()->chatRoomID) === null) throw new exception\IllegalLinkException();
2013-04-26 22:38:53 +00:00
}
/**
* Leaves the room.
*/
public function leave() {
// user cannot be set during an AJAX request but may be set by the chat itself
2013-04-26 22:38:53 +00:00
if (!isset($this->parameters['user'])) {
$this->parameters['user'] = WCF::getUser();
}
$room = RoomCache::getInstance()->getRoom($this->parameters['user']->chatRoomID);
if ($room === null) throw new exception\UserInputException();
2013-04-26 22:38:53 +00:00
if (CHAT_DISPLAY_JOIN_LEAVE) {
// leave message
$messageAction = new message\MessageAction(array(), 'create', array(
2013-04-26 22:38:53 +00:00
'data' => array(
2013-05-24 00:21:49 +00:00
'roomID' => $room->roomID,
2013-04-26 22:38:53 +00:00
'sender' => $this->parameters['user']->userID,
'username' => $this->parameters['user']->username,
'time' => TIME_NOW,
'type' => message\Message::TYPE_LEAVE,
'message' => '',
'color1' => $this->parameters['user']->chatColor1,
'color2' => $this->parameters['user']->chatColor2
2013-04-26 22:38:53 +00:00
)
));
$messageAction->executeAction();
}
// set current room to null
$editor = new \wcf\data\user\UserEditor($this->parameters['user']);
$editor->update(array(
'chatRoomID' => null
));
2013-05-02 20:43:52 +00:00
\wcf\system\nodePush\NodePushHandler::getInstance()->sendMessage('be.bastelstu.chat.join');
}
2013-05-03 18:02:18 +00:00
/**
* Forces dead users to leave the chat.
*/
public function removeDeadUsers() {
$deadUsers = Room::getDeadUsers();
foreach ($deadUsers as $deadUser) {
$roomAction = new self(array(), 'leave', array(
'user' => $deadUser
));
$roomAction->executeAction();
}
}
2013-05-02 20:43:52 +00:00
/**
* Validates permissions.
*/
public function validateGetBoxRoomList() {
if (!MODULE_CHAT) throw new \wcf\system\exception\IllegalLinkException();
2013-05-02 20:43:52 +00:00
}
/**
* Returns dashboard roomlist.
*/
public function getBoxRoomList() {
2013-05-24 00:21:49 +00:00
$rooms = RoomCache::getInstance()->getRooms();
2013-05-02 20:43:52 +00:00
foreach ($rooms as $key => $room) {
if (!$room->canEnter()) unset($rooms[$key]);
}
$this->readBoolean('showEmptyRooms', true);
2013-05-02 20:43:52 +00:00
\wcf\system\WCF::getTPL()->assign(array(
'rooms' => $rooms,
'showEmptyRooms' => $this->parameters['showEmptyRooms']
2013-05-02 20:43:52 +00:00
));
return array(
'template' => \wcf\system\WCF::getTPL()->fetch('boxRoomList', 'chat')
2013-05-02 20:43:52 +00:00
);
2013-04-26 22:38:53 +00:00
}
2012-02-04 20:06:44 +00:00
}