Commit 5336d5439fe2f3de8bbf77319e7f26c59d66d52e

Authored by Rodrigo Souto
1 parent 87aca610

chat: session

app/controllers/public/chat_controller.rb
@@ -6,6 +6,7 @@ class ChatController < PublicController @@ -6,6 +6,7 @@ class ChatController < PublicController
6 def start_session 6 def start_session
7 login = user.jid 7 login = user.jid
8 password = current_user.crypted_password 8 password = current_user.crypted_password
  9 + session[:chat] ||= {:rooms => []}
9 begin 10 begin
10 jid, sid, rid = RubyBOSH.initialize_session(login, password, "http://#{environment.default_hostname}/http-bind", 11 jid, sid, rid = RubyBOSH.initialize_session(login, password, "http://#{environment.default_hostname}/http-bind",
11 :wait => 30, :hold => 1, :window => 5) 12 :wait => 30, :hold => 1, :window => 5)
@@ -16,6 +17,31 @@ class ChatController < PublicController @@ -16,6 +17,31 @@ class ChatController < PublicController
16 end 17 end
17 end 18 end
18 19
  20 + def toggle
  21 + session[:chat][:status] = session[:chat][:status] == 'opened' ? 'closed' : 'opened'
  22 + render :nothing => true
  23 + end
  24 +
  25 + def tab
  26 + session[:chat][:tab_id] = params[:tab_id]
  27 + render :nothing => true
  28 + end
  29 +
  30 + def join
  31 + session[:chat][:rooms] << params[:room_id]
  32 + session[:chat][:rooms].uniq!
  33 + render :nothing => true
  34 + end
  35 +
  36 + def leave
  37 + session[:chat][:rooms].delete(params[:room_id])
  38 + render :nothing => true
  39 + end
  40 +
  41 + def my_session
  42 + render :text => session[:chat].to_json, :layout => false
  43 + end
  44 +
19 def avatar 45 def avatar
20 profile = environment.profiles.find_by_identifier(params[:id]) 46 profile = environment.profiles.find_by_identifier(params[:id])
21 filename, mimetype = profile_icon(profile, :minor, true) 47 filename, mimetype = profile_icon(profile, :minor, true)
public/javascripts/chat.js
@@ -182,18 +182,37 @@ jQuery(function($) { @@ -182,18 +182,37 @@ jQuery(function($) {
182 Jabber.show_status(presence); 182 Jabber.show_status(presence);
183 }, 183 },
184 184
185 - enter_room: function(room_jid) { 185 + enter_room: function(jid, push) {
  186 + if(push == undefined)
  187 + push = true
  188 + var jid_id = Jabber.jid_to_id(jid);
  189 + var conversation_id = Jabber.conversation_prefix + jid_id;
  190 + var button = $('#' + conversation_id + ' .join');
  191 + button.hide();
  192 + button.siblings('.leave').show();
186 Jabber.connection.send( 193 Jabber.connection.send(
187 - $pres({to: room_jid + '/' + $own_name}).c('x', {xmlns: Strophe.NS.MUC}).c('history', {maxchars: 0}) 194 + $pres({to: jid + '/' + $own_name}).c('x', {xmlns: Strophe.NS.MUC}).c('history', {maxchars: 0})
188 ); 195 );
189 - Jabber.insert_or_update_group(room_jid, 'online'); 196 + Jabber.insert_or_update_group(jid, 'online');
190 Jabber.update_chat_title(); 197 Jabber.update_chat_title();
  198 + sort_conversations();
  199 + if(push)
  200 + $.post('/chat/join', {room_id: jid});
191 }, 201 },
192 202
193 - leave_room: function(room_jid) {  
194 - Jabber.connection.send($pres({from: Jabber.connection.jid, to: room_jid + '/' + $own_name, type: 'unavailable'}))  
195 - Jabber.insert_or_update_group(room_jid, 'offline');  
196 - //FIXME remove group 203 + leave_room: function(jid) {
  204 + if(push == undefined)
  205 + push = true
  206 + var jid_id = Jabber.jid_to_id(jid);
  207 + var conversation_id = Jabber.conversation_prefix + jid_id;
  208 + var button = $('#' + conversation_id + ' .leave');
  209 + button.hide();
  210 + button.siblings('.join').show();
  211 + Jabber.connection.send($pres({from: Jabber.connection.jid, to: jid + '/' + $own_name, type: 'unavailable'}))
  212 + Jabber.insert_or_update_group(jid, 'offline');
  213 + sort_conversations();
  214 + if(push)
  215 + $.post('/chat/leave', {room_id: jid});
197 }, 216 },
198 217
199 update_chat_title: function () { 218 update_chat_title: function () {
@@ -262,6 +281,7 @@ jQuery(function($) { @@ -262,6 +281,7 @@ jQuery(function($) {
262 // set up presence handler and send initial presence 281 // set up presence handler and send initial presence
263 Jabber.connection.addHandler(Jabber.on_presence, null, "presence"); 282 Jabber.connection.addHandler(Jabber.on_presence, null, "presence");
264 Jabber.send_availability_status(Jabber.presence_status); 283 Jabber.send_availability_status(Jabber.presence_status);
  284 + load_defaults();
265 }, 285 },
266 286
267 // NOTE: cause Noosfero store's rosters in database based on friendship relation between people 287 // NOTE: cause Noosfero store's rosters in database based on friendship relation between people
@@ -574,6 +594,7 @@ jQuery(function($) { @@ -574,6 +594,7 @@ jQuery(function($) {
574 if(conversation.find('.chat-offset-container-0').length == 0) 594 if(conversation.find('.chat-offset-container-0').length == 0)
575 recent_messages(Jabber.jid_of(jid_id)); 595 recent_messages(Jabber.jid_of(jid_id));
576 conversation.find('.conversation .input-div textarea.input').focus(); 596 conversation.find('.conversation .input-div textarea.input').focus();
  597 + $.post('/chat/tab', {tab_id: jid_id});
577 }); 598 });
578 599
579 // put name into text area when click in one occupant 600 // put name into text area when click in one occupant
@@ -677,6 +698,20 @@ jQuery(function($) { @@ -677,6 +698,20 @@ jQuery(function($) {
677 }) 698 })
678 } 699 }
679 700
  701 + function load_defaults() {
  702 + $.getJSON('/chat/my_session', {}, function(data) {
  703 + $.each(data.rooms, function(i, room_jid) {
  704 + Jabber.enter_room(room_jid, false);
  705 + })
  706 +
  707 + $('#'+data.tab_id).click();
  708 +
  709 + console.log(data);
  710 + if(data.status == 'opened')
  711 + toggle_chat_window();
  712 + })
  713 + }
  714 +
680 function count_unread_messages(jid_id, hide) { 715 function count_unread_messages(jid_id, hide) {
681 var unread = $('.buddies #'+jid_id+ ' .unread-messages'); 716 var unread = $('.buddies #'+jid_id+ ' .unread-messages');
682 if (hide) { 717 if (hide) {
@@ -810,20 +845,17 @@ jQuery(function($) { @@ -810,20 +845,17 @@ jQuery(function($) {
810 845
811 $('#chat-label').click(function(){ 846 $('#chat-label').click(function(){
812 toggle_chat_window(); 847 toggle_chat_window();
  848 + $.post('/chat/toggle');
813 }); 849 });
814 850
815 $('.room-action.join').live('click', function(){ 851 $('.room-action.join').live('click', function(){
816 - Jabber.enter_room($(this).data('jid'));  
817 - $(this).hide();  
818 - $(this).siblings('.leave').show();  
819 - sort_conversations(); 852 + var jid = $(this).data('jid');
  853 + Jabber.enter_room(jid);
820 }); 854 });
821 855
822 $('.room-action.leave').live('click', function(){ 856 $('.room-action.leave').live('click', function(){
823 - Jabber.leave_room($(this).data('jid'));  
824 - $(this).hide();  
825 - $(this).siblings('.join').show();  
826 - sort_conversations(); 857 + var jid = $(this).data('jid');
  858 + Jabber.leave_room(jid);
827 }); 859 });
828 860
829 }); 861 });