소스 검색

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

JC Brand 9 년 전
부모
커밋
f7725943f8
3개의 변경된 파일32개의 추가작업 그리고 21개의 파일을 삭제
  1. 2 0
      docs/source/development.rst
  2. 2 0
      spec/protocol.js
  3. 28 21
      src/converse-core.js

+ 2 - 0
docs/source/development.rst

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

+ 2 - 0
spec/protocol.js

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

+ 28 - 21
src/converse-core.js

@@ -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
-                *    (String) name - The name of that user
-                *    (Array of Strings) groups - Any roster groups the user might belong to
-                *    (Function) callback - A function to call once the VCard is returned
-                *    (Function) errback - A function to call if an error occured
-                */
+                 *
+                 *  Parameters:
+                 *    (String) jid - The Jabber ID of the user being added
+                 *    (String) name - The name of that user
+                 *    (Array of Strings) groups - Any roster groups the user might belong to
+                 *    (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 @@
 
 
             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.
-                * Returns a promise which is resolved once the XMPP server has
-                * responded.
-                *  Parameters:
-                *    (String) jid - The Jabber ID of the user being added and subscribed to.
-                *    (String) name - The name of that user
-                *    (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.
-                */
+                 * registers the contact on the XMPP server.
+                 * Returns a promise which is resolved once the XMPP server has
+                 * responded.
+                 *
+                 *  Parameters:
+                 *    (String) jid - The Jabber ID of the user being added and subscribed to.
+                 *    (String) name - The name of that user
+                 *    (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 @@
                 }
                 }
             },
             },
 
 
-            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)
                         );
                         );
                     }
                     }