瀏覽代碼

Add the ability to reject presence subscriptions.

Still part of the refactoring of strophe.roster.
JC Brand 10 年之前
父節點
當前提交
1ea770c79a
共有 2 個文件被更改,包括 45 次插入35 次删除
  1. 37 24
      converse.js
  2. 8 11
      spec/controlbox.js

+ 37 - 24
converse.js

@@ -30,21 +30,7 @@
             }
         );
     } else {
-        root.converse = factory(
-            templates,
-            jQuery,
-            $iq,
-            $msg,
-            $pres,
-            $build,
-            DSA,
-            OTR,
-            Strophe,
-            _,
-            moment,
-            utils,
-            b64_sha1
-        );
+        root.converse = factory(templates, jQuery, $iq, $msg, $pres, $build, DSA, OTR, Strophe, _, moment, utils, b64_sha1);
     }
 }(this, function (templates, $, $iq, $msg, $pres, $build, DSA, OTR, Strophe, _, moment, utils, b64_sha1) {
     // "use strict";
@@ -397,8 +383,27 @@
                 }
             }
         };
+  
+        this.rejectPresenceSubscription = function (jid, message) {
+            /* Reject or cancel another user's subscription to our presence updates.
+             *  Parameters:
+             *    (String) jid - The Jabber ID of the user whose subscription
+             *      is being canceled.
+             *    (String) message - An optional message to the user 
+             */
+            var pres = $pres({to: jid, type: "unsubscribed"});
+            if (message && message !== "") { pres.c("status").t(message); }
+            converse.connection.send(pres);
+        };
 
         this.getVCard = function (jid, callback, errback) {
+            /* Request the VCard of another user.
+             *  Parameters:
+             *    (String) jid - The Jabber ID of the user whose VCard is being requested.
+             *    (Function) callback - A function to call once the VCard is returned
+             *    (Function) errback - A function to call if an error occured
+             *      while trying to fetch the VCard.
+             */
             if (!this.use_vcards) {
                 if (callback) {
                     callback(jid, jid);
@@ -3511,14 +3516,24 @@
             unauthorize: function (message) {
                 /* Unauthorize this contact's presence subscription
                  * Parameters:
-                 *   (String) message - An optional message to send to the person being unauthorized.
+                 *   (String) message - Optional message to send to the person being unauthorized
                  */
-                var pres = $pres({to: this.get('jid'), type: "unsubscribed"});
-                if (message && message !== "") { pres.c("status").t(message); }
-                converse.connection.send(pres);
+                converse.rejectPresenceSubscription(this.get('jid'), message);
                 return this;
             },
 
+            authorize: function (message) {
+                /* Authorize presence subscription
+                 * Parameters:
+                 *   (String) message - Optional message to send to the person being authorized
+                 */
+                var pres = $pres({to: jid, type: "subscribed"});
+                if (message && message !== "") {
+                    pres.c("status").t(message);
+                }
+                converse.connection.send(pres);
+            },
+
             removeFromRoster: function (callback) {
                 /* Instruct the XMPP server to remove this contact from our roster
                  * Parameters:
@@ -3657,7 +3672,7 @@
             acceptRequest: function (ev) {
                 if (ev && ev.preventDefault) { ev.preventDefault(); }
                 var jid = this.model.get('jid');
-                converse.connection.roster.authorize(jid);
+                this.model.authorize();
                 converse.connection.roster.add(jid, this.model.get('fullname'), [], function (iq) {
                     converse.roster.subscribe(jid, null, converse.xmppstatus.get('fullname'));
                 });
@@ -3667,8 +3682,7 @@
                 if (ev && ev.preventDefault) { ev.preventDefault(); }
                 var result = confirm(__("Are you sure you want to decline this contact request?"));
                 if (result === true) {
-                    converse.connection.roster.unauthorize(this.model.get('jid'));
-                    this.model.destroy();
+                    this.model.unauthorize().destroy();
                 }
                 return this;
             }
@@ -3902,9 +3916,8 @@
             handleIncomingSubscription: function (jid) {
                 var bare_jid = Strophe.getBareJidFromJid(jid);
                 var item = this.get(bare_jid);
-
                 if (!converse.allow_contact_requests) {
-                    converse.connection.roster.unauthorize(bare_jid);
+                    converse.rejectPresenceSubscription(jid, __("This client does not allow presence subscriptions"));
                     return true;
                 }
                 if (converse.auto_subscribe) {

+ 8 - 11
spec/controlbox.js

@@ -889,35 +889,32 @@
                 // mock_connection.
                 var name = mock.req_names.sort()[0];
                 var jid =  name.replace(/ /g,'.').toLowerCase() + '@localhost';
-                spyOn(this.connection.roster, 'authorize');
-
+                var contact = this.roster.get(jid);
+                spyOn(contact, 'authorize');
                 converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
                     .siblings('.request-actions')
                     .find('.accept-xmpp-request').click();
-
-                expect(this.connection.roster.authorize).toHaveBeenCalled();
+                expect(contact.authorize).toHaveBeenCalled();
             }, converse));
 
             it("can have their requests denied by the user", $.proxy(function () {
                 this.rosterview.model.reset();
                 runs($.proxy(function () {
-                    spyOn(converse, 'emit');
-                    spyOn(this.connection.roster, 'unauthorize');
-                    spyOn(window, 'confirm').andReturn(true);
                     utils.createContacts('requesting').openControlBox();
                     converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
                 }, this));
                 waits(50);
                 runs($.proxy(function () {
                     var name = mock.req_names.sort()[1];
+                    var jid =  name.replace(/ /g,'.').toLowerCase() + '@localhost';
+                    var contact = this.roster.get(jid);
+                    spyOn(window, 'confirm').andReturn(true);
+                    spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
                     converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
                         .siblings('.request-actions')
                         .find('.decline-xmpp-request').click();
-                }, this));
-                waits(50);
-                runs($.proxy(function () {
                     expect(window.confirm).toHaveBeenCalled();
-                    expect(this.connection.roster.unauthorize).toHaveBeenCalled();
+                    expect(contact.unauthorize).toHaveBeenCalled();
                     // There should now be one less contact
                     expect(this.roster.length).toEqual(mock.req_names.length-1);
                 }, this));