Remove Thread::setupAvatar method

This commit is contained in:
Dmitriy Simushev 2014-11-05 15:28:19 +00:00
parent 03328b9293
commit 43f7df6c76
3 changed files with 63 additions and 36 deletions

View File

@ -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
);
}
}
);

View File

@ -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);
}
}

View File

@ -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
);
}
}