Better status handling. We shouldn't access ClientStorage from UI side

This commit is contained in:
JC Brand 2012-07-01 19:49:58 +02:00
parent de549fe8bc
commit 4b94968fd8

View File

@ -11,34 +11,37 @@ xmppchat.UI = (function (xmppUI, $, console) {
ob.updateOnPresence = function (jid, status, presence) { ob.updateOnPresence = function (jid, status, presence) {
var user_id = Strophe.getNodeFromJid(jid), var user_id = Strophe.getNodeFromJid(jid),
bare_jid = Strophe.getBareJidFromJid(jid), bare_jid = Strophe.getBareJidFromJid(jid),
resource, existing_user_element, online_count; resource,
online_count,
$chat = $("#"+helpers.hash(bare_jid)),
$chat_content,
existing_user_element = $('#online-users-' + user_id);
if (xmppchat.isOwnUser(jid)) { return; } if (xmppchat.isOwnUser(jid)) { return; }
existing_user_element = $('#online-users-' + user_id); if ($chat.length > 0) {
if ((status === 'offline') || (status === 'unavailable')) { $chat_content = $chat.find(".chat-content");
// Remove the resource. $chat_content.find('div.chat-event').remove();
bare_jid = Strophe.getBareJidFromJid(jid); if (status === 'offline') {
resource = Strophe.getResourceFromJid(jid);
if (xmppchat.ChatPartners.remove(bare_jid, resource) === 0) {
// Only set the status offline if there aren't any other resources for that user
if (existing_user_element.length > 0) {
existing_user_element.attr('class', status);
}
return;
}
} else {
if (existing_user_element.length > 0) {
existing_user_element.attr('class', status);
} else {
xmppchat.Presence.getUserInfo(user_id, function (data) { xmppchat.Presence.getUserInfo(user_id, function (data) {
if ($('#online-users-' + user_id).length > 0) { return; } $chat_content.append($('<div></div>').addClass('chat-event').text(data.fullname + ' has gone offline.'));
var li = $('<li></li>').attr('id', 'online-users-'+user_id).attr('data-recipient', bare_jid); $chat_content.scrollTop($content[0].scrollHeight);
li.append($('<a></a>').addClass('user-details-toggle').text(data.fullname));
$('#online-users').append(li);
}); });
} }
} }
if (existing_user_element.length > 0) {
existing_user_element.attr('class', status);
} else if ((status !== 'offline') && (status !== 'unavailable')) {
xmppchat.Presence.getUserInfo(user_id, function (data) {
if ($('#online-users-' + user_id).length > 0) { return; }
var li = $('<li></li>').attr('id', 'online-users-'+user_id).attr('data-recipient', bare_jid);
li.append($('<a></a>').addClass('user-details-toggle').text(data.fullname));
$('#online-users').append(li);
});
} else { // status is offline and the user isn't shown as online
return;
}
$('#online-count').text(xmppchat.Presence.onlineCount()); $('#online-count').text(xmppchat.Presence.onlineCount());
}; };