Commit 024c2e05b347bd1c897ba7ee39a04847177ff45a

Authored by Victor Costa
Committed by Rodrigo Souto
1 parent bbde3e9f

Chat improvement

app/controllers/public/chat_controller.rb
@@ -45,9 +45,14 @@ class ChatController < PublicController @@ -45,9 +45,14 @@ class ChatController < PublicController
45 45
46 def recent_messages 46 def recent_messages
47 other = environment.profiles.find_by_identifier(params[:identifier]) 47 other = environment.profiles.find_by_identifier(params[:identifier])
48 - messages = ChatMessage.where('(to_id=:other and from_id=:me) or (to_id=:me and from_id=:other)', {:me => user.id, :other => other.id}).order('created_at DESC').includes(:to, :from).limit(20) 48 + if other.kind_of?(Organization)
  49 + messages = ChatMessage.where('to_id=:other', :other => other.id)
  50 + else
  51 + messages = ChatMessage.where('(to_id=:other and from_id=:me) or (to_id=:me and from_id=:other)', {:me => user.id, :other => other.id})
  52 + end
49 53
50 - messages = messages.map do |message| 54 + messages = messages.order('created_at DESC').includes(:to, :from).limit(20)
  55 + messages_json = messages.map do |message|
51 { 56 {
52 :body => message.body, 57 :body => message.body,
53 :to => {:id => message.to.identifier, :name => message.to.name, :type => message.to.type}, 58 :to => {:id => message.to.identifier, :name => message.to.name, :type => message.to.type},
@@ -55,7 +60,7 @@ class ChatController < PublicController @@ -55,7 +60,7 @@ class ChatController < PublicController
55 :created_at => message.created_at 60 :created_at => message.created_at
56 } 61 }
57 end 62 end
58 - render :json => messages.reverse 63 + render :json => messages_json.reverse
59 end 64 end
60 65
61 protected 66 protected
app/views/shared/logged_in/xmpp_chat.html.erb
@@ -25,7 +25,10 @@ @@ -25,7 +25,10 @@
25 25
26 <div id="friends"> 26 <div id="friends">
27 <div id='title-bar'><h1 class='title'><%= _("Online") %>&nbsp;(<span id='friends-online'>0</span>)</h1></div> 27 <div id='title-bar'><h1 class='title'><%= _("Online") %>&nbsp;(<span id='friends-online'>0</span>)</h1></div>
28 - <ul class='buddy-list'></ul> 28 + <ul class='buddy-list online'></ul>
  29 +
  30 + <div id='title-bar'><h1 class='title'><%= _("Offline") %></h1></div>
  31 + <ul class='buddy-list offline'></ul>
29 </div> 32 </div>
30 33
31 <div id="rooms"> 34 <div id="rooms">
public/javascripts/chat.js
@@ -24,7 +24,6 @@ jQuery(function($) { @@ -24,7 +24,6 @@ jQuery(function($) {
24 muc_domain: $muc_domain, 24 muc_domain: $muc_domain,
25 muc_supported: false, 25 muc_supported: false,
26 presence_status: '', 26 presence_status: '',
27 - update_presence_status_every: $update_presence_status_every, // time in seconds of how often update presence status to Noosfero DB  
28 conversation_prefix: 'conversation-', 27 conversation_prefix: 'conversation-',
29 jids: {}, 28 jids: {},
30 rooms: {}, 29 rooms: {},
@@ -62,11 +61,9 @@ jQuery(function($) { @@ -62,11 +61,9 @@ jQuery(function($) {
62 .replace(/%{presence_status}/g, presence) 61 .replace(/%{presence_status}/g, presence)
63 .replace('%{avatar}', getAvatar(identifier)) 62 .replace('%{avatar}', getAvatar(identifier))
64 .replace('%{name}', name); 63 .replace('%{name}', name);
65 - if ($(item).length > 0) {  
66 - $(item).parent('li').replaceWith(html);  
67 - } else {  
68 - $(list).append(html);  
69 - } 64 +
  65 + $(item).parent().remove();
  66 + $(list).append(html);
70 Jabber.jids[jid_id] = {jid: jid, name: name, type: type, presence: presence}; 67 Jabber.jids[jid_id] = {jid: jid, name: name, type: type, presence: presence};
71 }, 68 },
72 insert_or_update_group: function (jid, presence) { 69 insert_or_update_group: function (jid, presence) {
@@ -82,9 +79,10 @@ jQuery(function($) { @@ -82,9 +79,10 @@ jQuery(function($) {
82 }, 79 },
83 insert_or_update_contact: function (jid, name, presence) { 80 insert_or_update_contact: function (jid, name, presence) {
84 var jid_id = Jabber.jid_to_id(jid); 81 var jid_id = Jabber.jid_to_id(jid);
85 - var list = $('#buddy-list #friends .buddy-list');  
86 var item = $('#' + jid_id); 82 var item = $('#' + jid_id);
87 presence = presence || ($(item).length > 0 ? $(item).parent('li').attr('class') : 'offline'); 83 presence = presence || ($(item).length > 0 ? $(item).parent('li').attr('class') : 'offline');
  84 + var list = $('#buddy-list #friends .buddy-list' + (presence=='offline' ? '.offline' : '.online'));
  85 +
88 log('adding or updating contact ' + jid + ' as ' + presence); 86 log('adding or updating contact ' + jid + ' as ' + presence);
89 Jabber.insert_or_update_user(list, item, jid, name, presence, $('#chat #chat-templates .buddy-item').clone().html(), 'chat'); 87 Jabber.insert_or_update_user(list, item, jid, name, presence, $('#chat #chat-templates .buddy-item').clone().html(), 'chat');
90 $("#chat-window .tab a[href='#"+ Jabber.conversation_prefix + jid_id +"']") 88 $("#chat-window .tab a[href='#"+ Jabber.conversation_prefix + jid_id +"']")
@@ -182,7 +180,7 @@ jQuery(function($) { @@ -182,7 +180,7 @@ jQuery(function($) {
182 }, 180 },
183 181
184 update_chat_title: function () { 182 update_chat_title: function () {
185 - var friends_online = $('#buddy-list .buddy-list li:not(.offline)').length; 183 + var friends_online = $('#buddy-list .buddy-list.online li').length;
186 $('#friends-online').text(friends_online); 184 $('#friends-online').text(friends_online);
187 }, 185 },
188 186
@@ -200,7 +198,6 @@ jQuery(function($) { @@ -200,7 +198,6 @@ jQuery(function($) {
200 break; 198 break;
201 case Strophe.Status.DISCONNECTED: 199 case Strophe.Status.DISCONNECTED:
202 log('disconnected'); 200 log('disconnected');
203 - //Jabber.show_status('');  
204 $('#buddy-list ul.buddy-list, .occupant-list ul.occupant-list').html(''); 201 $('#buddy-list ul.buddy-list, .occupant-list ul.occupant-list').html('');
205 Jabber.update_chat_title(); 202 Jabber.update_chat_title();
206 $('#buddy-list .toolbar').removeClass('small-loading-dark'); 203 $('#buddy-list .toolbar').removeClass('small-loading-dark');
@@ -402,13 +399,6 @@ jQuery(function($) { @@ -402,13 +399,6 @@ jQuery(function($) {
402 Jabber.on_muc_support 399 Jabber.on_muc_support
403 ); 400 );
404 401
405 - // Timed handle to save presence status to Noosfero DB every (N) seconds  
406 - Jabber.connection.addTimedHandler(Jabber.update_presence_status_every * 1000, function() {  
407 - log('saving presence status to Noosfero DB');  
408 - $.get('/chat/update_presence_status', { status: {chat_status: Jabber.presence_status} });  
409 - return true;  
410 - });  
411 -  
412 // uncomment for extra debugging 402 // uncomment for extra debugging
413 //Strophe.log = function (lvl, msg) { log(msg); }; 403 //Strophe.log = function (lvl, msg) { log(msg); };
414 }, 404 },
@@ -467,11 +457,6 @@ jQuery(function($) { @@ -467,11 +457,6 @@ jQuery(function($) {
467 disconnect(); 457 disconnect();
468 }); 458 });
469 459
470 - // save presence_status as offline in Noosfero database when close or reload chat window  
471 - $(window).unload(function() {  
472 - //disconnect();  
473 - });  
474 -  
475 $('#chat-busy').click(function() { 460 $('#chat-busy').click(function() {
476 Jabber.presence_status = 'dnd'; 461 Jabber.presence_status = 'dnd';
477 Jabber.connect(); 462 Jabber.connect();
@@ -496,6 +481,7 @@ jQuery(function($) { @@ -496,6 +481,7 @@ jQuery(function($) {
496 Jabber.enter_room(jid); 481 Jabber.enter_room(jid);
497 var conversation = create_conversation_tab(name, jid_id); 482 var conversation = create_conversation_tab(name, jid_id);
498 conversation.find('.conversation').show(); 483 conversation.find('.conversation').show();
  484 + recent_messages(jid);
499 } 485 }
500 } 486 }
501 else { 487 else {
@@ -532,6 +518,7 @@ jQuery(function($) { @@ -532,6 +518,7 @@ jQuery(function($) {
532 518
533 conversation.find('.conversation').show(); 519 conversation.find('.conversation').show();
534 count_unread_messages(jid_id, true); 520 count_unread_messages(jid_id, true);
  521 + recent_messages(Jabber.jid_of(jid_id));
535 conversation.find('.conversation .input-div textarea.input').focus(); 522 conversation.find('.conversation .input-div textarea.input').focus();
536 }); 523 });
537 524
@@ -582,8 +569,6 @@ jQuery(function($) { @@ -582,8 +569,6 @@ jQuery(function($) {
582 } 569 }
583 textarea.attr('data-to', jid); 570 textarea.attr('data-to', jid);
584 571
585 - recent_messages(jid);  
586 -  
587 return panel; 572 return panel;
588 } 573 }
589 574
@@ -596,11 +581,14 @@ jQuery(function($) { @@ -596,11 +581,14 @@ jQuery(function($) {
596 var from = message['from']; 581 var from = message['from'];
597 var to = message['to']; 582 var to = message['to'];
598 var date = message['created_at']; 583 var date = message['created_at'];
  584 + var group = to['type']=='Community' ? 'conference.' : '';
  585 + var domain = '127.0.0.1';
599 586
600 - if(from['id']!=getCurrentIdentifier()) {  
601 - Jabber.show_message(from['id']+'@127.0.0.1', from['name'], body, 'other', from['id'], date); 587 + if(from['id']==getCurrentIdentifier()) {
  588 + Jabber.show_message(to['id']+'@'+group+domain, $own_name, body, 'self', to['id'], date);
602 } else { 589 } else {
603 - Jabber.show_message(to['id']+'@127.0.0.1', $own_name, body, 'self', to['id'], date); 590 + var target = group!='' ? to['id'] : from['id']
  591 + Jabber.show_message(target+'@'+group+domain, from['name'], body, 'other', from['id'], date);
604 } 592 }
605 }); 593 });
606 }); 594 });
@@ -684,10 +672,3 @@ jQuery(function($) { @@ -684,10 +672,3 @@ jQuery(function($) {
684 } 672 }
685 $('#chat #buddy-list').perfectScrollbar(); 673 $('#chat #buddy-list').perfectScrollbar();
686 }); 674 });
687 -  
688 -function checkTime(i) {  
689 - if (i<10) {  
690 - i="0" + i;  
691 - }  
692 - return i;  
693 -}  
public/stylesheets/chat.css
@@ -47,7 +47,7 @@ @@ -47,7 +47,7 @@
47 max-height: 40px; 47 max-height: 40px;
48 } 48 }
49 #buddy-list .buddy-list li.offline, .occupant-list li.offline { 49 #buddy-list .buddy-list li.offline, .occupant-list li.offline {
50 - display: none; 50 +/* display: none;*/
51 } 51 }
52 #chat #buddy-list .toolbar { 52 #chat #buddy-list .toolbar {
53 border: 0; 53 border: 0;