Better roster support (adding/removing of contacts).
This commit is contained in:
parent
8cdc8d303b
commit
997bcdc5f2
74
chat.js
74
chat.js
@ -234,13 +234,18 @@ var xmppchat = (function (jarnxmpp, $, console) {
|
|||||||
if (ptype === 'subscribe') {
|
if (ptype === 'subscribe') {
|
||||||
// User wants to subscribe to us. Always approve and
|
// User wants to subscribe to us. Always approve and
|
||||||
// ask to subscribe to him
|
// ask to subscribe to him
|
||||||
xmppchat.Roster.authorize(jid);
|
xmppchat.Roster.authorize(bare_jid);
|
||||||
xmppchat.Roster.subscribe(jid);
|
xmppchat.Roster.subscribe(bare_jid);
|
||||||
|
|
||||||
} else if (ptype === 'unsubscribe') {
|
} else if (ptype === 'unsubscribe') {
|
||||||
xmppchat.ChatPartners.removeAll(bare_jid);
|
if (_.indexOf(xmppchat.Roster.getCachedJids(), bare_jid) != -1) {
|
||||||
xmppchat.Roster.unauthorize(bare_jid);
|
xmppchat.Roster.unauthorize(bare_jid);
|
||||||
$(document).trigger('jarnxmpp.presence', [jid, status, presence]);
|
xmppchat.Roster.unsubscribe(bare_jid);
|
||||||
|
$(document).trigger('jarnxmpp.presence', [jid, 'unsubscribe', presence]);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (ptype === 'unsubscribed') {
|
||||||
|
return;
|
||||||
|
|
||||||
} else if (ptype !== 'error') { // Presence has changed
|
} else if (ptype !== 'error') { // Presence has changed
|
||||||
if (ptype === 'unavailable') {
|
if (ptype === 'unavailable') {
|
||||||
@ -269,7 +274,12 @@ var xmppchat = (function (jarnxmpp, $, console) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ob.Taskbuffer = (function ($) {
|
ob.Taskbuffer = (function ($) {
|
||||||
|
// Executes tasks one after another (i.e next task is started only when
|
||||||
|
// the previous one has been completed).
|
||||||
buffer = {};
|
buffer = {};
|
||||||
|
// Tasks must be objects with keys: 'that', 'method' and 'parameters'
|
||||||
|
// 'that' the context for the method, while 'parameters' is the list of arguments
|
||||||
|
// passed to it.
|
||||||
buffer.tasks = [];
|
buffer.tasks = [];
|
||||||
buffer.deferred = $.when();
|
buffer.deferred = $.when();
|
||||||
buffer.handleTasks = function () {
|
buffer.handleTasks = function () {
|
||||||
@ -292,6 +302,51 @@ var xmppchat = (function (jarnxmpp, $, console) {
|
|||||||
return ob;
|
return ob;
|
||||||
})(jarnxmpp || {}, jQuery, console || {log: function(){}});
|
})(jarnxmpp || {}, jQuery, console || {log: function(){}});
|
||||||
|
|
||||||
|
xmppchat.Roster = (function (roster, jquery, console) {
|
||||||
|
var contacts = {},
|
||||||
|
ob = roster;
|
||||||
|
|
||||||
|
_updateCache = function () {
|
||||||
|
if (this.subscription === 'none') {
|
||||||
|
delete contacts[this.jid];
|
||||||
|
} else {
|
||||||
|
contacts[this.jid] = this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_triggerEvent = function () {
|
||||||
|
$(document).trigger('xmppchat.roster_updated');
|
||||||
|
};
|
||||||
|
|
||||||
|
ob._connection = xmppchat.connection;
|
||||||
|
|
||||||
|
ob.update = function (items, item) {
|
||||||
|
old_cache = ob.getCached();
|
||||||
|
for (var i=0; i<items.length; i++) {
|
||||||
|
if (items[i].subscription === 'none') {
|
||||||
|
delete contacts[items[i].jid];
|
||||||
|
} else {
|
||||||
|
contacts[items[i].jid] = items[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log('update, size is: '+ _.size(contacts));
|
||||||
|
if (!_.isEqual(old_cache, ob.getCached())) {
|
||||||
|
console.log('triggering event');
|
||||||
|
$(document).trigger('xmppchat.roster_updated');
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
ob.getCached = function () {
|
||||||
|
return _.values(contacts);
|
||||||
|
};
|
||||||
|
|
||||||
|
ob.getCachedJids = function () {
|
||||||
|
return _.keys(contacts);
|
||||||
|
};
|
||||||
|
return ob;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Event handlers
|
// Event handlers
|
||||||
// --------------
|
// --------------
|
||||||
@ -311,12 +366,9 @@ $(document).ready(function () {
|
|||||||
|
|
||||||
xmppchat.UI.restoreOpenChats();
|
xmppchat.UI.restoreOpenChats();
|
||||||
|
|
||||||
xmppchat.Roster = Strophe._connectionPlugins.roster;
|
xmppchat.Roster = xmppchat.Roster(Strophe._connectionPlugins.roster, $, console);
|
||||||
xmppchat.Roster._connection = xmppchat.connection;
|
xmppchat.Roster.registerCallback(xmppchat.Roster.update);
|
||||||
xmppchat.Roster.get(function (contacts) {
|
xmppchat.Roster.get();
|
||||||
xmppchat.Roster.contacts = contacts;
|
|
||||||
$(document).trigger('xmppchat.roster_updated');
|
|
||||||
});
|
|
||||||
xmppchat.Presence.sendPresence();
|
xmppchat.Presence.sendPresence();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
33
chatui.js
33
chatui.js
@ -472,6 +472,19 @@ xmppchat.UI = (function (xmppUI, $, console) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ob.setOwnStatus = function (el) {
|
||||||
|
var jid = xmppchat.connection.jid,
|
||||||
|
value = $(el).find('span').text();
|
||||||
|
|
||||||
|
$(".dropdown dt a").html('I am ' + value);
|
||||||
|
$(".dropdown dt a").attr('class', value);
|
||||||
|
$(".dropdown dd ul").hide();
|
||||||
|
$("#source").val($(el).find("span.value").html());
|
||||||
|
|
||||||
|
xmppchat.Presence.sendPresence(value);
|
||||||
|
xmppchat.Storage.set(xmppchat.username+'-xmpp-status', value);
|
||||||
|
};
|
||||||
|
|
||||||
ob.createStatusSelectWidget = function () {
|
ob.createStatusSelectWidget = function () {
|
||||||
var select = $('select#select-xmpp-status'),
|
var select = $('select#select-xmpp-status'),
|
||||||
selected = select.find('option[selected]'),
|
selected = select.find('option[selected]'),
|
||||||
@ -521,8 +534,8 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$(document).bind('xmppchat.roster_updated', function (event) {
|
$(document).bind('xmppchat.roster_updated', function (event) {
|
||||||
var contacts = xmppchat.Roster.contacts;
|
$('#xmpp-contacts').empty();
|
||||||
$(contacts).each(function (idx, contact) {
|
$(xmppchat.Roster.getCached()).each(function (idx, contact) {
|
||||||
if (contact.subscription !== 'none') {
|
if (contact.subscription !== 'none') {
|
||||||
var user_id = Strophe.getNodeFromJid(contact.jid),
|
var user_id = Strophe.getNodeFromJid(contact.jid),
|
||||||
bare_jid = Strophe.getBareJidFromJid(contact.jid);
|
bare_jid = Strophe.getBareJidFromJid(contact.jid);
|
||||||
@ -625,19 +638,13 @@ $(document).ready(function () {
|
|||||||
$("a.subscribe-to-user").live('click', function (ev) {
|
$("a.subscribe-to-user").live('click', function (ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
xmppchat.Roster.subscribe($(this).attr('data-recipient'));
|
xmppchat.Roster.subscribe($(this).attr('data-recipient'));
|
||||||
|
$(this).remove();
|
||||||
|
$('form.search-xmpp-contact').hide();
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".dropdown dd ul li a").click(function() {
|
$(".dropdown dd ul li a").click(function(ev) {
|
||||||
var jid = xmppchat.connection.jid,
|
ev.preventDefault();
|
||||||
value = $(this).find('span').text();
|
xmppchat.UI.setOwnStatus(this);
|
||||||
|
|
||||||
$(".dropdown dt a").html('I am ' + value);
|
|
||||||
$(".dropdown dt a").attr('class', value);
|
|
||||||
$(".dropdown dd ul").hide();
|
|
||||||
$("#source").val($(this).find("span.value").html());
|
|
||||||
|
|
||||||
xmppchat.Presence.sendPresence(value);
|
|
||||||
xmppchat.Storage.set(xmppchat.username+'-xmpp-status', value);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$('select#select-xmpp-status').bind('change', function (ev) {
|
$('select#select-xmpp-status').bind('change', function (ev) {
|
||||||
|
Loading…
Reference in New Issue
Block a user