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

This commit is contained in:
JC Brand 2016-03-01 21:57:27 +00:00
parent 8665c0ac1c
commit f7725943f8
3 changed files with 32 additions and 21 deletions

View File

@ -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) { ... });`` |

View File

@ -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 @@
'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();

View File

@ -1024,6 +1024,7 @@
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: * Parameters:
* (String) jid - The Jabber ID of the user being added * (String) jid - The Jabber ID of the user being added
* (String) name - The name of that user * (String) name - The name of that user
@ -1044,6 +1045,7 @@
* 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: * Parameters:
* (String) jid - The Jabber ID of the user being added and subscribed to. * (String) jid - The Jabber ID of the user being added and subscribed to.
* (String) name - The name of that user * (String) name - The name of that user
@ -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 @@
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 @@
} }
} 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)
); );
} }