Commit 09647389dd4e7e06c0cadd78e73ec684eac73632

Authored by Rodrigo Souto
1 parent ce7c5efc

Register when no more messages retrieved to not fetch more messages from that conversations

Showing 1 changed file with 13 additions and 6 deletions   Show diff stats
public/javascripts/chat.js
... ... @@ -27,6 +27,7 @@ jQuery(function($) {
27 27 conversation_prefix: 'conversation-',
28 28 jids: {},
29 29 rooms: {},
  30 + no_more_messages: {},
30 31  
31 32 template: function(selector) {
32 33 return $('#chat #chat-templates '+selector).clone().html();
... ... @@ -124,9 +125,9 @@ jQuery(function($) {
124 125 var tab_id = '#' + Jabber.conversation_prefix + jid_id;
125 126 var history = $(tab_id).find('.history');
126 127  
127   - var offset_container = $('#chat-offset-container-'+offset);
  128 + var offset_container = history.find('.chat-offset-container-'+offset);
128 129 if(offset_container.length == 0)
129   - offset_container = $('<div id="chat-offset-container-'+offset+'"></div>').prependTo(history);
  130 + offset_container = $('<div class="chat-offset-container-'+offset+'"></div>').prependTo(history);
130 131  
131 132 if (offset_container.find('.message:last').attr('data-who') == who) {
132 133 offset_container.find('.message:last .content').append('<p>' + body + '</p>');
... ... @@ -536,9 +537,10 @@ jQuery(function($) {
536 537 var name = Jabber.name_of(jid_id);
537 538 var conversation = create_conversation_tab(name, jid_id);
538 539  
539   - conversation.find('.conversation').show();
  540 + $('.conversation').hide();
  541 + conversation.show();
540 542 count_unread_messages(jid_id, true);
541   - if(conversation.find('#chat-offset-container-0').length == 0)
  543 + if(conversation.find('.chat-offset-container-0').length == 0)
542 544 recent_messages(Jabber.jid_of(jid_id));
543 545 conversation.find('.conversation .input-div textarea.input').focus();
544 546 });
... ... @@ -601,11 +603,16 @@ jQuery(function($) {
601 603 }
602 604  
603 605 function recent_messages(jid, offset) {
  606 + if (Jabber.no_more_messages[jid]) return;
  607 +
604 608 if(!offset) offset = 0;
605 609 start_fetching('.history');
606 610 $.getJSON('/chat/recent_messages', {identifier: getIdentifier(jid), offset: offset}, function(data) {
607   - //TODO Register if no more messages returned and stop trying to load
608   - // more messages in the future.
  611 + // Register if no more messages returned and stop trying to load
  612 + // more messages in the future.
  613 + if(data.length == 0)
  614 + Jabber.no_more_messages[jid] = true;
  615 +
609 616 $.each(data, function(i, message) {
610 617 var body = message['body'];
611 618 var from = message['from'];
... ...