From 9e30cb66e7b9224668d6cabca3c5575de46d4f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 25 May 2013 00:50:50 +0200 Subject: [PATCH] Allow fetching avatars for chat messages --- file/lib/data/message/Message.class.php | 2 +- .../data/message/ViewableMessage.class.php | 54 +++++++++++++++++++ .../message/ViewableMessageList.class.php | 35 ++++++++++++ file/lib/page/NewMessagesPage.class.php | 8 ++- 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 file/lib/data/message/ViewableMessage.class.php create mode 100644 file/lib/data/message/ViewableMessageList.class.php diff --git a/file/lib/data/message/Message.class.php b/file/lib/data/message/Message.class.php index a04d6f5..559dd54 100644 --- a/file/lib/data/message/Message.class.php +++ b/file/lib/data/message/Message.class.php @@ -44,7 +44,7 @@ class Message extends \chat\data\CHATDatabaseObject { protected static $users = array(); /** - * @see \chat\data\\message\Message::getFormattedMessage() + * @see \chat\data\message\Message::getFormattedMessage() */ public function __toString() { return $this->getFormattedMessage(); diff --git a/file/lib/data/message/ViewableMessage.class.php b/file/lib/data/message/ViewableMessage.class.php new file mode 100644 index 0000000..ec590b4 --- /dev/null +++ b/file/lib/data/message/ViewableMessage.class.php @@ -0,0 +1,54 @@ + + * @package be.bastelstu.chat + * @subpackage data.message + */ +class ViewableMessage extends \wcf\data\DatabaseObjectDecorator { + /** + * @see \wcf\data\DatabaseObjectDecorator::$baseClass + */ + protected static $baseClass = 'chat\data\message\Message'; + + /** + * user profile object + * @var \wcf\data\user\UserProfile + */ + protected $userProfile = null; + + /** + * Returns the profile object of the user who created the post. + * + * @return wcf\data\user\UserProfile + */ + public function getUserProfile() { + if ($this->userProfile === null) { + $this->userProfile = new \wcf\data\user\UserProfile(new \wcf\data\user\User(null, $this->getDecoratedObject()->data)); + } + + return $this->userProfile; + } + + /** + * @see \chat\data\message\Message::jsonify() + */ + public function jsonify($raw = false) { + $array = parent::jsonify(true); + + $array['avatar'] = array( + 16 => $this->getUserProfile()->getAvatar()->getImageTag(16), + 24 => $this->getUserProfile()->getAvatar()->getImageTag(24), + 32 => $this->getUserProfile()->getAvatar()->getImageTag(32), + 48 => $this->getUserProfile()->getAvatar()->getImageTag(48) + ); + + if ($raw) return $array; + return \wcf\util\JSON::encode($array); + } +} diff --git a/file/lib/data/message/ViewableMessageList.class.php b/file/lib/data/message/ViewableMessageList.class.php new file mode 100644 index 0000000..2870cc5 --- /dev/null +++ b/file/lib/data/message/ViewableMessageList.class.php @@ -0,0 +1,35 @@ + + * @package be.bastelstu.chat + * @subpackage chat.room + */ +class ViewableMessageList extends MessageList { + /** + * @see \wcf\data\DatabaseObjectList::$decoratorClassName + */ + public $decoratorClassName = 'chat\data\message\ViewableMessage'; + + /** + * @see \wcf\data\DatabaseObjectList::__construct() + */ + public function __construct() { + parent::__construct(); + + $this->sqlSelects .= "user_avatar.*, user_option_value.*, user_table.*"; + $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user user_table ON (user_table.userID = message.sender)"; + $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_avatar user_avatar ON (user_avatar.avatarID = user_table.avatarID)"; + $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)"; + + if (MODULE_USER_RANK) { + $this->sqlSelects .= ",user_rank.*"; + $this->sqlJoins .= " LEFT JOIN wcf".WCF_N."_user_rank user_rank ON (user_rank.rankID = user_table.rankID)"; + } + } +} diff --git a/file/lib/page/NewMessagesPage.class.php b/file/lib/page/NewMessagesPage.class.php index 0ed91ff..2a3399b 100644 --- a/file/lib/page/NewMessagesPage.class.php +++ b/file/lib/page/NewMessagesPage.class.php @@ -73,7 +73,7 @@ public function readData() { * Fetches the new messages */ public function readMessages() { - $this->messages = data\message\MessageList::getMessagesSince($this->room, WCF::getUser()->chatLastSeen); + $this->messages = data\message\ViewableMessageList::getMessagesSince($this->room, WCF::getUser()->chatLastSeen); // update last seen message $sql = "SELECT @@ -142,7 +142,11 @@ public function show() { $items = array(); if (ENABLE_DEBUG_MODE) { foreach ($b->getItems() as $item) { - $items[] = array('text' => $item['text'], 'use' => $item['use']); + $items[] = array( + 'text' => $item['text'], + 'use' => $item['use'], + 'trace' => $item['trace'] + ); } }