diff --git a/api.py b/api.py index f4dc0ff..202d8db 100644 --- a/api.py +++ b/api.py @@ -94,11 +94,21 @@ class APIWrapper(webapp2.RequestHandler): @security.weak_security_checks @session.session_required def post(self): - ret = [] + ret = { + 'status': 'ok', + 'responses': [], + 'messages': [], + } for command in self.request_json['commands']: callback = self._COMMANDS[command['command']] - result = callback(self.verified_google_user, self.client, command.get('arguments', {})) - ret.append(result) + result = callback( + self.verified_google_user, + self.client, + command.get('arguments', {})) + # Magic: if result contains "messages", haul them up a level so the + # client can see them as a single stream. + ret['messages'].extend(result.pop('messages', [])) + ret['responses'].append(result) return ret diff --git a/lib/session.py b/lib/session.py index 75e5881..0d5449b 100644 --- a/lib/session.py +++ b/lib/session.py @@ -75,10 +75,7 @@ def session_required(handler): else: self.client = models.Client.FromGoogleUser(self.verified_google_user) - ret = { - 'status': 'ok', - 'responses': handler(self), - } + ret = handler(self) if client_key != self.client.key(): # Tell the client that this changed ret['client_id'] = auth.Sign(self.client.key()) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 26e3d84..cb9fcd5 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -136,6 +136,9 @@ cosmopolite.Client.prototype.sendRPCs_ = function(commands, delay) { this.$.proxy(commands[i]['onSuccess'], this)(data.responses[i]); } } + // Handle messages that were immediately available as if they came over the + // channel. + data['messages'].forEach(this.onServerMessage_, this); }) .fail(function(xhr) { var intDelay = @@ -181,9 +184,6 @@ cosmopolite.Client.prototype.onCreateChannel_ = function(data) { onmessage: this.$.proxy(this.onSocketMessage_, this), onerror: this.$.proxy(this.onSocketError_, this), }); - // Handle messages that were immediately available as if they came over the - // channel. - data['messages'].forEach(this.onServerMessage_, this); }; cosmopolite.Client.prototype.onSocketOpen_ = function() {