Commit d0dc83ed authored by JC Brand's avatar JC Brand

XEP-0297: Forward sent messages to connected resources.

- This allows multiple open tabs (which is each its own resource) to be aware
when one tab sent a message.
parent 8c780642
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
evaluate : /\{\[([\s\S]+?)\]\}/g, evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g interpolate : /\{\{([\s\S]+?)\}\}/g
}; };
return factory({}, jQuery, store, _, console); return factory(jQuery, store, _, console);
} }
); );
} else { } else {
...@@ -66,11 +66,11 @@ ...@@ -66,11 +66,11 @@
evaluate : /\{\[([\s\S]+?)\]\}/g, evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g interpolate : /\{\{([\s\S]+?)\}\}/g
}; };
root.xmppchat = factory({}, jQuery, store, _, console || {log: function(){}}); root.xmppchat = factory(jQuery, store, _, console || {log: function(){}});
} }
}(this, function (jarnxmpp, $, store, _, console) { }(this, function ($, store, _, console) {
var xmppchat = jarnxmpp; var xmppchat = {};
xmppchat.collections = { xmppchat.collections = {
/* FIXME: XEP-0136 specifies 'urn:xmpp:archive' but the mod_archive_odbc /* FIXME: XEP-0136 specifies 'urn:xmpp:archive' but the mod_archive_odbc
...@@ -265,14 +265,14 @@ ...@@ -265,14 +265,14 @@
/* XXX: event.mtype should be 'xhtml' for XHTML-IM messages, /* XXX: event.mtype should be 'xhtml' for XHTML-IM messages,
but I only seem to get 'text'. but I only seem to get 'text'.
*/ */
var body = $(message).children('body').text(), var body = $message.children('body').text(),
jid = $(message).attr('from'), from = Strophe.getBareJidFromJid($message.attr('from')),
composing = $(message).find('composing'), to = $message.attr('to'),
composing = $message.find('composing'),
$chat_content = $(this.el).find('.chat-content'), $chat_content = $(this.el).find('.chat-content'),
user_id = Strophe.getNodeFromJid(jid), delayed = $message.find('delay').length > 0,
delayed = $(message).find('delay').length > 0,
fullname = this.model.get('fullname'), fullname = this.model.get('fullname'),
time, stamp; time, stamp, username, sender;
if (xmppchat.xmppstatus.getStatus() === 'offline') { if (xmppchat.xmppstatus.getStatus() === 'offline') {
// only update the UI if the user is not offline // only update the UI if the user is not offline
...@@ -284,22 +284,31 @@ ...@@ -284,22 +284,31 @@
return; return;
} }
} else { } else {
xmppchat.storage.addMessage(jid, body, 'from'); if (from == xmppchat.connection.bare_jid) {
$chat_content.find('div.chat-event').remove(); // I am the sender, so this must be a forwarded message...
$chat_content.find('div.chat-event').remove();
username = 'me';
sender = 'me';
} else {
xmppchat.storage.addMessage(from, body, 'from');
$chat_content.find('div.chat-event').remove();
username = fullname.split(' ')[0];
sender = 'them';
}
if (delayed) { if (delayed) {
// XXX: Test properly (for really old messages we somehow need to show // XXX: Test properly (for really old messages we somehow need to show
// their date as well) // their date as well)
stamp = $(message).find('delay').attr('stamp'); stamp = $message.find('delay').attr('stamp');
time = (new Date(stamp)).toLocaleTimeString().substring(0,5); time = (new Date(stamp)).toLocaleTimeString().substring(0,5);
} else { } else {
time = (new Date()).toLocaleTimeString().substring(0,5); time = (new Date()).toLocaleTimeString().substring(0,5);
} }
$chat_content.append( $chat_content.append(
this.message_template({ this.message_template({
'sender': 'them', 'sender': sender,
'time': time, 'time': time,
'message': body.replace(/<br \/>/g, ""), 'message': body,
'username': fullname.split(' ')[0], 'username': username,
'extra_classes': delayed && 'delayed' || '' 'extra_classes': delayed && 'delayed' || ''
})); }));
$chat_content.scrollTop($chat_content[0].scrollHeight); $chat_content.scrollTop($chat_content[0].scrollHeight);
...@@ -340,17 +349,24 @@ ...@@ -340,17 +349,24 @@
}, },
sendMessage: function (text) { sendMessage: function (text) {
// TODO: Also send message to all my own connected resources, so that
// they can display it as well....
// TODO: Look in ChatPartners to see what resources we have for the recipient. // 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 // if we have one resource, we sent to only that resources, if we have multiple
// we send to the bare jid. // we send to the bare jid.
var timestamp = new Date().getTime();
var bare_jid = this.model.get('jid'); var bare_jid = this.model.get('jid');
var message = $msg({to: bare_jid, type: 'chat'}) var message = $msg({from: xmppchat.connection.bare_jid, to: bare_jid, type: 'chat', id: timestamp})
.c('body').t(text).up() .c('body').t(text).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'});
// Forward the message, so that other connected resources are also aware of it.
// TODO: Forward the message only to other connected resources (inside the browser)
var forwarded = $msg({to:xmppchat.connection.bare_jid, type:'chat', id:timestamp})
.c('forwarded', {xmlns:'urn:xmpp:forward:0'})
.c('delay', {xmns:'urn:xmpp:delay',stamp:timestamp}).up()
.cnode(message.tree());
xmppchat.connection.send(message); xmppchat.connection.send(message);
xmppchat.connection.send(forwarded);
xmppchat.storage.addMessage(bare_jid, text, 'to'); xmppchat.storage.addMessage(bare_jid, text, 'to');
this.appendMessage(text); this.appendMessage(text);
}, },
...@@ -436,7 +452,6 @@ ...@@ -436,7 +452,6 @@
'placeholder="Personal message"/>'+ 'placeholder="Personal message"/>'+
'</form>'), '</form>'),
render: function () { render: function () {
$(this.el).attr('id', this.model.get('box_id')); $(this.el).attr('id', this.model.get('box_id'));
$(this.el).html(this.template(this.model.toJSON())); $(this.el).html(this.template(this.model.toJSON()));
...@@ -976,24 +991,43 @@ ...@@ -976,24 +991,43 @@
}, },
messageReceived: function (message) { messageReceived: function (message) {
var jid = $(message).attr('from'), if ($(message).attr('from') == xmppchat.connection.jid) {
bare_jid = Strophe.getBareJidFromJid(jid), // FIXME: Forwarded messages should be sent to specific resources, not broadcasted
resource = Strophe.getResourceFromJid(jid), return true;
view = this.views[bare_jid], }
fullname; var $forwarded = $(message).children('forwarded');
if ($forwarded.length > 0) {
$message = $forwarded.children('message');
} else {
$message = $(message);
}
var from = Strophe.getBareJidFromJid($message.attr('from')),
to = Strophe.getBareJidFromJid($message.attr('to')),
view, resource;
if (from == xmppchat.connection.bare_jid) {
// I am the sender, so this must be a forwarded message...
partner_jid = to;
resource = Strophe.getResourceFromJid($message.attr('to'));
} else {
partner_jid = from;
resource = Strophe.getResourceFromJid($message.attr('from'));
}
view = this.views[partner_jid];
if (!view) { if (!view) {
$.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(bare_jid), $.proxy(function (data) { $.getJSON(portal_url + "/xmpp-userinfo?user_id=" + Strophe.getNodeFromJid(partner_jid), $.proxy(function (data) {
view = this.createChatBox(jid, data); view = this.createChatBox(jid, data);
view.messageReceived(message); view.messageReceived(message);
xmppchat.roster.addResource(bare_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
}, this)); }, this));
return; return;
} else if (!view.isVisible()) { } else if (!view.isVisible()) {
this.showChat(bare_jid); this.showChat(partner_jid);
} }
view.messageReceived(message); view.messageReceived(message);
xmppchat.roster.addResource(bare_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
}, },
initialize: function () { initialize: function () {
...@@ -1006,7 +1040,7 @@ ...@@ -1006,7 +1040,7 @@
} }
}, this); }, this);
this.views = {}; this.views = {};
// Add the controlbox view and the panels. // Add the controlbox view and the panels
this.views.controlbox = xmppchat.controlbox; this.views.controlbox = xmppchat.controlbox;
this.views.controlbox.$el.appendTo(this.$el); this.views.controlbox.$el.appendTo(this.$el);
this.views.controlbox.contactspanel = new xmppchat.ContactsPanel().render(); this.views.controlbox.contactspanel = new xmppchat.ContactsPanel().render();
...@@ -1624,12 +1658,13 @@ ...@@ -1624,12 +1658,13 @@
model: new xmppchat.ControlBox({'id':'controlbox', 'jid':'controlbox'}) model: new xmppchat.ControlBox({'id':'controlbox', 'jid':'controlbox'})
}).render(); }).render();
$(document).bind('jarnxmpp.connected', $.proxy(function (ev, conn) { $(document).bind('xmpp.disconnected', $.proxy(function (ev, conn) {
alert("Connection Failed :("); console.log("Connection Failed :(");
}, this)); }, this));
$(document).unbind('jarnxmpp.connected'); $(document).unbind('xmpp.connected');
$(document).bind('jarnxmpp.connected', $.proxy(function () { $(document).bind('xmpp.connected', $.proxy(function (ev, connection) {
this.connection = connection
this.connection.xmlInput = function (body) { console.log(body); }; this.connection.xmlInput = function (body) { console.log(body); };
this.connection.xmlOutput = function (body) { console.log(body); }; this.connection.xmlOutput = function (body) { console.log(body); };
this.connection.bare_jid = Strophe.getBareJidFromJid(this.connection.jid); this.connection.bare_jid = Strophe.getBareJidFromJid(this.connection.jid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment