diff --git a/app/controllers/public/chat_controller.rb b/app/controllers/public/chat_controller.rb index 0536c42..6172900 100644 --- a/app/controllers/public/chat_controller.rb +++ b/app/controllers/public/chat_controller.rb @@ -35,6 +35,29 @@ class ChatController < PublicController render :nothing => true end + def save_message + to = environment.profiles.find_by_identifier(params[:to]) + body = params[:body] + + ChatMessage.create!(:to => to, :from => user, :body => body) + render :text => 'ok' + end + + def recent_messages + other = environment.profiles.find_by_identifier(params[:identifier]) + 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) + + messages = messages.map do |message| + { + :body => message.body, + :to => {:id => message.to.identifier, :name => message.to.name, :type => message.to.type}, + :from => {:id => message.from.identifier, :name => message.from.name, :type => message.from.type}, + :created_at => message.created_at + } + end + render :json => messages.reverse + end + protected def check_environment_feature diff --git a/app/models/chat_message.rb b/app/models/chat_message.rb new file mode 100644 index 0000000..6fecffb --- /dev/null +++ b/app/models/chat_message.rb @@ -0,0 +1,7 @@ +class ChatMessage < ActiveRecord::Base + attr_accessible :body, :from, :to + + belongs_to :to, :class_name => 'Profile' + belongs_to :from, :class_name => 'Profile' + +end diff --git a/db/migrate/20140820173129_create_chat_messages.rb b/db/migrate/20140820173129_create_chat_messages.rb new file mode 100644 index 0000000..83ee2ea --- /dev/null +++ b/db/migrate/20140820173129_create_chat_messages.rb @@ -0,0 +1,11 @@ +class CreateChatMessages < ActiveRecord::Migration + def change + create_table :chat_messages do |t| + t.integer :to_id + t.integer :from_id + t.string :body + + t.timestamps + end + end +end diff --git a/public/javascripts/chat.js b/public/javascripts/chat.js index 7a4b52a..168f020 100644 --- a/public/javascripts/chat.js +++ b/public/javascripts/chat.js @@ -467,7 +467,7 @@ jQuery(function($) { // save presence_status as offline in Noosfero database when close or reload chat window $(window).unload(function() { - disconnect(); + //disconnect(); }); $('#chat-busy').click(function() { @@ -508,12 +508,20 @@ jQuery(function($) { var jid = $(this).attr('data-to'); var body = $(this).val(); body = body.stripScripts(); + save_message(jid, body); Jabber.deliver_message(jid, body); $(this).val(''); return false; } }); + function save_message(jid, body) { + $.post('/chat/save_message', { + to: getIdentifier(jid), + body: body + }); + } + // open new conversation or change to already opened tab $('#buddy-list .buddy-list li a').live('click', function() { var jid_id = $(this).attr('id'); @@ -534,14 +542,18 @@ jQuery(function($) { $('.conversation textarea:visible').val(val + name + ', ').focus(); }); - $('.conversation .history').live('click', function() { + $('#chat .conversation .history').live('click', function() { $('.conversation textarea:visible').focus(); }); - $('.conversation .back').live('click', function() { + $('#chat .conversation .back').live('click', function() { $('#chat #chat-window .conversation').hide(); }); + $('#chat .toolbar .back').live('click', function() { + $('#chat').hide('fast'); + }); + function create_conversation_tab(title, jid_id) { var conversation_id = Jabber.conversation_prefix + jid_id; var conversation = $('#' + conversation_id); @@ -567,9 +579,30 @@ jQuery(function($) { panel.find('.history').addClass('room'); } textarea.attr('data-to', jid); + + recent_messages(jid); + return panel; } + function recent_messages(jid) { + $.getJSON('/chat/recent_messages', { + identifier: getIdentifier(jid) + }, function(data) { + $.each(data, function(i, message) { + var body = message['body']; + var from = message['from']; + var to = message['to']; + + if(from['id']!=getCurrentIdentifier()) { + Jabber.show_message(from['id']+'@127.0.0.1', from['name'], body, 'other', from['id']); + } else { + Jabber.show_message(to['id']+'@127.0.0.1', $own_name, body, 'self', to['id']); + } + }); + }); + } + function count_unread_messages(jid_id, hide) { var unread = $('.buddy-list #'+jid_id+ ' .unread-messages'); if (hide) { -- libgit2 0.21.2