Extract redirection logic from "Chat\Operator\ChatController"

This commit is contained in:
Dmitriy Simushev 2014-06-02 13:05:34 +00:00
parent 7ceb242814
commit e31b03ccdf
8 changed files with 98 additions and 44 deletions

View File

@ -5,5 +5,5 @@
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
(function(a,d,e){a.Views.RedirectControl=a.Views.Control.extend({template:d.templates.chat_controls_redirect,events:e.extend({},a.Views.Control.prototype.events,{click:"redirect"}),initialize:function(){a.Objects.Models.user.on("change",this.render,this)},serializeData:function(){var b=this.model.toJSON();b.user=a.Objects.Models.user.toJSON();return b},redirect:function(){var b=a.Objects.Models.user;if(b.get("isAgent")&&b.get("canPost")&&(b=this.model.get("link"))){var c=a.Objects.Models.page.get("style"); (function(b,e,f){b.Views.RedirectControl=b.Views.Control.extend({template:e.templates.chat_controls_redirect,events:f.extend({},b.Views.Control.prototype.events,{click:"redirect"}),initialize:function(){b.Objects.Models.user.on("change",this.render,this)},serializeData:function(){var a=this.model.toJSON();a.user=b.Objects.Models.user.toJSON();return a},redirect:function(){var a=b.Objects.Models.user;if(a.get("isAgent")&&a.get("canPost")&&(a=this.model.get("link"))){var c=b.Objects.Models.page.get("style"),
window.location.href=b.replace(/\&amp\;/g,"&")+(c?"&style="+c:"")}}})})(Mibew,Handlebars,_); d="";c&&(d=(-1===a.indexOf("?")?"?":"&")+"style="+c);window.location.href=a.replace(/\&amp\;/g,"&")+d}}})})(Mibew,Handlebars,_);

View File

@ -208,8 +208,8 @@ changeGroupDescription:function(){var a=this.ui.groupSelect.prop("selectedIndex"
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
(function(a,d,e){a.Views.RedirectControl=a.Views.Control.extend({template:d.templates.chat_controls_redirect,events:e.extend({},a.Views.Control.prototype.events,{click:"redirect"}),initialize:function(){a.Objects.Models.user.on("change",this.render,this)},serializeData:function(){var b=this.model.toJSON();b.user=a.Objects.Models.user.toJSON();return b},redirect:function(){var b=a.Objects.Models.user;if(b.get("isAgent")&&b.get("canPost")&&(b=this.model.get("link"))){var c=a.Objects.Models.page.get("style"); (function(b,e,f){b.Views.RedirectControl=b.Views.Control.extend({template:e.templates.chat_controls_redirect,events:f.extend({},b.Views.Control.prototype.events,{click:"redirect"}),initialize:function(){b.Objects.Models.user.on("change",this.render,this)},serializeData:function(){var a=this.model.toJSON();a.user=b.Objects.Models.user.toJSON();return a},redirect:function(){var a=b.Objects.Models.user;if(a.get("isAgent")&&a.get("canPost")&&(a=this.model.get("link"))){var c=b.Objects.Models.page.get("style"),
window.location.href=b.replace(/\&amp\;/g,"&")+(c?"&style="+c:"")}}})})(Mibew,Handlebars,_); d="";c&&(d=(-1===a.indexOf("?")?"?":"&")+"style="+c);window.location.href=a.replace(/\&amp\;/g,"&")+d}}})})(Mibew,Handlebars,_);
/* /*
Copyright 2005-2014 the original author or authors. Copyright 2005-2014 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License"). Licensed under the Apache License, Version 2.0 (the "License").

View File

@ -62,8 +62,13 @@
if (link) { if (link) {
// Redirect browser to user redirection page // Redirect browser to user redirection page
var style = Mibew.Objects.Models.page.get('style'); var style = Mibew.Objects.Models.page.get('style');
var styleArg = '';
if (style) {
styleArg = ((link.indexOf('?') === -1) ? '?' : '&')
+ 'style=' + style;
}
window.location.href = link.replace(/\&amp\;/g, '&') window.location.href = link.replace(/\&amp\;/g, '&')
+ (style ? ('&style=' + style) : ''); + styleArg;
} }
} }
} }

View File

@ -613,10 +613,8 @@ function setup_chatview_for_operator(Thread $thread, $operator)
$data['chat']['messageForm']['predefinedAnswers'] = $predefined_answers; $data['chat']['messageForm']['predefinedAnswers'] = $predefined_answers;
} }
// Set link to user redirection page // Set link to user redirection page
$params = "thread=" . $thread->id . "&token=" . $thread->lastToken; $data['chat']['links']['redirect'] = MIBEW_WEB_ROOT . "/operator/chat/"
$data['chat']['links']['redirect'] = MIBEW_WEB_ROOT . "/operator/chat?" . $thread->id . '/' . $thread->lastToken . '/redirection-links';
. $params
. "&redirect=1";
$data['namePostfix'] = ""; $data['namePostfix'] = "";

View File

@ -76,21 +76,11 @@ class ChatController extends AbstractController
$page = setup_chatview_for_operator($thread, $operator); $page = setup_chatview_for_operator($thread, $operator);
if ($request->query->get('redirect')) { // Build js application options
$page = array_merge_recursive( $page['chatOptions'] = json_encode($page['chat']);
$page,
setup_redirect_links($thread_id, $operator, $token)
);
// Render the page with redirection links. // Render the page with chat.
return $this->render('redirect', $page); return $this->render('chat', $page);
} else {
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
// Render the page with chat.
return $this->render('chat', $page);
}
} }
/** /**

View File

@ -20,6 +20,7 @@ namespace Mibew\Controller\Chat\Operator;
use Mibew\Controller\Chat\AbstractController; use Mibew\Controller\Chat\AbstractController;
use Mibew\Database; use Mibew\Database;
use Mibew\Http\Exception\BadRequestException; use Mibew\Http\Exception\BadRequestException;
use Mibew\Http\Exception\NotFoundException;
use Mibew\Thread; use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -29,31 +30,64 @@ use Symfony\Component\HttpFoundation\Request;
class RedirectController extends AbstractController class RedirectController extends AbstractController
{ {
/** /**
* Process chat's redirections. * Renders a page with redirections links.
* *
* @param Request $request Incoming request. * @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered * @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered
* page content or a redirect response. * page content or a redirect response.
* @throws BadRequestException If the thread cannot be loaded by some * @throws NotFoundException If the thread with specified ID and token is
* reasons. * not found.
*/ */
public function indexAction(Request $request) public function showRedirectionLinksAction(Request $request)
{ {
// Get and validate thread id // Check if we should force the user to use SSL
$thread_id = $request->query->get('thread'); $ssl_redirect = $this->sslRedirect($request);
if (!preg_match("/^\d{1,10}$/", $thread_id)) { if ($ssl_redirect !== false) {
throw new BadRequestException('Wrong value of "thread" argument.'); return $ssl_redirect;
} }
// Get and validate token $operator = $this->getOperator();
$token = $request->query->get('token'); $thread_id = $request->attributes->get('thread_id');
if (!preg_match("/^\d{1,10}$/", $token)) { $token = $request->attributes->get('token');
throw new BadRequestException('Wrong value of "token" argument.');
}
$thread = Thread::load($thread_id, $token); $thread = Thread::load($thread_id, $token);
if (!$thread) { if (!$thread) {
throw new BadRequestException('Wrong thread.'); throw new NotFoundException('The thread is not found.');
}
if ($thread->agentId != $operator['operatorid']) {
$page = array('errors' => array('Can redirect only own threads.'));
return $this->render('error', $page);
}
$page = array_merge_recursive(
setup_chatview_for_operator($thread, $operator),
setup_redirect_links($this->getRouter()->getGenerator(), $thread_id, $operator, $token)
);
// Render the page with redirection links.
return $this->render('redirect', $page);
}
/**
* Process chat thread redirection.
*
* @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered
* page content or a redirect response.
* @throws NotFoundException If the thread with specified ID and token is
* not found.
* @throws BadRequestException If one or more arguments have a wrong format.
*/
public function redirectAction(Request $request)
{
$thread_id = $request->attributes->get('thread_id');
$token = $request->attributes->get('token');
$thread = Thread::load($thread_id, $token);
if (!$thread) {
throw new NotFoundException('The thread is not found.');
} }
$page = array( $page = array(
@ -109,6 +143,13 @@ class RedirectController extends AbstractController
} }
} }
/**
* Redirects a chat thread to the group with the specified ID.
*
* @param \Mibew\Thread $thread Chat thread to redirect.
* @param int $group_id ID of the target group.
* @return boolean True if the thread was redirected and false on failure.
*/
protected function redirectToGroup(Thread $thread, $group_id) protected function redirectToGroup(Thread $thread, $group_id)
{ {
if ($thread->state != Thread::STATE_CHATTING) { if ($thread->state != Thread::STATE_CHATTING) {
@ -138,6 +179,13 @@ class RedirectController extends AbstractController
return true; return true;
} }
/**
* Redirects a chat thread to the operator with the specified ID.
*
* @param \Mibew\Thread $thread Chat thread to redirect.
* @param int $group_id ID of the target operator.
* @return boolean True if the thread was redirected and false on failure.
*/
protected function redirectToOperator(Thread $thread, $operator_id) protected function redirectToOperator(Thread $thread, $operator_id)
{ {
if ($thread->state != Thread::STATE_CHATTING) { if ($thread->state != Thread::STATE_CHATTING) {

View File

@ -19,6 +19,7 @@
use Mibew\Database; use Mibew\Database;
use Mibew\EventDispatcher; use Mibew\EventDispatcher;
use Mibew\Settings; use Mibew\Settings;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/** /**
* Name of the cookie to remember an operator * Name of the cookie to remember an operator
@ -603,7 +604,7 @@ function get_logged_in()
: false; : false;
} }
function setup_redirect_links($threadid, $operator, $token) function setup_redirect_links(UrlGeneratorInterface $url_generator, $threadid, $operator, $token)
{ {
$result = array(); $result = array();
@ -637,7 +638,7 @@ function setup_redirect_links($threadid, $operator, $token)
$groups = array_slice($groups, $p['start'], $p['end'] - $p['start']); $groups = array_slice($groups, $p['start'], $p['end'] - $p['start']);
$agent_list = ""; $agent_list = "";
$params = array('thread' => $threadid, 'token' => $token); $params = array('thread_id' => $threadid, 'token' => $token);
foreach ($operators as $agent) { foreach ($operators as $agent) {
$params['nextAgent'] = $agent['operatorid']; $params['nextAgent'] = $agent['operatorid'];
$status = $agent['time'] < Settings::get('online_timeout') $status = $agent['time'] < Settings::get('online_timeout')
@ -645,7 +646,7 @@ function setup_redirect_links($threadid, $operator, $token)
? getlocal("char.redirect.operator.online_suff") ? getlocal("char.redirect.operator.online_suff")
: getlocal("char.redirect.operator.away_suff")) : getlocal("char.redirect.operator.away_suff"))
: ""; : "";
$agent_list .= "<li><a href=\"" . add_params(MIBEW_WEB_ROOT . "/operator/chat/redirect", $params) $agent_list .= "<li><a href=\"" . $url_generator->generate('chat_operator_redirect', $params)
. "\" title=\"" . get_operator_name($agent) . "\">" . "\" title=\"" . get_operator_name($agent) . "\">"
. get_operator_name($agent) . get_operator_name($agent)
. "</a> $status</li>"; . "</a> $status</li>";
@ -654,13 +655,13 @@ function setup_redirect_links($threadid, $operator, $token)
$group_list = ""; $group_list = "";
if (Settings::get('enablegroups') == "1") { if (Settings::get('enablegroups') == "1") {
$params = array('thread' => $threadid, 'token' => $token); $params = array('thread_id' => $threadid, 'token' => $token);
foreach ($groups as $group) { foreach ($groups as $group) {
$params['nextGroup'] = $group['groupid']; $params['nextGroup'] = $group['groupid'];
$status = group_is_online($group) $status = group_is_online($group)
? getlocal("char.redirect.operator.online_suff") ? getlocal("char.redirect.operator.online_suff")
: (group_is_away($group) ? getlocal("char.redirect.operator.away_suff") : ""); : (group_is_away($group) ? getlocal("char.redirect.operator.away_suff") : "");
$group_list .= "<li><a href=\"" . add_params(MIBEW_WEB_ROOT . "/operator/chat/redirect", $params) $group_list .= "<li><a href=\"" . $url_generator->generate('chat_operator_redirect', $params)
. "\" title=\"" . get_group_name($group) . "\">" . "\" title=\"" . get_group_name($group) . "\">"
. get_group_name($group) . get_group_name($group)
. "</a> $status</li>"; . "</a> $status</li>";

View File

@ -8,10 +8,22 @@ chat_operator:
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
chat_operator_redirect: chat_operator_redirect:
path: /operator/chat/redirect path: /operator/chat/{thread_id}/{token}/redirect
defaults: defaults:
_controller: Mibew\Controller\Chat\Operator\RedirectController::indexAction _controller: Mibew\Controller\Chat\Operator\RedirectController::redirectAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
requirements:
thread_id: \d{1,10}
token: \d{1,10}
chat_operator_redirection_links:
path: /operator/chat/{thread_id}/{token}/redirection-links
defaults:
_controller: Mibew\Controller\Chat\Operator\RedirectController::showRedirectionLinksAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
requirements:
thread_id: \d{1,10}
token: \d{1,10}
## User's chat ## User's chat
chat_user: chat_user: