diff --git a/converse.js b/converse.js index 431ee0cb4..2e08ba9ef 100644 --- a/converse.js +++ b/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) { diff --git a/spec/controlbox.js b/spec/controlbox.js index 6706864d5..55c8346ce 100644 --- a/spec/controlbox.js +++ b/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));