Commit 00320acf7d6c64fbcfc3688f98b6f22c16d1372f

Authored by Rodrigo Souto
1 parent 41d26bb7

chat-workarounds: ensure availabilities through noosfero

This patch is part of several workarounds done to fix ejabberd problems
with usernames bigger than 8 chars. For more information check:
https://github.com/processone/ejabberd/issues/1152
app/controllers/public/account_controller.rb
@@ -157,6 +157,7 @@ class AccountController < ApplicationController @@ -157,6 +157,7 @@ class AccountController < ApplicationController
157 if logged_in? 157 if logged_in?
158 self.current_user.forget_me 158 self.current_user.forget_me
159 end 159 end
  160 + current_user.update({:chat_status_at => DateTime.now}.merge({:last_chat_status => current_user.chat_status, :chat_status => 'offline'}))
160 reset_session 161 reset_session
161 session[:notice] = _("You have been logged out.") 162 session[:notice] = _("You have been logged out.")
162 redirect_to :controller => 'home', :action => 'index' 163 redirect_to :controller => 'home', :action => 'index'
app/controllers/public/chat_controller.rb
@@ -120,6 +120,15 @@ class ChatController < PublicController @@ -120,6 +120,15 @@ class ChatController < PublicController
120 render :text => rosters.to_json 120 render :text => rosters.to_json
121 end 121 end
122 122
  123 + def availabilities
  124 + availabilities = user.friends.map do |friend|
  125 + status = friend.user.chat_status
  126 + status = 'offline' if status.blank?
  127 + {:jid => friend.jid, :status => status}
  128 + end
  129 + render :text => availabilities.to_json
  130 + end
  131 +
123 protected 132 protected
124 133
125 def check_environment_feature 134 def check_environment_feature
public/javascripts/chat.js
@@ -282,7 +282,7 @@ jQuery(function($) { @@ -282,7 +282,7 @@ jQuery(function($) {
282 282
283 $.ajax({ 283 $.ajax({
284 url: '/chat/rosters', 284 url: '/chat/rosters',
285 - dataType: 'json,' 285 + dataType: 'json',
286 success: function(data){ 286 success: function(data){
287 $(data.friends).each(function(index, friend){ 287 $(data.friends).each(function(index, friend){
288 var jid = friend.jid; 288 var jid = friend.jid;
@@ -321,6 +321,7 @@ jQuery(function($) { @@ -321,6 +321,7 @@ jQuery(function($) {
321 Jabber.connection.addHandler(Jabber.on_presence, null, "presence"); 321 Jabber.connection.addHandler(Jabber.on_presence, null, "presence");
322 Jabber.send_availability_status(Jabber.presence_status); 322 Jabber.send_availability_status(Jabber.presence_status);
323 load_defaults(); 323 load_defaults();
  324 + updateAvailabilities();
324 }); 325 });
325 }, 326 },
326 error: function(data, textStatus, jqXHR){ 327 error: function(data, textStatus, jqXHR){
@@ -412,21 +413,7 @@ jQuery(function($) { @@ -412,21 +413,7 @@ jQuery(function($) {
412 else { 413 else {
413 log('receiving contact presence from ' + presence.from + ' as ' + presence.show); 414 log('receiving contact presence from ' + presence.from + ' as ' + presence.show);
414 var jid = Strophe.getBareJidFromJid(presence.from); 415 var jid = Strophe.getBareJidFromJid(presence.from);
415 - if (jid != Jabber.connection.jid) {  
416 - var jid_id = Jabber.jid_to_id(jid);  
417 - var name = Jabber.name_of(jid_id);  
418 - if(presence.show == 'chat')  
419 - Jabber.remove_notice(jid_id);  
420 - Jabber.insert_or_update_contact(jid, name, presence.show);  
421 - Jabber.update_chat_title();  
422 - }  
423 - else {  
424 - // why server sends presence from myself to me?  
425 - log('ignoring presence from myself');  
426 - if(presence.show=='offline') {  
427 - Jabber.send_availability_status(Jabber.presence_status);  
428 - }  
429 - } 416 + setFriendStatus(jid, presence.show);
430 } 417 }
431 } 418 }
432 return true; 419 return true;
@@ -905,6 +892,24 @@ jQuery(function($) { @@ -905,6 +892,24 @@ jQuery(function($) {
905 } 892 }
906 } 893 }
907 894
  895 + function setFriendStatus(jid, status) {
  896 + if (jid != Jabber.connection.jid) {
  897 + var jid_id = Jabber.jid_to_id(jid);
  898 + var name = Jabber.name_of(jid_id);
  899 + if(status == 'chat')
  900 + Jabber.remove_notice(jid_id);
  901 + Jabber.insert_or_update_contact(jid, name, status);
  902 + Jabber.update_chat_title();
  903 + }
  904 + else {
  905 + // why server sends presence from myself to me?
  906 + log('ignoring presence from myself');
  907 + if(status=='offline') {
  908 + Jabber.send_availability_status(Jabber.presence_status);
  909 + }
  910 + }
  911 + }
  912 +
908 $('.title-bar a').click(function() { 913 $('.title-bar a').click(function() {
909 $(this).parents('.status-group').find('.buddies').toggle('fast'); 914 $(this).parents('.status-group').find('.buddies').toggle('fast');
910 return false; 915 return false;
@@ -974,4 +979,23 @@ jQuery(function($) { @@ -974,4 +979,23 @@ jQuery(function($) {
974 979
975 window.onfocus = function() {Jabber.window_visibility = true}; 980 window.onfocus = function() {Jabber.window_visibility = true};
976 window.onblur = function() {Jabber.window_visibility = false}; 981 window.onblur = function() {Jabber.window_visibility = false};
  982 +
  983 + //FIXME Workaround to solve availability problems
  984 + function updateAvailabilities() {
  985 + $.ajax({
  986 + url: '/chat/availabilities',
  987 + dataType: 'json',
  988 + success: function(data){
  989 + $(data).each(function(index, friend){
  990 + var jid_id = Jabber.jid_to_id(friend.jid);
  991 + if (Jabber.jids[jid_id].presence != friend.status)
  992 + setFriendStatus(friend.jid, friend.status)
  993 + });
  994 + },
  995 + complete: function(data){ setTimeout(updateAvailabilities, 10000) },
  996 + error: function(data, textStatus, jqXHR){
  997 + console.log(data);
  998 + },
  999 + });
  1000 + }
977 }); 1001 });