diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index bf007ffb..b6197866 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -57,9 +57,9 @@ $dbtables = array( "groupid" => "int references ${mysqlprefix}chatgroup(groupid)", ), - "${mysqlprefix}chatrequestbuffer" => array( + "${mysqlprefix}requestbuffer" => array( "requestid" => "int NOT NULL auto_increment PRIMARY KEY", - "threadid" => "int NOT NULL references ${mysqlprefix}chatthread(threadid)", + "requestkey" => "varchar(128) NOT NULL", "request" => "text NOT NULL" ), @@ -167,8 +167,8 @@ $dbtables_indexes = array( "groupid" => "groupid", "operatorid" => "operatorid" ), - "${mysqlprefix}chatrequestbuffer" => array( - "threadid" => "threadid" + "${mysqlprefix}requestbuffer" => array( + "requestkey" => "requestkey" ), "${mysqlprefix}chatmessage" => array( "idx_agentid" => "agentid" @@ -194,7 +194,7 @@ $memtables = array(); $dbtables_can_update = array( "${mysqlprefix}chatthread" => array("agentId", "userTyping", "agentTyping", "messageCount", "nextagent", "shownmessageid", "userid", "userAgent", "groupid", "dtmchatstarted"), - "${mysqlprefix}chatrequestbuffer" => array("requestid", "threadid", "request"), + "${mysqlprefix}requestbuffer" => array("requestid", "requestkey", "request"), "${mysqlprefix}chatmessage" => array("agentId"), "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"), "${mysqlprefix}chatban" => array(), diff --git a/src/messenger/webim/libs/classes/client_side_processor.php b/src/messenger/webim/libs/classes/client_side_processor.php new file mode 100644 index 00000000..b227a7bf --- /dev/null +++ b/src/messenger/webim/libs/classes/client_side_processor.php @@ -0,0 +1,96 @@ +mibewAPI->encodePackage( + $responses, + $this->config['signature'], + true + )); + } + + /** + * Add request to client side to the buffer. Use database as storage. + * Override this method if you want to use another storage and/or save logic. + * + * @param String $key Request key. Use to load request from buffer. + * @param $request Request array. + */ + protected function addRequestToBuffer($key, $request) { + // Save request to database + $db = Database::getInstance(); + $db->query( + "INSERT INTO {requestbuffer} (request, requestkey) VALUES (:request, :key)", + array(':request' => serialize($request), ':key' => $key) + ); + } + + /** + * Load stored requests to the client side + * + * @param String $key Request key + * @return array Array of requests with given key + */ + protected function getRequestsFromBuffer($key) { + $db = Database::getInstance(); + // Get requests from database + $requests = $db->query( + "SELECT request FROM {requestbuffer} WHERE requestkey = :key", + array(':key' => $key), + array('return_rows' => Database::RETURN_ALL_ROWS) + ); + // Remove got requests from database + $db->query( + "DELETE FROM {requestbuffer} WHERE requestkey = :key", + array(':key' => $key) + ); + // Unserialize requests + $result = array(); + foreach($requests as $request_info) { + $result[] = unserialize($request_info['request']); + } + return $result; + } + +} + +?> \ No newline at end of file diff --git a/src/messenger/webim/libs/classes/thread_processor.php b/src/messenger/webim/libs/classes/thread_processor.php index defc683e..b26db040 100644 --- a/src/messenger/webim/libs/classes/thread_processor.php +++ b/src/messenger/webim/libs/classes/thread_processor.php @@ -29,7 +29,7 @@ * * Implements Singleton pattern */ -class ThreadProcessor extends RequestProcessor { +class ThreadProcessor extends ClientSideProcessor { /** * An instance of the ThreadProcessor class @@ -61,7 +61,10 @@ class ThreadProcessor extends RequestProcessor { $thread = Thread::load($thread_id, $last_token); // Check thread if (! $thread) { - throw new ThreadProcessorException('Wrong thread', ThreadProcessorException::ERROR_WRONG_THREAD); + throw new ThreadProcessorException( + 'Wrong thread', + ThreadProcessorException::ERROR_WRONG_THREAD + ); } // Return thread return $thread; @@ -102,17 +105,6 @@ class ThreadProcessor extends RequestProcessor { )); } - /** - * Call function at window side - * - * @param array $functions Array of functions to call. See Mibew API for details. - * @param array|null $callback callback array for synchronous requests. - * @return mixed request result or boolean false on failure. - */ - public function call($functions, $callback = null) { - return parent::call($functions, true, $callback); - } - /** * Creates and returns an instance of the MibewAPI class. * @@ -122,20 +114,6 @@ class ThreadProcessor extends RequestProcessor { return MibewAPI::getAPI('MibewAPIChatInteraction'); } - /** - * Sends asynchronous responses - * - * @param array $responses An array of the 'Request' arrays. See Mibew API for details - */ - protected function sendAsyncResponses($responses) { - header("Content-type: text/plain; charset=UTF-8"); - echo($this->mibewAPI->encodePackage( - $responses, - $this->config['signature'], - true - )); - } - /** * Sends asynchronous request * @@ -161,42 +139,11 @@ class ThreadProcessor extends RequestProcessor { ); } } - // Save request to database - $db = Database::getInstance(); - $db->query( - "INSERT INTO {chatrequestbuffer} (request, threadid) VALUES (:request, :threadid)", - array(':request' => serialize($request), ':threadid' => $thread_id) - ); + // Store request in buffer + $this->addRequestToBuffer('thread_'.$thread_id, $request); return true; } - /** - * Load stored requests to window from database - * - * @param Thread $thread Requests loads for this thread - * @return array Array of requests to $thread thread - */ - protected function getStoredRequests(Thread $thread) { - $db = Database::getInstance(); - // Get requests from database - $requests = $db->query( - "SELECT request FROM {chatrequestbuffer} WHERE threadid = :threadid", - array(':threadid' => $thread->id), - array('return_rows' => Database::RETURN_ALL_ROWS) - ); - // Remove got requests from database - $db->query( - "DELETE FROM {chatrequestbuffer} WHERE threadid = :threadid", - array(':threadid' => $thread->id) - ); - // Unserialize requests - $result = array(); - foreach($requests as $request_info) { - $result[] = unserialize($request_info['request']); - } - return $result; - } - /** * Send new messages to window * @@ -285,7 +232,7 @@ class ThreadProcessor extends RequestProcessor { $this->sendMessages($thread, $args['user'], $args['lastId']); // Load stored requests - $stored_requests = $this->getStoredRequests($thread); + $stored_requests = $this->getRequestsFromBuffer('thread_'.$thread->id); if ($stored_requests !== false) { $this->responses = array_merge($this->responses, $stored_requests); } diff --git a/src/messenger/webim/thread.php b/src/messenger/webim/thread.php index eb583ac1..ad818796 100644 --- a/src/messenger/webim/thread.php +++ b/src/messenger/webim/thread.php @@ -23,6 +23,7 @@ require_once('libs/classes/mibew_api.php'); require_once('libs/classes/mibew_api_interaction.php'); require_once('libs/classes/mibew_api_chat_interaction.php'); require_once('libs/classes/mibew_api_execution_context.php'); +require_once('libs/classes/client_side_processor.php'); require_once('libs/classes/thread_processor.php'); $processor = ThreadProcessor::getInstance();