From b11654bf7e54cdd86398a33ce278f3b70e4e319d Mon Sep 17 00:00:00 2001 From: Zaran Date: Mon, 17 Jan 2011 10:34:09 +0100 Subject: Add presence management (change contact display in roster on each presence stanza) --- webclient/lib/basic.js | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'webclient/lib') diff --git a/webclient/lib/basic.js b/webclient/lib/basic.js index 93ef884..84e6ff4 100644 --- a/webclient/lib/basic.js +++ b/webclient/lib/basic.js @@ -33,7 +33,7 @@ function getPresence(contact) } function jid_to_id(jid) { - return Strophe.getBareJidFromJid(jid).replace("@", "-").replace(".", "-"); + return Strophe.getBareJidFromJid(jid).replace(/[@.]/g,'-'); } function getJID(contact) @@ -51,21 +51,17 @@ function insertContact(contact) contacts.each(function () { var locpres = getPresence($(this)); var locjid = getJID($(this)); - if (presence > locpres) { $(this).before(contact); inserted = true; return false; } - else + else if ( (presence == locpres) && (jid < locjid) ) { - if (jid < locjid) - { - $(this).before(contact); - inserted = true; - return false; - } + $(this).before(contact); + inserted = true; + return false; } }); @@ -76,8 +72,39 @@ function insertContact(contact) $('#roster ul').append(contact); } +function onPresence(presence) +{ + var who = $(presence).attr('from'); + var type = $(presence).attr('type'); + if (type !== 'error') + { + var contact = $('#'+jid_to_id(who)); + contact.removeClass('online away offline'); + if (type === 'unavailable') + { + contact.addClass('offline'); + } + else + { + var show = $(presence).find('show').text(); + if (show === '' || show === '') + { + contact.addClass('online'); + } + else + { + contact.addClass('away'); + } + } + contact.remove(); + insertContact(contact); + } + return true; +} + function onRoster(iq) { + $('#roster li').remove(); var elems = iq.getElementsByTagName('query'); var query = elems[0]; Strophe.forEachChild(query, 'item', function(item) @@ -90,6 +117,8 @@ function onRoster(iq) + "
" + jid + "
"); insertContact(contact); }); + connection.addHandler(onPresence,null,'presence', null, null, null, null); + connection.send($pres()); return true; } @@ -114,7 +143,6 @@ function onConnect(status) { log('Strophe is connected.'); getRoster(); - connection.send($pres().tree()); } } -- cgit v1.2.3-70-g09d2