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') {
|
||||
// User wants to subscribe to us. Always approve and
|
||||
// ask to subscribe to him
|
||||
xmppchat.Roster.authorize(jid);
|
||||
xmppchat.Roster.subscribe(jid);
|
||||
xmppchat.Roster.authorize(bare_jid);
|
||||
xmppchat.Roster.subscribe(bare_jid);
|
||||
|
||||
} else if (ptype === 'unsubscribe') {
|
||||
xmppchat.ChatPartners.removeAll(bare_jid);
|
||||
xmppchat.Roster.unauthorize(bare_jid);
|
||||
$(document).trigger('jarnxmpp.presence', [jid, status, presence]);
|
||||
if (_.indexOf(xmppchat.Roster.getCachedJids(), bare_jid) != -1) {
|
||||
xmppchat.Roster.unauthorize(bare_jid);
|
||||
xmppchat.Roster.unsubscribe(bare_jid);
|
||||
$(document).trigger('jarnxmpp.presence', [jid, 'unsubscribe', presence]);
|
||||
}
|
||||
|
||||
} else if (ptype === 'unsubscribed') {
|
||||
return;
|
||||
|
||||
} else if (ptype !== 'error') { // Presence has changed
|
||||
if (ptype === 'unavailable') {
|
||||
@ -269,7 +274,12 @@ var xmppchat = (function (jarnxmpp, $, console) {
|
||||
};
|
||||
|
||||
ob.Taskbuffer = (function ($) {
|
||||
// Executes tasks one after another (i.e next task is started only when
|
||||
// the previous one has been completed).
|
||||
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.deferred = $.when();
|
||||
buffer.handleTasks = function () {
|
||||
@ -292,6 +302,51 @@ var xmppchat = (function (jarnxmpp, $, console) {
|
||||
return ob;
|
||||
})(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
|
||||
// --------------
|
||||
@ -311,12 +366,9 @@ $(document).ready(function () {
|
||||
|
||||
xmppchat.UI.restoreOpenChats();
|
||||
|
||||
xmppchat.Roster = Strophe._connectionPlugins.roster;
|
||||
xmppchat.Roster._connection = xmppchat.connection;
|
||||
xmppchat.Roster.get(function (contacts) {
|
||||
xmppchat.Roster.contacts = contacts;
|
||||
$(document).trigger('xmppchat.roster_updated');
|
||||
});
|
||||
xmppchat.Roster = xmppchat.Roster(Strophe._connectionPlugins.roster, $, console);
|
||||
xmppchat.Roster.registerCallback(xmppchat.Roster.update);
|
||||
xmppchat.Roster.get();
|
||||
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 () {
|
||||
var select = $('select#select-xmpp-status'),
|
||||
selected = select.find('option[selected]'),
|
||||
@ -521,8 +534,8 @@ $(document).ready(function () {
|
||||
});
|
||||
|
||||
$(document).bind('xmppchat.roster_updated', function (event) {
|
||||
var contacts = xmppchat.Roster.contacts;
|
||||
$(contacts).each(function (idx, contact) {
|
||||
$('#xmpp-contacts').empty();
|
||||
$(xmppchat.Roster.getCached()).each(function (idx, contact) {
|
||||
if (contact.subscription !== 'none') {
|
||||
var user_id = Strophe.getNodeFromJid(contact.jid),
|
||||
bare_jid = Strophe.getBareJidFromJid(contact.jid);
|
||||
@ -625,19 +638,13 @@ $(document).ready(function () {
|
||||
$("a.subscribe-to-user").live('click', function (ev) {
|
||||
ev.preventDefault();
|
||||
xmppchat.Roster.subscribe($(this).attr('data-recipient'));
|
||||
$(this).remove();
|
||||
$('form.search-xmpp-contact').hide();
|
||||
});
|
||||
|
||||
$(".dropdown dd ul li a").click(function() {
|
||||
var jid = xmppchat.connection.jid,
|
||||
value = $(this).find('span').text();
|
||||
|
||||
$(".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);
|
||||
$(".dropdown dd ul li a").click(function(ev) {
|
||||
ev.preventDefault();
|
||||
xmppchat.UI.setOwnStatus(this);
|
||||
});
|
||||
|
||||
$('select#select-xmpp-status').bind('change', function (ev) {
|
||||
|
Loading…
Reference in New Issue
Block a user