From 5336d5439fe2f3de8bbf77319e7f26c59d66d52e Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 26 Jan 2015 20:01:00 -0300 Subject: [PATCH] chat: session --- app/controllers/public/chat_controller.rb | 26 ++++++++++++++++++++++++++ public/javascripts/chat.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/app/controllers/public/chat_controller.rb b/app/controllers/public/chat_controller.rb index b31cc6c..c3da723 100644 --- a/app/controllers/public/chat_controller.rb +++ b/app/controllers/public/chat_controller.rb @@ -6,6 +6,7 @@ class ChatController < PublicController def start_session login = user.jid password = current_user.crypted_password + session[:chat] ||= {:rooms => []} begin jid, sid, rid = RubyBOSH.initialize_session(login, password, "http://#{environment.default_hostname}/http-bind", :wait => 30, :hold => 1, :window => 5) @@ -16,6 +17,31 @@ class ChatController < PublicController end end + def toggle + session[:chat][:status] = session[:chat][:status] == 'opened' ? 'closed' : 'opened' + render :nothing => true + end + + def tab + session[:chat][:tab_id] = params[:tab_id] + render :nothing => true + end + + def join + session[:chat][:rooms] << params[:room_id] + session[:chat][:rooms].uniq! + render :nothing => true + end + + def leave + session[:chat][:rooms].delete(params[:room_id]) + render :nothing => true + end + + def my_session + render :text => session[:chat].to_json, :layout => false + end + def avatar profile = environment.profiles.find_by_identifier(params[:id]) filename, mimetype = profile_icon(profile, :minor, true) diff --git a/public/javascripts/chat.js b/public/javascripts/chat.js index 0dda218..87e311f 100644 --- a/public/javascripts/chat.js +++ b/public/javascripts/chat.js @@ -182,18 +182,37 @@ jQuery(function($) { Jabber.show_status(presence); }, - enter_room: function(room_jid) { + enter_room: function(jid, push) { + if(push == undefined) + push = true + var jid_id = Jabber.jid_to_id(jid); + var conversation_id = Jabber.conversation_prefix + jid_id; + var button = $('#' + conversation_id + ' .join'); + button.hide(); + button.siblings('.leave').show(); Jabber.connection.send( - $pres({to: room_jid + '/' + $own_name}).c('x', {xmlns: Strophe.NS.MUC}).c('history', {maxchars: 0}) + $pres({to: jid + '/' + $own_name}).c('x', {xmlns: Strophe.NS.MUC}).c('history', {maxchars: 0}) ); - Jabber.insert_or_update_group(room_jid, 'online'); + Jabber.insert_or_update_group(jid, 'online'); Jabber.update_chat_title(); + sort_conversations(); + if(push) + $.post('/chat/join', {room_id: jid}); }, - leave_room: function(room_jid) { - Jabber.connection.send($pres({from: Jabber.connection.jid, to: room_jid + '/' + $own_name, type: 'unavailable'})) - Jabber.insert_or_update_group(room_jid, 'offline'); - //FIXME remove group + leave_room: function(jid) { + if(push == undefined) + push = true + var jid_id = Jabber.jid_to_id(jid); + var conversation_id = Jabber.conversation_prefix + jid_id; + var button = $('#' + conversation_id + ' .leave'); + button.hide(); + button.siblings('.join').show(); + Jabber.connection.send($pres({from: Jabber.connection.jid, to: jid + '/' + $own_name, type: 'unavailable'})) + Jabber.insert_or_update_group(jid, 'offline'); + sort_conversations(); + if(push) + $.post('/chat/leave', {room_id: jid}); }, update_chat_title: function () { @@ -262,6 +281,7 @@ jQuery(function($) { // set up presence handler and send initial presence Jabber.connection.addHandler(Jabber.on_presence, null, "presence"); Jabber.send_availability_status(Jabber.presence_status); + load_defaults(); }, // NOTE: cause Noosfero store's rosters in database based on friendship relation between people @@ -574,6 +594,7 @@ jQuery(function($) { if(conversation.find('.chat-offset-container-0').length == 0) recent_messages(Jabber.jid_of(jid_id)); conversation.find('.conversation .input-div textarea.input').focus(); + $.post('/chat/tab', {tab_id: jid_id}); }); // put name into text area when click in one occupant @@ -677,6 +698,20 @@ jQuery(function($) { }) } + function load_defaults() { + $.getJSON('/chat/my_session', {}, function(data) { + $.each(data.rooms, function(i, room_jid) { + Jabber.enter_room(room_jid, false); + }) + + $('#'+data.tab_id).click(); + + console.log(data); + if(data.status == 'opened') + toggle_chat_window(); + }) + } + function count_unread_messages(jid_id, hide) { var unread = $('.buddies #'+jid_id+ ' .unread-messages'); if (hide) { @@ -810,20 +845,17 @@ jQuery(function($) { $('#chat-label').click(function(){ toggle_chat_window(); + $.post('/chat/toggle'); }); $('.room-action.join').live('click', function(){ - Jabber.enter_room($(this).data('jid')); - $(this).hide(); - $(this).siblings('.leave').show(); - sort_conversations(); + var jid = $(this).data('jid'); + Jabber.enter_room(jid); }); $('.room-action.leave').live('click', function(){ - Jabber.leave_room($(this).data('jid')); - $(this).hide(); - $(this).siblings('.join').show(); - sort_conversations(); + var jid = $(this).data('jid'); + Jabber.leave_room(jid); }); }); -- libgit2 0.21.2