Add config option `allow_contact_requests` to toggle user adding

This commit is contained in:
JC Brand 2013-10-03 14:22:33 +02:00
parent 6d026f010e
commit 7f371b883e
4 changed files with 90 additions and 40 deletions

View File

@ -8,7 +8,8 @@ Changelog
* Hungarian translation [w3host] * Hungarian translation [w3host]
* Russian translation [bkocherov] * Russian translation [bkocherov]
* Update CSS to avoid clash with bootstrap [seocam] * Update CSS to avoid clash with bootstrap [seocam]
* Add config option ``allow_muc`` to enable/disable multi-user chat (MUC) [jcbrand] * Add config option ``allow_muc`` to toggle multi-user chat (MUC) [jcbrand]
* Add config option ``allow_contact_requests`` to toggle user adding [jcbrand]
0.6.4 (2013-09-15) 0.6.4 (2013-09-15)
------------------ ------------------

View File

@ -41,8 +41,11 @@
}(this, function ($, _, console) { }(this, function ($, _, console) {
var converse = {}; var converse = {};
converse.initialize = function (settings, callback) { converse.initialize = function (settings, callback) {
// Default values
var converse = this; var converse = this;
// Default values
this.allow_contact_requests = true;
this.allow_muc = true;
this.allow_otr = true;
this.animate = true; this.animate = true;
this.auto_list_rooms = false; this.auto_list_rooms = false;
this.auto_subscribe = false; this.auto_subscribe = false;
@ -50,17 +53,19 @@
this.debug = false; this.debug = false;
this.hide_muc_server = false; this.hide_muc_server = false;
this.i18n = locales.en; this.i18n = locales.en;
this.allow_muc = true;
this.prebind = false; this.prebind = false;
this.show_controlbox_by_default = false; this.show_controlbox_by_default = false;
this.xhr_user_search = false;
this.xhr_custom_status = false;
this.testing = false; // Exposes sensitive data for testing. Never set to true in production systems! this.testing = false; // Exposes sensitive data for testing. Never set to true in production systems!
this.xhr_custom_status = false;
this.xhr_user_search = false;
this.callback = callback || function () {}; this.callback = callback || function () {};
// Allow only the whitelisted settings attributes to be overwritten, // Allow only the whitelisted settings attributes to be overwritten,
// nothing else. // nothing else.
whitelist = [ whitelist = [
'allow_contact_requests',
'allow_muc',
'animate', 'animate',
'auto_list_rooms', 'auto_list_rooms',
'auto_subscribe', 'auto_subscribe',
@ -557,7 +562,7 @@
updateVCard: function () { updateVCard: function () {
var jid = this.model.get('jid'), var jid = this.model.get('jid'),
rosteritem = converse.roster.get(jid); rosteritem = converse.roster.get(jid);
if ((rosteritem)&&(!rosteritem.get('vcard_updated'))) { if ((rosteritem) && (!rosteritem.get('vcard_updated'))) {
converse.getVCard( converse.getVCard(
jid, jid,
$.proxy(function (jid, fullname, image, image_type, url) { $.proxy(function (jid, fullname, image, image_type, url) {
@ -565,8 +570,7 @@
'fullname' : fullname || jid, 'fullname' : fullname || jid,
'url': url, 'url': url,
'image_type': image_type, 'image_type': image_type,
'image': image, 'image': image
'vcard_updated': converse.toISOString(new Date())
}); });
}, this), }, this),
$.proxy(function (stanza) { $.proxy(function (stanza) {
@ -687,7 +691,10 @@
'<option value="offline">'+__('Offline')+'</option>'+ '<option value="offline">'+__('Offline')+'</option>'+
'</select>'+ '</select>'+
'</span>'+ '</span>'+
'</form>'+ '</form>'
),
add_contact_dropdown_template: _.template(
'<dl class="add-converse-contact dropdown">' + '<dl class="add-converse-contact dropdown">' +
'<dt id="xmpp-contact-search" class="fancy-dropdown">' + '<dt id="xmpp-contact-search" class="fancy-dropdown">' +
'<a class="toggle-xmpp-contact-form" href="#"'+ '<a class="toggle-xmpp-contact-form" href="#"'+
@ -698,7 +705,7 @@
'</dl>' '</dl>'
), ),
add_contact_template: _.template( add_contact_form_template: _.template(
'<li>'+ '<li>'+
'<form class="add-xmpp-contact">' + '<form class="add-xmpp-contact">' +
'<input type="text" name="identifier" class="username" placeholder="'+__('Contact username')+'"/>' + '<input type="text" name="identifier" class="username" placeholder="'+__('Contact username')+'"/>' +
@ -723,13 +730,20 @@
render: function () { render: function () {
var markup; var markup;
var widgets = this.template();
this.$tabs.append(this.tab_template()); this.$tabs.append(this.tab_template());
if (converse.xhr_user_search) { if (converse.xhr_user_search) {
markup = this.search_contact_template(); markup = this.search_contact_template();
} else { } else {
markup = this.add_contact_template(); markup = this.add_contact_form_template();
} }
this.$el.html(this.template());
if (converse.allow_contact_requests) {
widgets += this.add_contact_dropdown_template();
}
this.$el.html(widgets);
this.$el.find('.search-xmpp ul').append(markup); this.$el.find('.search-xmpp ul').append(markup);
this.$el.append(converse.rosterview.$el); this.$el.append(converse.rosterview.$el);
return this; return this;
@ -1719,6 +1733,11 @@
} }
chatbox = this.get(partner_jid); chatbox = this.get(partner_jid);
roster_item = converse.roster.get(partner_jid); roster_item = converse.roster.get(partner_jid);
if (!roster_item) {
// The buddy was likely removed
return true;
}
if (!chatbox) { if (!chatbox) {
chatbox = this.create({ chatbox = this.create({
'id': partner_jid, 'id': partner_jid,
@ -1893,14 +1912,14 @@
this.$el.addClass(item.get('chat_status')); this.$el.addClass(item.get('chat_status'));
if (ask === 'subscribe') { if ((ask === 'subscribe') && (converse.allow_contact_requests)) {
this.$el.addClass('pending-xmpp-contact'); this.$el.addClass('pending-xmpp-contact');
this.$el.html(this.pending_template(item.toJSON())); this.$el.html(this.pending_template(item.toJSON()));
} else if (ask === 'request') { } else if ((ask === 'request') && (converse.allow_contact_requests)) {
this.$el.addClass('requesting-xmpp-contact'); this.$el.addClass('requesting-xmpp-contact');
this.$el.html(this.request_template(item.toJSON())); this.$el.html(this.request_template(item.toJSON()));
converse.showControlBox(); converse.showControlBox();
} else if (subscription === 'both' || subscription === 'to') { } else if (subscription === 'both' || ((subscription === 'to') && converse.allow_contact_requests)) {
this.$el.addClass('current-xmpp-contact'); this.$el.addClass('current-xmpp-contact');
this.$el.html(this.template( this.$el.html(this.template(
_.extend(item.toJSON(), {'status_desc': statuses[item.get('chat_status')||'offline']}) _.extend(item.toJSON(), {'status_desc': statuses[item.get('chat_status')||'offline']})
@ -2155,6 +2174,10 @@
if ((presence_type === 'subscribed') || (presence_type === 'unsubscribe')) { if ((presence_type === 'subscribed') || (presence_type === 'unsubscribe')) {
return true; return true;
} else if (presence_type === 'subscribe') { } else if (presence_type === 'subscribe') {
if (!converse.allow_contact_requests) {
converse.connection.roster.unauthorize(bare_jid);
return true;
}
if (converse.auto_subscribe) { if (converse.auto_subscribe) {
if ((!item) || (item.get('subscription') != 'to')) { if ((!item) || (item.get('subscription') != 'to')) {
this.subscribeBack(jid); this.subscribeBack(jid);
@ -2165,25 +2188,32 @@
if ((item) && (item.get('subscription') != 'none')) { if ((item) && (item.get('subscription') != 'none')) {
converse.connection.roster.authorize(bare_jid); converse.connection.roster.authorize(bare_jid);
} else { } else {
converse.getVCard( if (!this.get(bare_jid)) {
bare_jid, // TODO: we can perhaps do the creation inside
$.proxy(function (jid, fullname, img, img_type, url) { // getVCard.
this.add({ converse.getVCard(
jid: bare_jid, bare_jid,
subscription: 'none', $.proxy(function (jid, fullname, img, img_type, url) {
ask: 'request', this.add({
fullname: fullname, jid: bare_jid,
image: img, subscription: 'none',
image_type: img_type, ask: 'request',
url: url, fullname: fullname,
is_last: true image: img,
}); image_type: img_type,
}, this), url: url,
$.proxy(function (jid, fullname, img, img_type, url) { vcard_updated: converse.toISOString(new Date()),
converse.log("Error while retrieving vcard"); is_last: true
this.add({jid: bare_jid, subscription: 'none', ask: 'request', fullname: jid, is_last: true}); });
}, this) }, this),
); $.proxy(function (jid, fullname, img, img_type, url) {
converse.log("Error while retrieving vcard");
this.add({jid: bare_jid, subscription: 'none', ask: 'request', fullname: jid, is_last: true});
}, this)
);
} else {
return true;
}
} }
} }
} else if (presence_type === 'unsubscribed') { } else if (presence_type === 'unsubscribed') {
@ -2217,6 +2247,15 @@
} }
}, },
requesting_contacts_template: _.template(
'<dt id="xmpp-contact-requests">'+__('Contact requests')+'</dt>'),
contacts_template: _.template(
'<dt id="xmpp-contacts">'+__('My contacts')+'</dt>'),
pending_contacts_template: _.template(
'<dt id="pending-xmpp-contacts">'+__('Pending contacts')+'</dt>'),
initialize: function () { initialize: function () {
this.model.on("add", function (item) { this.model.on("add", function (item) {
var view = new converse.RosterItemView({model: item}); var view = new converse.RosterItemView({model: item});
@ -2240,7 +2279,12 @@
this.model.on("remove", function (item) { this.removeRosterItem(item); }, this); this.model.on("remove", function (item) { this.removeRosterItem(item); }, this);
this.model.on("destroy", function (item) { this.removeRosterItem(item); }, this); this.model.on("destroy", function (item) { this.removeRosterItem(item); }, this);
this.$el.hide().html(this.template()); var roster_markup = this.contacts_template()
if (converse.allow_contact_requests) {
roster_markup = this.requesting_contacts_template() + roster_markup + this.pending_contacts_template();
}
this.$el.hide().html(roster_markup);
this.model.fetch({ this.model.fetch({
add: true, add: true,
success: function (model, resp, options) { success: function (model, resp, options) {
@ -2270,10 +2314,6 @@
chatbox.save(changes); chatbox.save(changes);
}, },
template: _.template('<dt id="xmpp-contact-requests">'+__('Contact requests')+'</dt>' +
'<dt id="xmpp-contacts">'+__('My contacts')+'</dt>' +
'<dt id="pending-xmpp-contacts">'+__('Pending contacts')+'</dt>'),
render: function (item) { render: function (item) {
var $my_contacts = this.$el.find('#xmpp-contacts'), var $my_contacts = this.$el.find('#xmpp-contacts'),
$contact_requests = this.$el.find('#xmpp-contact-requests'), $contact_requests = this.$el.find('#xmpp-contact-requests'),

View File

@ -465,6 +465,16 @@ JS file so that it will include the new settings. Please refer to the
Configuration variables Configuration variables
======================= =======================
allow_contact_requests
----------------------
Default = ``true``
Allow users to add one another as contacts. If this is set to false, the
**Add a contact** widget, **Contact Requests** and **Pending Contacts** roster
sections will all not appear. Additionally, all incoming contact requests will be
ignored.
allow_muc allow_muc
--------- ---------

View File

@ -192,7 +192,6 @@
<script> <script>
require(['converse'], function (converse) { require(['converse'], function (converse) {
converse.initialize({ converse.initialize({
allow_muc: true,
auto_list_rooms: false, auto_list_rooms: false,
auto_subscribe: false, auto_subscribe: false,
bosh_service_url: 'https://bind.opkode.im', // Please use this connection manager only for testing purposes bosh_service_url: 'https://bind.opkode.im', // Please use this connection manager only for testing purposes