1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00

Improved text/plain output.

This commit is contained in:
Tim Düsterhus 2012-11-20 16:01:32 +01:00
parent 92d63b9124
commit 626911953d
4 changed files with 87 additions and 27 deletions

View File

@ -1,11 +1,13 @@
<?php
namespace wcf\data\chat\message;
use \wcf\system\Regex;
use \wcf\system\WCF;
use \wcf\util\ChatUtil;
/**
* Represents a chat message.
*
* @author Tim Düsterhus
* @author Tim Düsterhus
* @copyright 2010-2012 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package be.bastelstu.wcf.chat
@ -36,6 +38,12 @@ class ChatMessage extends \wcf\data\DatabaseObject {
const TYPE_GLOBALMESSAGE = 11;
const TYPE_ERROR = 12;
/**
* cache for users
* @var array<\wcf\data\user\User>
*/
protected static $users = array();
/**
* @see \wcf\data\chat\message\ChatMessage::getFormattedMessage()
*/
@ -51,21 +59,21 @@ public function __toString() {
*/
public function getFormattedMessage($outputType = 'text/html') {
$message = $this->message;
switch ($this->type) {
case self::TYPE_JOIN:
case self::TYPE_LEAVE:
case self::TYPE_BACK:
case self::TYPE_AWAY:
WCF::getTPL()->assign(@unserialize($message));
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type);
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type, unserialize($message) ?: array());
break;
case self::TYPE_MODERATE:
$message = @unserialize($message);
WCF::getTPL()->assign($message);
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type.'.'.$message['type']);
$message = unserialize($message);
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type.'.'.$message['type'], $message ?: array());
$message = self::replaceUserLink($message, $outputType);
break;
case self::TYPE_WHISPER:
$message = @unserialize($message);
$message = unserialize($message);
$message = $message['message'];
case self::TYPE_NORMAL:
case self::TYPE_ME:
@ -73,21 +81,33 @@ public function getFormattedMessage($outputType = 'text/html') {
$message = \wcf\system\bbcode\SimpleMessageParser::getInstance()->parse($message, true, $this->enableSmilies);
}
break;
default:
if ($this->enableHTML) {
$message = self::replaceUserLink($message, $outputType);
}
break;
}
return $message;
}
/**
* Returns the formatted username
*
* Returns the username.
*
* @param boolean $colored
* @return string
*/
public function getFormattedUsername() {
$username = $this->getUsername();
public function getUsername($colored = false) {
$username = $this->username;
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information');
if ($this->type == self::TYPE_ERROR) return WCF::getLanguage()->get('wcf.chat.error');
if ($colored) {
$username = \wcf\util\ChatUtil::gradient($username, $this->color1, $this->color2);
}
if ($this->type != self::TYPE_INFORMATION && $this->type != self::TYPE_ERROR) $username = \wcf\util\ChatUtil::gradient($username, $this->color1, $this->color2);
if ($this->type == self::TYPE_WHISPER) {
$message = @unserialize($this->message);
$message = unserialize($this->message);
$username .= ' -> '.$message['username'];
}
@ -95,15 +115,42 @@ public function getFormattedUsername() {
}
/**
* Returns the unformatted username.
*
* @return string
* Replaces a userLink in a message.
*/
public function getUsername() {
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information');
if ($this->type == self::TYPE_ERROR) return WCF::getLanguage()->get('wcf.chat.error');
public static function replaceUserLink($message, $outputType) {
static $regex = null;
if ($regex === null) $regex = new Regex('<span class="userLink" data-user-id="(\d+)" />');
return $this->username;
if ($outputType === 'text/html') {
return $regex->replace($message, new \wcf\system\Callback(function ($matches) {
return self::getUserLink($matches[1]);
}));
}
else {
return $regex->replace($message, new \wcf\system\Callback(function ($matches) {
self::getUserLink($matches[1]);
return self::$users[$matches[1]]->username;
}));
}
}
/**
* Returns a fully colored userlink.
*/
public static function getUserLink($userID) {
if (!isset(self::$users[$userID])) {
self::$users[$userID] = $user = new \wcf\data\user\User($userID);
// Username + link to profile
$color = ChatUtil::readUserData('color', $user);
$profile = \wcf\system\request\LinkHandler::getInstance()->getLink('User', array(
'object' => $user
));
self::$users[$userID]->userLink = '<a href="'.$profile.'" class="userLink" data-user-id="'.$user->userID.'">'.ChatUtil::gradient($user->username, $color[1], $color[2]).'</a>';
}
return self::$users[$userID]->userLink;
}
/**
@ -113,11 +160,23 @@ public function getUsername() {
* @return string
*/
public function jsonify($raw = false) {
switch ($this->type) {
case self::TYPE_NORMAL:
case self::TYPE_ERROR:
case self::TYPE_INFORMATION:
case self::TYPE_WHISPER:
$separator = ':';
break;
default:
$separator = ' ';
break;
}
$array = array(
'formattedUsername' => $this->getFormattedUsername(),
'formattedMessage' => (string) $this,
'formattedUsername' => $this->getUsername(true),
'formattedMessage' => $this->getFormattedMessage(),
'formattedTime' => \wcf\util\DateUtil::format(\wcf\util\DateUtil::getDateTimeByTimestamp($this->time), 'H:i:s'),
'separator' => ($this->type == self::TYPE_NORMAL || $this->type == self::TYPE_ERROR || $this->type == self::TYPE_INFORMATION) ? ': ' : ' ',
'separator' => $separator,
'message' => $this->getFormattedMessage('text/plain'),
'sender' => (int) $this->sender,
'username' => $this->getUsername(),

View File

@ -28,9 +28,9 @@ public function __construct(\wcf\system\chat\command\CommandHandler $commandHand
// Username + link to profile
$color = ChatUtil::readUserData('color', $this->user);
$profile = \wcf\system\request\LinkHandler::getInstance()->getLink('User', array(
'object' => $this->user
'object' => $this->user
));
$this->lines[WCF::getLanguage()->get('wcf.user.username')] = '<a href="'.$profile.'" class="userLink" data-user-id="'.$this->user->userID.'">'.ChatUtil::gradient($this->user->username, $color[1], $color[2]).'</a>';
$this->lines[WCF::getLanguage()->get('wcf.user.username')] = '<span class="userLink" data-user-id="'.$this->user->userID.'" />';
// Away-Status
if (ChatUtil::readUserData('away', $this->user) !== null) {

View File

@ -145,7 +145,7 @@ public static function nodePushRunning() {
/**
* Reads user data.
*
* @param string $field
* @param string $field
* @param \wcf\data\user\User $user
* @return mixed
*/

View File

@ -61,6 +61,7 @@ Hinweis: Setzen Sie diese Einstellung nur, wenn Sie wissen, was sie bewirkt. Die
<item name="wcf.chat.title"><![CDATA[Chat]]></item>
<item name="wcf.chat.protocol"><![CDATA[Protokoll]]></item>
<item name="wcf.chat.room"><![CDATA[Raum]]></item>
<item name="wcf.chat.rooms"><![CDATA[Räume]]></item>
<item name="wcf.chat.users"><![CDATA[Nutzer]]></item>
@ -106,7 +107,7 @@ Hinweis: Setzen Sie diese Einstellung nur, wenn Sie wissen, was sie bewirkt. Die
<item name="wcf.chat.message.4"><![CDATA[ist jetzt wieder da.]]></item>
<!-- 5 = TYPE_MODERATE -->
<item name="wcf.chat.message.5.restore"><![CDATA[hat {@$link} zurückgesetzt.]]></item>
<item name="wcf.chat.message.5.mute"><![CDATA[hat {@$link} bis {@$until|time} geknebelt.]]></item>
<item name="wcf.chat.message.5.mute"><![CDATA[hat {@$link} bis {@$until|plainTime} geknebelt.]]></item>
<!-- 7 = TYPE_WHISPER -->
<item name="wcf.chat.message.7"><![CDATA[flüstert{if $showReceiver} an {$username}{/if}:]]></item>