From 67e901d3d0dea778e054dc8145f64f469909be70 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 6 Feb 2013 15:22:17 +0000 Subject: [PATCH] Call updateMessages function not from server but from chat window --- src/messenger/webim/js/compiled/chat/app.js | 11 +-- .../js/compiled/chat/collections/messages.js | 5 +- src/messenger/webim/js/compiled/chat_app.js | 16 ++--- src/messenger/webim/js/source/chat/app.js | 52 +++++++++++++- .../js/source/chat/collections/messages.js | 50 +++---------- .../webim/libs/classes/thread_processor.php | 72 +++++++++---------- 6 files changed, 111 insertions(+), 95 deletions(-) diff --git a/src/messenger/webim/js/compiled/chat/app.js b/src/messenger/webim/js/compiled/chat/app.js index 8fbf96cd..44d2d646 100644 --- a/src/messenger/webim/js/compiled/chat/app.js +++ b/src/messenger/webim/js/compiled/chat/app.js @@ -5,8 +5,9 @@ Copyright (c) 2005-2011 Mibew Messenger Community License: http://mibew.org/license.php */ -(function(a,j,k){var f=new j.Marionette.Application;f.addRegions({controlsRegion:"#controls-region",avatarRegion:"#avatar-region",messagesRegion:a.Regions.Messages,statusRegion:"#status-region",messageFormRegion:"#message-form-region",soundRegion:"#sound-region"});f.addInitializer(function(d){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status;g.server=new a.Server(k.extend({interactionType:MibewAPIChatInteraction},d.server));c.thread=new a.Models.Thread(d.thread); -c.user=new a.Models.ChatUser(d.user);c.page=new a.Models.Page(d.page);var e=new a.Collections.Controls;c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:d.links.mailLink}),e.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:d.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:d.links.historyLink}),e.add(b.history)); -b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140});e.add(b.refresh);d.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:d.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3}); -g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status}));c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(d.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages})); -c.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.runUpdater()});a.Application=f})(Mibew,Backbone,_); +(function(a,j,k){var d=new j.Marionette.Application;d.addRegions({controlsRegion:"#controls-region",avatarRegion:"#avatar-region",messagesRegion:a.Regions.Messages,statusRegion:"#status-region",messageFormRegion:"#message-form-region",soundRegion:"#sound-region"});d.addInitializer(function(e){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status;g.server=new a.Server(k.extend({interactionType:MibewAPIChatInteraction},e.server));c.thread=new a.Models.Thread(e.thread); +c.user=new a.Models.ChatUser(e.user);c.page=new a.Models.Page(e.page);var f=new a.Collections.Controls;c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),f.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:e.links.mailLink}),f.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:e.links.redirectLink}),f.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:e.links.historyLink}),f.add(b.history)); +b.sound=new a.Models.SoundControl({weight:160});f.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140});f.add(b.refresh);e.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:e.links.sslLink}),f.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});f.add(b.close);g.Collections.controls=f;d.controlsRegion.show(new a.Views.ControlsCollection({collection:f}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3}); +g.Collections.status=new a.Collections.Status([h.message,h.typing]);d.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status}));c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,d.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(e.messageForm);d.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));d.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages})); +c.sound=new a.Models.Sound;d.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.callFunctionsPeriodically(function(){var b=a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&& +a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))})});d.on("start",function(){a.Objects.server.runUpdater()});a.Application=d})(Mibew,Backbone,_); diff --git a/src/messenger/webim/js/compiled/chat/collections/messages.js b/src/messenger/webim/js/compiled/chat/collections/messages.js index 6dcbffd3..c9c68b26 100644 --- a/src/messenger/webim/js/compiled/chat/collections/messages.js +++ b/src/messenger/webim/js/compiled/chat/collections/messages.js @@ -5,6 +5,5 @@ Copyright (c) 2005-2011 Mibew Messenger Community License: http://mibew.org/license.php */ -(function(b,d,c){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){b.Objects.server.callFunctionsPeriodically(c.bind(this.updateFunctionBuilder,this),c.bind(this.updateChatState,this));b.Objects.server.registerFunction("updateMessages",c.bind(this.apiUpdateMessages,this))},apiUpdateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});for(var e=[],c=0,d=a.messages.length;cgetMessages($is_user, $last_message_id); - if (! empty($messages)) { - // Send messages - $this->responses[] = array( - 'token' => md5(time() . rand()), - 'functions' => array( - array( - 'function' => 'updateMessages', - 'arguments' => array( - 'threadId' => $thread->id, - 'token' => $thread->lastToken, - 'return' => array(), - 'references' => array(), - 'messages' => $messages, - 'lastId' => $last_message_id - ) - ) - ) - ); - } - } - /** * Update chat window state. API function * @@ -255,7 +222,7 @@ class ThreadProcessor extends ClientSideProcessor { $thread = self::getThread($args['threadId'], $args['token']); // Check variables - self::checkParams($args, array('user', 'typed', 'lastId')); + self::checkParams($args, array('user', 'typed')); if (! $args['user']) { $operator = self::checkOperator(); @@ -264,9 +231,6 @@ class ThreadProcessor extends ClientSideProcessor { $thread->ping($args['user'], $args['typed']); - // Update messages - $this->sendMessages($thread, $args['user'], $args['lastId']); - // Create requests key $requests_key = false; if ($args['user']) { @@ -299,6 +263,40 @@ class ThreadProcessor extends ClientSideProcessor { ); } + /** + * Send new messages to window. API function + * + * @param array $args Associative array of arguments. It must contains following keys: + * - 'threadId': Id of the thread related to chat window + * - 'token': last thread token + * - 'user': TRUE if window used by user and FALSE otherwise + * - 'lastId': last sent message id + */ + protected function apiUpdateMessages($args) { + // Load thread + $thread = self::getThread($args['threadId'], $args['token']); + + // Check variables + self::checkParams($args, array('user', 'lastId')); + + // Check access + if (! $args['user']) { + self::checkOperator(); + } + + // Send new messages + $last_message_id = $args['lastId']; + $messages = $thread->getMessages($args['user'], $last_message_id); + if (empty($messages)) { + $messages = array(); + } + + return array( + 'messages' => $messages, + 'lastId' => $last_message_id + ); + } + /** * Post message to thread. API function *