Commit f7725943 authored by JC Brand's avatar JC Brand

Emit an event 'contactRequest' when a subscription request is received

parent 8665c0ac
...@@ -751,6 +751,8 @@ Here are the different events that are emitted: ...@@ -751,6 +751,8 @@ Here are the different events that are emitted:
+---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| **chatBoxToggled** | When a chat box has been minimized or maximized. | ``converse.listen.on('chatBoxToggled', function (event, chatbox) { ... });`` | | **chatBoxToggled** | When a chat box has been minimized or maximized. | ``converse.listen.on('chatBoxToggled', function (event, chatbox) { ... });`` |
+---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| **contactRequest** | Someone has requested to subscribe to your presence (i.e. to be your contact). | ``converse.listen.on('contactRequest', function (event, user_data) { ... });`` |
+---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy, status) { ... });`` | | **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy, status) { ... });`` |
+---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, buddy, messageText) { ... });`` | | **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, buddy, messageText) { ... });`` |
......
...@@ -501,6 +501,7 @@ ...@@ -501,6 +501,7 @@
}); });
waits(50); waits(50);
runs(function () { runs(function () {
spyOn(converse, "emit");
/* /*
* <presence * <presence
* from='user@example.com' * from='user@example.com'
...@@ -513,6 +514,7 @@ ...@@ -513,6 +514,7 @@
'type': 'subscribe' 'type': 'subscribe'
}); });
this.connection._dataRecv(test_utils.createRequest(stanza)); this.connection._dataRecv(test_utils.createRequest(stanza));
expect(converse.emit).toHaveBeenCalledWith('contactRequest', jasmine.any(Object));
var $header = $('a:contains("Contact requests")'); var $header = $('a:contains("Contact requests")');
expect($header.length).toBe(1); expect($header.length).toBe(1);
expect($header.is(":visible")).toBeTruthy(); expect($header.is(":visible")).toBeTruthy();
......
...@@ -1024,13 +1024,14 @@ ...@@ -1024,13 +1024,14 @@
sendContactAddIQ: function (jid, name, groups, callback, errback) { sendContactAddIQ: function (jid, name, groups, callback, errback) {
/* Send an IQ stanza to the XMPP server to add a new roster contact. /* Send an IQ stanza to the XMPP server to add a new roster contact.
* Parameters: *
* (String) jid - The Jabber ID of the user being added * Parameters:
* (String) name - The name of that user * (String) jid - The Jabber ID of the user being added
* (Array of Strings) groups - Any roster groups the user might belong to * (String) name - The name of that user
* (Function) callback - A function to call once the VCard is returned * (Array of Strings) groups - Any roster groups the user might belong to
* (Function) errback - A function to call if an error occured * (Function) callback - A function to call once the VCard is returned
*/ * (Function) errback - A function to call if an error occured
*/
name = _.isEmpty(name)? jid: name; name = _.isEmpty(name)? jid: name;
var iq = $iq({type: 'set'}) var iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER}) .c('query', {xmlns: Strophe.NS.ROSTER})
...@@ -1041,15 +1042,16 @@ ...@@ -1041,15 +1042,16 @@
addContact: function (jid, name, groups, attributes) { addContact: function (jid, name, groups, attributes) {
/* Adds a RosterContact instance to converse.roster and /* Adds a RosterContact instance to converse.roster and
* registers the contact on the XMPP server. * registers the contact on the XMPP server.
* Returns a promise which is resolved once the XMPP server has * Returns a promise which is resolved once the XMPP server has
* responded. * responded.
* Parameters: *
* (String) jid - The Jabber ID of the user being added and subscribed to. * Parameters:
* (String) name - The name of that user * (String) jid - The Jabber ID of the user being added and subscribed to.
* (Array of Strings) groups - Any roster groups the user might belong to * (String) name - The name of that user
* (Object) attributes - Any additional attributes to be stored on the user's model. * (Array of Strings) groups - Any roster groups the user might belong to
*/ * (Object) attributes - Any additional attributes to be stored on the user's model.
*/
var deferred = new $.Deferred(); var deferred = new $.Deferred();
groups = groups || []; groups = groups || [];
name = _.isEmpty(name)? jid: name; name = _.isEmpty(name)? jid: name;
...@@ -1212,9 +1214,12 @@ ...@@ -1212,9 +1214,12 @@
} }
}, },
createContactFromVCard: function (iq, jid, fullname, img, img_type, url) { createRequestingContactFromVCard: function (iq, jid, fullname, img, img_type, url) {
/* A contact request was recieved, and we then asked for the
* VCard of that user.
*/
var bare_jid = Strophe.getBareJidFromJid(jid); var bare_jid = Strophe.getBareJidFromJid(jid);
this.create({ var user_data = {
jid: bare_jid, jid: bare_jid,
subscription: 'none', subscription: 'none',
ask: null, ask: null,
...@@ -1224,7 +1229,9 @@ ...@@ -1224,7 +1229,9 @@
image_type: img_type, image_type: img_type,
url: url, url: url,
vcard_updated: moment().format() vcard_updated: moment().format()
}); };
this.create(user_data);
converse.emit('contactRequest', user_data);
}, },
handleIncomingSubscription: function (jid) { handleIncomingSubscription: function (jid) {
...@@ -1248,10 +1255,10 @@ ...@@ -1248,10 +1255,10 @@
} }
} else if (!contact) { } else if (!contact) {
converse.getVCard( converse.getVCard(
bare_jid, this.createContactFromVCard.bind(this), bare_jid, this.createRequestingContactFromVCard.bind(this),
function (iq, jid) { function (iq, jid) {
converse.log("Error while retrieving vcard for "+jid); converse.log("Error while retrieving vcard for "+jid);
this.createContactFromVCard.call(this, iq, jid); this.createRequestingContactFromVCard.call(this, iq, jid);
}.bind(this) }.bind(this)
); );
} }
......
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