diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 20c4564..75a4aaf 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -157,6 +157,7 @@ class AccountController < ApplicationController if logged_in? self.current_user.forget_me end + current_user.update({:chat_status_at => DateTime.now}.merge({:last_chat_status => current_user.chat_status, :chat_status => 'offline'})) reset_session session[:notice] = _("You have been logged out.") redirect_to :controller => 'home', :action => 'index' diff --git a/app/controllers/public/chat_controller.rb b/app/controllers/public/chat_controller.rb index 6aeb7be..fd76376 100644 --- a/app/controllers/public/chat_controller.rb +++ b/app/controllers/public/chat_controller.rb @@ -120,6 +120,15 @@ class ChatController < PublicController render :text => rosters.to_json end + def availabilities + availabilities = user.friends.map do |friend| + status = friend.user.chat_status + status = 'offline' if status.blank? + {:jid => friend.jid, :status => status} + end + render :text => availabilities.to_json + end + protected def check_environment_feature diff --git a/public/javascripts/chat.js b/public/javascripts/chat.js index c06ab88..c297390 100644 --- a/public/javascripts/chat.js +++ b/public/javascripts/chat.js @@ -282,7 +282,7 @@ jQuery(function($) { $.ajax({ url: '/chat/rosters', - dataType: 'json,' + dataType: 'json', success: function(data){ $(data.friends).each(function(index, friend){ var jid = friend.jid; @@ -321,6 +321,7 @@ jQuery(function($) { Jabber.connection.addHandler(Jabber.on_presence, null, "presence"); Jabber.send_availability_status(Jabber.presence_status); load_defaults(); + updateAvailabilities(); }); }, error: function(data, textStatus, jqXHR){ @@ -412,21 +413,7 @@ jQuery(function($) { else { log('receiving contact presence from ' + presence.from + ' as ' + presence.show); var jid = Strophe.getBareJidFromJid(presence.from); - if (jid != Jabber.connection.jid) { - var jid_id = Jabber.jid_to_id(jid); - var name = Jabber.name_of(jid_id); - if(presence.show == 'chat') - Jabber.remove_notice(jid_id); - Jabber.insert_or_update_contact(jid, name, presence.show); - Jabber.update_chat_title(); - } - else { - // why server sends presence from myself to me? - log('ignoring presence from myself'); - if(presence.show=='offline') { - Jabber.send_availability_status(Jabber.presence_status); - } - } + setFriendStatus(jid, presence.show); } } return true; @@ -905,6 +892,24 @@ jQuery(function($) { } } + function setFriendStatus(jid, status) { + if (jid != Jabber.connection.jid) { + var jid_id = Jabber.jid_to_id(jid); + var name = Jabber.name_of(jid_id); + if(status == 'chat') + Jabber.remove_notice(jid_id); + Jabber.insert_or_update_contact(jid, name, status); + Jabber.update_chat_title(); + } + else { + // why server sends presence from myself to me? + log('ignoring presence from myself'); + if(status=='offline') { + Jabber.send_availability_status(Jabber.presence_status); + } + } + } + $('.title-bar a').click(function() { $(this).parents('.status-group').find('.buddies').toggle('fast'); return false; @@ -974,4 +979,23 @@ jQuery(function($) { window.onfocus = function() {Jabber.window_visibility = true}; window.onblur = function() {Jabber.window_visibility = false}; + + //FIXME Workaround to solve availability problems + function updateAvailabilities() { + $.ajax({ + url: '/chat/availabilities', + dataType: 'json', + success: function(data){ + $(data).each(function(index, friend){ + var jid_id = Jabber.jid_to_id(friend.jid); + if (Jabber.jids[jid_id].presence != friend.status) + setFriendStatus(friend.jid, friend.status) + }); + }, + complete: function(data){ setTimeout(updateAvailabilities, 10000) }, + error: function(data, textStatus, jqXHR){ + console.log(data); + }, + }); + } }); -- libgit2 0.21.2