From cf4cec1e29e66d224c57d9e253403d657ae1b353 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Fri, 16 Aug 2013 16:05:24 +0200 Subject: [PATCH] Refactored sendMessage Let the message only be sent when an event has been triggered. This allows us to first (if necessary) let OTR process the image. --- converse.js | 67 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/converse.js b/converse.js index 641ff1b06..03a6324fa 100644 --- a/converse.js +++ b/converse.js @@ -227,17 +227,17 @@ priv: myKey, debug: true }; - var contact = new otr.OTR(options); - - contact.on('ui', function (msg) { + this.otr = new otr.OTR(options); + this.otr.on('ui', $.proxy(function (msg) { console.log("message to display to the user:"+msg); - }); - contact.on('io', function (msg) { + }, this)); + this.otr.on('io', $.proxy(function (msg) { console.log("message to display to the user:"+msg); - }); - contact.on('error', function (msg) { + this.trigger('sendMessage', msg); + }, this)); + this.otr.on('error', $.proxy(function (msg) { console.log("message to display to the user:"+msg); - }); + }, this)); }, messageReceived: function (message) { @@ -308,6 +308,20 @@ '' ), + initialize: function (){ + this.model.messages.on('add', this.showMessage, this); + this.model.on('show', this.show, this); + this.model.on('destroy', this.hide, this); + this.model.on('change', this.onChange, this); + this.model.on('sendMessage', this.onMessageSend, this); + this.updateVCard(); + this.$el.appendTo(converse.chatboxesview.$el); + this.render().show().model.messages.fetch({add: true}); + if (this.model.get('status')) { + this.showStatusMessage(this.model.get('status')); + } + }, + appendMessage: function ($el, msg_dict) { var this_date = converse.parseISO8601(msg_dict.time), text = msg_dict.message, @@ -393,14 +407,7 @@ }, sendMessage: function (text) { - // TODO: Look in ChatPartners to see what resources we have for the recipient. - // if we have one resource, we sent to only that resources, if we have multiple - // we send to the bare jid. - var timestamp = (new Date()).getTime(), - bare_jid = this.model.get('jid'), - match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/), - msgs; - + var match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/), msgs; if (match) { if (match[1] === "clear") { this.$el.find('.chat-content').empty(); @@ -432,6 +439,20 @@ return; } } + if (this.model.otr) { + this.model.otr.sendMsg(text); + } + else { + this.model.trigger('sendMessage', text); + } + }, + + onMessageSend: function (text) { + // TODO: Look in ChatPartners to see what resources we have for the recipient. + // if we have one resource, we sent to only that resources, if we have multiple + // we send to the bare jid. + var timestamp = (new Date()).getTime(); + var bare_jid = this.model.get('jid'); var message = $msg({from: converse.connection.jid, to: bare_jid, type: 'chat', id: timestamp}) .c('body').t(text).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}); @@ -441,6 +462,7 @@ .c('forwarded', {xmlns:'urn:xmpp:forward:0'}) .c('delay', {xmns:'urn:xmpp:delay',stamp:timestamp}).up() .cnode(message.tree()); + converse.connection.send(message); converse.connection.send(forwarded); // Add the new message @@ -540,19 +562,6 @@ } }, - initialize: function (){ - this.model.messages.on('add', this.showMessage, this); - this.model.on('show', this.show, this); - this.model.on('destroy', this.hide, this); - this.model.on('change', this.onChange, this); - this.updateVCard(); - this.$el.appendTo(converse.chatboxesview.$el); - this.render().show().model.messages.fetch({add: true}); - if (this.model.get('status')) { - this.showStatusMessage(this.model.get('status')); - } - }, - template: _.template( '
' + 'X' +