1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2025-01-09 00:20:08 +00:00

Use WCF.Action.Proxy for message sending

Closes #17
This commit is contained in:
Tim Düsterhus 2013-03-11 15:17:43 +01:00
parent 7bfead22c9
commit daeb63d219
6 changed files with 130 additions and 259 deletions

View File

@ -539,15 +539,23 @@ window.console ?=
# text = @events.submit.fire text
$('#timsChatInput').val('').focus().keyup()
$.ajax $('#timsChatForm').attr('action'),
proxy = new WCF.Action.Proxy
autoSend: true
data:
actionName: 'send'
className: 'chat\\data\\message\\MessageAction'
parameters:
text: text
smilies: $('#timsChatSmilies').data 'status'
type: 'POST',
beforeSend: (jqXHR) ->
enableSmilies: $('#timsChatSmilies').data 'status'
showLoadingOverlay: false
success: =>
$('#timsChatInputContainer').removeClass('formError').find('.innerError').hide()
@getMessages()
complete: () ->
failure: (data) =>
return true if not (data?.returnValues?.errorType?)
$('#timsChatInputContainer').addClass('formError').find('.innerError').html(data.returnValues.errorType).show()
false
###
# Toggles between user- and room-list.
#

View File

@ -1,5 +1,9 @@
<?php
namespace chat\data\message;
use chat\data\room;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;
use wcf\util\MessageUtil;
/**
* Executes message related actions.
@ -35,4 +39,106 @@ public function prune() {
return call_user_func(array($this->className, 'deleteAll'), $objectIDs);
}
/**
*
*/
public function validateSend() {
// read parameters
$this->readString('text');
$this->readBoolean('enableSmilies');
$this->parameters['text'] = MessageUtil::stripCrap($this->parameters['text']);
// validate text
if (strlen($this->parameters['text']) > CHAT_MAX_LENGTH) throw new UserInputException('text', 'tooLong');
// search for censored words
if (ENABLE_CENSORSHIP) {
$result = \wcf\system\message\censorship\Censorship::getInstance()->test($this->parameters['text']);
if ($result) {
throw new UserInputException('message', WCF::getLanguage()->getDynamicVariable('wcf.message.error.censoredWordsFound', array('censoredWords' => $result)));
}
}
// read user data
$this->parameters['userData']['color'] = \chat\util\ChatUtil::readUserData('color');
$this->parameters['userData']['roomID'] = \chat\util\ChatUtil::readUserData('roomID');
$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 PermissionDeniedException();
// handle commands
$commandHandler = new \chat\system\command\CommandHandler($this->parameters['text']);
if ($commandHandler->isCommand()) {
try {
$command = $commandHandler->loadCommand();
if ($command->enableSmilies != \chat\system\command\ICommand::SETTING_USER) $this->parameters['enableSmilies'] = $command->enableSmilies;
$this->enableHTML = $command->enableHTML;
if ($command->enableBBCodes != \chat\system\command\ICommand::SETTING_USER) $this->enableBBCodes = $command->enableBBCodes;
$this->parameters['type'] = $command->getType();
$this->parameters['text'] = $command->getMessage();
$this->parameters['receiver'] = $command->getReceiver();
}
catch (\chat\system\command\NotFoundException $e) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.notFound', array('exception' => $e)));
}
catch (\chat\system\command\UserNotFoundException $e) {
throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.userNotFound', array('exception' => $e)));
}
}
else {
$this->parameters['type'] = Message::TYPE_NORMAL;
$this->parameters['receiver'] = null;
}
}
public function send() {
\chat\util\ChatUtil::writeUserData(array('away' => null));
// mark user as back
if ($this->parameters['userData']['away'] !== null) {
$messageAction = new MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['room']->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => Message::TYPE_BACK,
'message' => '',
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
$messageAction->executeAction();
}
$this->objectAction = new MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->parameters['room']->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'receiver' => $this->parameters['receiver'],
'time' => TIME_NOW,
'type' => $this->parameters['type'],
'message' => $this->parameters['text'],
//'enableSmilies' => $this->enableSmilies,
//'enableHTML' => $this->enableHTML,
//'enableBBCodes' => $this->enableBBCodes,
'color1' => $this->parameters['userData']['color'][1],
'color2' => $this->parameters['userData']['color'][2]
)
));
$this->objectAction->executeAction();
$returnValues = $this->objectAction->getReturnValues();
// add activity points
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.message', $returnValues['returnValues']->messageID, WCF::getUser()->userID);
}
}

View File

@ -1,235 +0,0 @@
<?php
namespace chat\form;
use \chat\data;
use \wcf\system\exception\UserInputException;
use \wcf\system\WCF;
use \wcf\util\StringUtil;
/**
* Inserts a message
*
* @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 form
*/
class ChatForm extends \wcf\form\AbstractForm {
/**
* Should HTML be enabled for this message.
*
* @var integer
*/
public $enableHTML = 0;
/**
* Should bbcodes be enabled for this message.
*
* @var integer
*/
public $enableBBCodes = CHAT_ENABLE_BBCODES;
/**
* Should smilies be enabled for this message.
*
* @var integer
*/
public $enableSmilies = 1;
/**
* @see wcf\page\AbstractPage::$loginRequired
*/
public $loginRequired = true;
/**
* @see \wcf\page\AbstractPage::$neededModules
*/
public $neededModules = array('CHAT_ACTIVE');
/**
* @see \wcf\page\AbstractPage::$neededPermissions
*/
public $neededPermissions = array();
/**
* The given message-string.
*
* @var string
*/
public $message = '';
/**
* The current room.
*
* @var \wcf\data\chat\room\ChatRoom
*/
public $room = null;
/**
* Values read from the UserStorage of the current user.
*
* @var array
*/
public $userData = array();
/**
* @see \wcf\page\AbstractForm::$useTemplate
*/
public $useTemplate = false;
/**
* shortcut for the active request
* @see wcf\system\request\Request::getRequestObject()
*/
public $request = null;
/**
* Disallows direct access.
*
* @see wcf\page\IPage::__run()
*/
public function __run() {
if (($this->request = \wcf\system\request\RequestHandler::getInstance()->getActiveRequest()->getRequestObject()) === $this) throw new \wcf\system\exception\IllegalLinkException();
parent::__run();
}
/**
* @see \wcf\page\IPage::readData()
*/
public function readData() {
$this->userData['color'] = \chat\util\ChatUtil::readUserData('color');
$this->userData['roomID'] = \chat\util\ChatUtil::readUserData('roomID');
$this->userData['away'] = \chat\util\ChatUtil::readUserData('away');
$cache = data\room\Room::getCache();
if (!isset($cache[$this->userData['roomID']])) throw new \wcf\system\exception\IllegalLinkException();
$this->room = $cache[$this->userData['roomID']];
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
if (!$this->room->canWrite()) throw new \wcf\system\exception\PermissionDeniedException();
parent::readData();
}
/**
* @see \wcf\form\IForm::readFormParameters()
*/
public function readFormParameters() {
parent::readFormParameters();
if (isset($_REQUEST['text'])) $this->message = \wcf\util\MessageUtil::stripCrap(StringUtil::trim($_REQUEST['text']));
if (isset($_REQUEST['smilies'])) $this->enableSmilies = intval($_REQUEST['smilies']);
}
/**
* @see \wcf\form\IForm::validate()
*/
public function validate() {
parent::validate();
if ($this->message === '') {
throw new UserInputException('text');
}
if (strlen($this->message) > CHAT_MAX_LENGTH) {
throw new UserInputException('text', 'tooLong');
}
}
/**
* @see \wcf\form\IForm::save()
*/
public function save() {
parent::save();
\chat\util\ChatUtil::writeUserData(array('away' => null));
$commandHandler = new \chat\system\command\CommandHandler($this->message);
if ($commandHandler->isCommand()) {
try {
$command = $commandHandler->loadCommand();
if ($command->enableSmilies != \chat\system\command\ICommand::SETTING_USER) $this->enableSmilies = $command->enableSmilies;
$this->enableHTML = $command->enableHTML;
if ($command->enableBBCodes != \chat\system\command\ICommand::SETTING_USER) $this->enableBBCodes = $command->enableBBCodes;
$type = $command->getType();
$this->message = $command->getMessage();
$receiver = $command->getReceiver();
}
catch (\chat\system\command\NotFoundException $e) {
$this->message = WCF::getLanguage()->get('chat.error.notFound');
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
}
catch (\chat\system\command\UserNotFoundException $e) {
$this->message = WCF::getLanguage()->getDynamicVariable('chat.error.userNotFound', array('username' => $e->getUsername()));
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
$this->enableHTML = 1;
}
catch (\wcf\system\exception\PermissionDeniedException $e) {
$this->message = WCF::getLanguage()->get('chat.error.permissionDenied');
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
}
catch (\Exception $e) {
$this->message = WCF::getLanguage()->get('chat.error.exception');
$type = data\message\Message::TYPE_ERROR;
$receiver = WCF::getUser()->userID;
}
}
else {
$type = data\message\Message::TYPE_NORMAL;
$receiver = null;
}
// mark user as back
if ($this->userData['away'] !== null) {
$messageAction = new data\message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => data\message\Message::TYPE_BACK,
'message' => '',
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
$messageAction->executeAction();
}
$this->objectAction = new data\message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'receiver' => $receiver,
'time' => TIME_NOW,
'type' => $type,
'message' => $this->message,
'enableSmilies' => $this->enableSmilies,
'enableHTML' => $this->enableHTML,
'enableBBCodes' => $this->enableBBCodes,
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
));
$this->objectAction->executeAction();
$returnValues = $this->objectAction->getReturnValues();
// add activity points
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.message', $returnValues['returnValues']->messageID, WCF::getUser()->userID);
$this->saved();
}
/**
* @see \wcf\page\IPage::show()
*/
public function show() {
header("HTTP/1.0 204 No Content");
parent::show();
}
}

View File

@ -79,13 +79,6 @@ class ChatPage extends \wcf\page\AbstractPage {
*/
public $userData = array();
/**
* The request that is actually handled.
*
* @var mixed
*/
public $request = null;
/**
* @see \wcf\page\IPage::assignVariables()
*/
@ -158,14 +151,6 @@ public function readData() {
public function readParameters() {
parent::readParameters();
$this->request = $this;
switch ($this->action) {
case 'Send':
$this->request = new \chat\form\ChatForm();
$this->request->__run();
exit;
}
if (isset($_REQUEST['id'])) $this->roomID = (int) $_REQUEST['id'];
if (isset($_REQUEST['ajax'])) $this->useTemplate = false;
}

View File

@ -92,7 +92,7 @@ Hinweis: Setzen Sie diese Einstellung nur, wenn Sie wissen, was sie bewirkt. Die
<category name="chat.error">
<item name="chat.error"><![CDATA[Fehler]]></item>
<item name="chat.error.notFound"><![CDATA[Der Befehl wurde nicht gefunden.]]></item>
<item name="chat.error.userNotFound"><![CDATA[Der Benutzer „{$username}“ wurde nicht gefunden.]]></item>
<item name="chat.error.userNotFound"><![CDATA[Der Benutzer „{$exception->getUsername()}“ wurde nicht gefunden.]]></item>
<item name="chat.error.permissionDenied"><![CDATA[Sie dürfen diesen Befehl nicht verwenden.]]></item>
<item name="chat.error.exception"><![CDATA[Es gab ein Problem bei der Ausführung dieses Befehls. Bitte wenden Sie sich an einen Administrator.]]></item>
</category>

View File

@ -92,14 +92,21 @@
<div id="timsChatTopic" class="container{if $room->topic|language === ''} empty{/if}">{$room->topic|language}</div>
<fieldset>
<div id="timsChatMessageContainer" class="timsChatMessageContainer container box shadow1">
<p class="error noJsOnly">{lang}chat.general.noJs{/lang}</p>
<p class="error noJsOnly" style="display: none;">{lang}chat.general.noJs{/lang}</p>
<ul>
</ul>
</div>
</fieldset>
<form id="timsChatForm" action="{link application='chat' controller='Chat' action='Send'}{/link}" method="post">
<fieldset>
<dl class="wide" id="timsChatInputContainer">
<dd>
<input id="timsChatInput" accesskey="w" type="text" class="inputText long" name="text" autocomplete="off" maxlength="{@CHAT_MAX_LENGTH}" disabled="disabled" required="required" placeholder="{lang}chat.general.submit.default{/lang}" />
<small class="innerError" style="display: none;">derp</small>
</dd>
</dl>
</fieldset>
</form>
<div id="timsChatControls" class="marginTop">