From 43f7df6c76865f28a82716db624bac841e1b64a4 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 5 Nov 2014 15:28:19 +0000 Subject: [PATCH] Remove Thread::setupAvatar method --- src/mibew/js/source/chat/models/avatar.js | 39 ++++++++++++++++++- .../RequestProcessor/ThreadProcessor.php | 26 +++++++++++++ src/mibew/libs/classes/Mibew/Thread.php | 34 ---------------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/src/mibew/js/source/chat/models/avatar.js b/src/mibew/js/source/chat/models/avatar.js index f326bf43..a0bcb8ab 100644 --- a/src/mibew/js/source/chat/models/avatar.js +++ b/src/mibew/js/source/chat/models/avatar.js @@ -54,6 +54,13 @@ _.bind(this.apiSetupAvatar, this) ) ); + + // Update avatar if operator changed. + Mibew.Objects.Models.thread.on( + 'change:agentId', + this.setFromThread, + this + ); }, // Model finalizer @@ -72,9 +79,37 @@ * @param args {Object} An object of passed arguments */ apiSetupAvatar: function(args) { - if (args.imageLink) { - this.set({imageLink: args.imageLink}); + this.set({imageLink: (args.imageLink || false)}); + }, + + /** + * Sets avatar based on data from the thread. + * @param {Object} thread An instance of Mibew.Models.Thread + */ + setFromThread: function(thread) { + if (!thread.get('agentId')) { + // There is no operator. Hide the avatar. + this.set({imageLink: false}); + + return; } + + // Request operator's avatar at the server side + Mibew.Objects.server.callFunctions( + [{ + 'function': 'getAvatar', + 'arguments': { + 'references': {}, + 'return': { + 'imageLink': 'imageLink' + }, + 'threadId': thread.get('id'), + 'token': thread.get('token') + } + }], + _.bind(this.apiSetupAvatar, this), + true + ); } } ); diff --git a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php index 01e8d2c6..abd08ab3 100644 --- a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php +++ b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php @@ -727,4 +727,30 @@ class ThreadProcessor extends ClientSideProcessor implements RouterAwareInterfac mibew_mail($inbox_mail, $email, $subject, $body); } } + + /** + * Returns relative path of the avatar for operator related with the thread. + * + * @param array $args Associative array of arguments. It must contains the + * following keys: + * - 'threadId': ID of the thread the avatar should be retrieved for. + * - 'token': Token of the thread. + * @return array Array of results. It contains following keys: + * - 'imageLink': string, relative path to operator's avatar. + */ + protected function apiGetAvatar($args) + { + // Load thread and check thread's last token + $thread = self::getThread($args['threadId'], $args['token']); + + $image_link = false; + if ($thread->agentId) { + $operator = operator_by_id($thread->agentId); + if ($operator['vcavatar']) { + $image_link = $operator['vcavatar']; + } + } + + return array('imageLink' => $image_link); + } } diff --git a/src/mibew/libs/classes/Mibew/Thread.php b/src/mibew/libs/classes/Mibew/Thread.php index 149ae38c..fb3f5d3a 100644 --- a/src/mibew/libs/classes/Mibew/Thread.php +++ b/src/mibew/libs/classes/Mibew/Thread.php @@ -19,10 +19,8 @@ namespace Mibew; -// Import namespaces and classes of the core use Mibew\EventDispatcher\EventDispatcher; use Mibew\EventDispatcher\Events; -use Mibew\RequestProcessor\ThreadProcessor; /** * Represents a chat thread @@ -783,9 +781,6 @@ class Thread // Send messages $this->postMessage(self::KIND_EVENTS, $message_to_post); - $this->setupAvatar( - $operator['vcavatar'] ? $operator['vcavatar'] : "" - ); } } @@ -1065,9 +1060,6 @@ class Thread // Send message if ($message) { $this->postMessage(self::KIND_EVENTS, $message); - $this->setupAvatar( - $operator['vcavatar'] ? $operator['vcavatar'] : "" - ); } return true; @@ -1236,30 +1228,4 @@ class Thread return $token; } - - /** - * Set operator avatar in the user's chat window - * - * @param string $link URL of the new operator avatar - */ - protected function setupAvatar($link) - { - $processor = ThreadProcessor::getInstance(); - $processor->call( - array( - array( - 'function' => 'setupAvatar', - 'arguments' => array( - 'threadId' => $this->id, - 'token' => $this->lastToken, - 'return' => array(), - 'references' => array(), - 'recipient' => 'user', - 'imageLink' => $link, - ), - ), - ), - true - ); - } }