Commit 8a1382428dd80ffb3f261a018c27b07020c1ec3c

Authored by Victor Costa
Committed by Rodrigo Souto
1 parent e69496e7

Save chat history

Conflicts:
	app/controllers/public/chat_controller.rb

Conflicts:
	app/controllers/public/chat_controller.rb
app/controllers/public/chat_controller.rb
... ... @@ -35,6 +35,29 @@ class ChatController < PublicController
35 35 render :nothing => true
36 36 end
37 37  
  38 + def save_message
  39 + to = environment.profiles.find_by_identifier(params[:to])
  40 + body = params[:body]
  41 +
  42 + ChatMessage.create!(:to => to, :from => user, :body => body)
  43 + render :text => 'ok'
  44 + end
  45 +
  46 + def recent_messages
  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)
  49 +
  50 + messages = messages.map do |message|
  51 + {
  52 + :body => message.body,
  53 + :to => {:id => message.to.identifier, :name => message.to.name, :type => message.to.type},
  54 + :from => {:id => message.from.identifier, :name => message.from.name, :type => message.from.type},
  55 + :created_at => message.created_at
  56 + }
  57 + end
  58 + render :json => messages.reverse
  59 + end
  60 +
38 61 protected
39 62  
40 63 def check_environment_feature
... ...
app/models/chat_message.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +class ChatMessage < ActiveRecord::Base
  2 + attr_accessible :body, :from, :to
  3 +
  4 + belongs_to :to, :class_name => 'Profile'
  5 + belongs_to :from, :class_name => 'Profile'
  6 +
  7 +end
... ...
db/migrate/20140820173129_create_chat_messages.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class CreateChatMessages < ActiveRecord::Migration
  2 + def change
  3 + create_table :chat_messages do |t|
  4 + t.integer :to_id
  5 + t.integer :from_id
  6 + t.string :body
  7 +
  8 + t.timestamps
  9 + end
  10 + end
  11 +end
... ...
public/javascripts/chat.js
... ... @@ -467,7 +467,7 @@ jQuery(function($) {
467 467  
468 468 // save presence_status as offline in Noosfero database when close or reload chat window
469 469 $(window).unload(function() {
470   - disconnect();
  470 + //disconnect();
471 471 });
472 472  
473 473 $('#chat-busy').click(function() {
... ... @@ -508,12 +508,20 @@ jQuery(function($) {
508 508 var jid = $(this).attr('data-to');
509 509 var body = $(this).val();
510 510 body = body.stripScripts();
  511 + save_message(jid, body);
511 512 Jabber.deliver_message(jid, body);
512 513 $(this).val('');
513 514 return false;
514 515 }
515 516 });
516 517  
  518 + function save_message(jid, body) {
  519 + $.post('/chat/save_message', {
  520 + to: getIdentifier(jid),
  521 + body: body
  522 + });
  523 + }
  524 +
517 525 // open new conversation or change to already opened tab
518 526 $('#buddy-list .buddy-list li a').live('click', function() {
519 527 var jid_id = $(this).attr('id');
... ... @@ -534,14 +542,18 @@ jQuery(function($) {
534 542 $('.conversation textarea:visible').val(val + name + ', ').focus();
535 543 });
536 544  
537   - $('.conversation .history').live('click', function() {
  545 + $('#chat .conversation .history').live('click', function() {
538 546 $('.conversation textarea:visible').focus();
539 547 });
540 548  
541   - $('.conversation .back').live('click', function() {
  549 + $('#chat .conversation .back').live('click', function() {
542 550 $('#chat #chat-window .conversation').hide();
543 551 });
544 552  
  553 + $('#chat .toolbar .back').live('click', function() {
  554 + $('#chat').hide('fast');
  555 + });
  556 +
545 557 function create_conversation_tab(title, jid_id) {
546 558 var conversation_id = Jabber.conversation_prefix + jid_id;
547 559 var conversation = $('#' + conversation_id);
... ... @@ -567,9 +579,30 @@ jQuery(function($) {
567 579 panel.find('.history').addClass('room');
568 580 }
569 581 textarea.attr('data-to', jid);
  582 +
  583 + recent_messages(jid);
  584 +
570 585 return panel;
571 586 }
572 587  
  588 + function recent_messages(jid) {
  589 + $.getJSON('/chat/recent_messages', {
  590 + identifier: getIdentifier(jid)
  591 + }, function(data) {
  592 + $.each(data, function(i, message) {
  593 + var body = message['body'];
  594 + var from = message['from'];
  595 + var to = message['to'];
  596 +
  597 + if(from['id']!=getCurrentIdentifier()) {
  598 + Jabber.show_message(from['id']+'@127.0.0.1', from['name'], body, 'other', from['id']);
  599 + } else {
  600 + Jabber.show_message(to['id']+'@127.0.0.1', $own_name, body, 'self', to['id']);
  601 + }
  602 + });
  603 + });
  604 + }
  605 +
573 606 function count_unread_messages(jid_id, hide) {
574 607 var unread = $('.buddy-list #'+jid_id+ ' .unread-messages');
575 608 if (hide) {
... ...