Beautify user chat URLs

This commit is contained in:
Dmitriy Simushev 2014-06-03 09:43:44 +00:00
parent 44ca9e2a2e
commit 6c9734648a
4 changed files with 90 additions and 67 deletions

View File

@ -523,8 +523,6 @@ function setup_chatview_for_user(Thread $thread)
'isAgent' => false,
);
$params = "thread=" . $thread->id . "&token=" . $thread->lastToken;
// Set link to send mail page
$data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat"
. '/' . $thread->id . '/' . $thread->lastToken . '/mail';
@ -532,8 +530,7 @@ function setup_chatview_for_user(Thread $thread)
// Set SSL link
if (Settings::get('enablessl') == "1" && !is_secure_request()) {
$data['chat']['links']['ssl'] = get_app_location(true, true)
. "/chat?"
. $params;
. '/chat/' . $thread->id . '/' . $thread->lastToken;
}
return $data;

View File

@ -18,7 +18,7 @@
namespace Mibew\Controller\Chat\User;
use Mibew\Controller\Chat\AbstractController;
use Mibew\Http\Exception\BadRequestException;
use Mibew\Http\Exception\NotFoundException;
use Mibew\Settings;
use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request;
@ -32,12 +32,38 @@ class ChatController extends AbstractController
* Process chat pages.
*
* @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered
* page content or a redirect response.
* @throws BadRequestException If the thread cannot be loaded by some
* reasons.
* @return string Rendered page content or a redirect response.
* @throws NotFoundException If the thread with specified ID and token is
* not found.
*/
public function indexAction(Request $request)
{
$thread_id = $request->attributes->getInt('thread_id');
$token = $request->attributes->get('token');
$thread = Thread::load($thread_id, $token);
if (!$thread) {
throw new NotFoundException('The thread is not found.');
}
$page = setup_chatview_for_user($thread);
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
// Expand page
return $this->render('chat', $page);
}
/**
* Starts the chat.
*
* @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse
* Rendered page content or a redirect response.
* @todo Split the action into pieces.
*/
public function startAction(Request $request)
{
// Check if we should force the user to use SSL
$ssl_redirect = $this->sslRedirect($request);
@ -55,66 +81,13 @@ class ChatController extends AbstractController
return $this->render('nochat', $page);
}
$action = $request->query->get('act');
if ($action != 'invitation') {
$action = 'default';
}
if ($action == 'invitation' && Settings::get('enabletracking')) {
// Check if user invited to chat
$invitation_state = invitation_state($_SESSION['visitorid']);
if ($invitation_state['invited'] && $invitation_state['threadid']) {
$thread = Thread::load($invitation_state['threadid']);
// Prepare page
$page = setup_invitation_view($thread);
// Build js application options
$page['invitationOptions'] = json_encode($page['invitation']);
// Expand page
return $this->render('chat', $page);
}
}
if (!$request->query->has('token') || !$request->query->has('thread')) {
return $this->startChat($request);
}
// Get and validate thread id
$thread_id = $request->query->get('thread');
if (!preg_match("/^\d{1,10}$/", $thread_id)) {
throw new BadRequestException('Wrong value of "thread" argument.');
}
// Get token and verify it
$token = $request->query->get('token');
if (!preg_match("/^\d{1,10}$/", $token)) {
throw new BadRequestException('Wrong value of "token" argument.');
}
$thread = Thread::load($thread_id, $token);
if (!$thread) {
throw new BadRequestException('Wrong thread.');
}
$page = setup_chatview_for_user($thread);
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
// Expand page
return $this->render('chat', $page);
}
protected function startChat(Request $request)
{
$thread = null;
// Try to get thread from the session
if (isset($_SESSION['threadid'])) {
$thread = Thread::reopen($_SESSION['threadid']);
}
// Create new thread
if (!$thread) {
// Load group info
$group_id = '';
@ -214,7 +187,7 @@ class ChatController extends AbstractController
);
}
$path_args = array(
'thread' => intval($thread->id),
'thread_id' => intval($thread->id),
'token' => urlencode($thread->lastToken),
);
@ -225,4 +198,44 @@ class ChatController extends AbstractController
return $this->redirect($this->generateUrl('chat_user', $path_args));
}
/**
* Process chat in an invitation block.
*
* @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered
* page content or a redirect response.
*/
public function invitationAction(Request $request)
{
// Check if an user tries to use invitation functionality when it's
// disabled.
if (!Settings::get('enabletracking')) {
return $this->redirect($this->generateUrl('chat_user_start'));
}
// Check if we should force the user to use SSL.
$ssl_redirect = $this->sslRedirect($request);
if ($ssl_redirect !== false) {
return $ssl_redirect;
}
// Check if user invited to chat.
$invitation_state = invitation_state($_SESSION['visitorid']);
if (!$invitation_state['invited'] || !$invitation_state['threadid']) {
return $this->redirect($this->generateUrl('chat_user_start'));
}
$thread = Thread::load($invitation_state['threadid']);
// Prepare page
$page = setup_invitation_view($thread);
// Build js application options
$page['invitationOptions'] = json_encode($page['invitation']);
// Expand page
return $this->render('chat', $page);
}
}

View File

@ -118,7 +118,7 @@ class WidgetController extends AbstractController
$response_data['data']['invitation'] = array(
'operatorName' => htmlspecialchars($operator_name),
'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'threadUrl' => $this->generateUrl('chat_user', array('act' => 'invitation')),
'threadUrl' => $this->generateUrl('chat_user_invitation'),
'acceptCaption' => getlocal('invitation.accept.caption'),
);

View File

@ -38,9 +38,17 @@ chat_operator_redirection_links:
## User's chat
chat_user:
path: /chat
path: /chat/{thread_id}/{token}
defaults:
_controller: Mibew\Controller\Chat\User\ChatController::indexAction
requirements:
thread_id: \d{1,10}
token: \d{1,10}
chat_user_invitation:
path: /chat/invitation
defaults:
_controller: Mibew\Controller\Chat\User\ChatController::invitationAction
chat_user_mail:
path: /chat/{thread_id}/{token}/mail
@ -60,6 +68,11 @@ chat_user_mail_send:
token: \d{1,10}
methods: [POST]
chat_user_start:
path: /chat
defaults:
_controller: Mibew\Controller\Chat\User\ChatController::startAction
# Pages that are available for all users
button:
path: /b