From 6cdc3f67dff204cd64c8a5ecdc789c0d70e423c5 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Tue, 23 Apr 2013 13:26:29 -0400 Subject: [PATCH] Only fetch an extra message in get_old_messages if a narrow isn't specified In the case where we're getting old messages for a narrowed view, the anchor message id might not actually be in the result set so there's no reason to fetch an extra message. (imported from commit e610d1f2cb95be3ff9fce6dc95e40c560bc5bf84) --- zephyr/static/js/zephyr.js | 2 +- zephyr/views.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index e51d513c87..77855f490f 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -986,7 +986,7 @@ function load_more_messages(msg_list) { msg_list: msg_list, cont: function (messages) { ui.hide_loading_more_messages_indicator(); - if (messages.length === batch_size + 1) { + if (messages.length >= batch_size) { load_more_enabled = true; } } diff --git a/zephyr/views.py b/zephyr/views.py index c92ce4ff5c..5f75a8d978 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -734,7 +734,10 @@ def get_old_messages_backend(request, user_profile, query = UserMessage.objects.select_related().filter(user_profile=user_profile) \ .order_by('message') + num_extra_messages = 1 + if narrow is not None: + num_extra_messages = 0 build = NarrowBuilder(user_profile, prefix) for operator, operand in narrow: query = build(query, operator, operand) @@ -742,16 +745,18 @@ def get_old_messages_backend(request, user_profile, def add_prefix(**kwargs): return dict((prefix + key, kwargs[key]) for key in kwargs.keys()) - # We add 1 to the number of messages requested to ensure that the - # resulting list always contains the anchor message + # We add 1 to the number of messages requested if no narrow was + # specified to ensure that the resulting list always contains the + # anchor message. If a narrow was specified, the anchor message + # might not match the narrow anyway. if num_before != 0 and num_after == 0: - num_before += 1 + num_before += num_extra_messages query_result = last_n(num_before, query.filter(**add_prefix(id__lte=anchor))) elif num_before == 0 and num_after != 0: - num_after += 1 + num_after += num_extra_messages query_result = query.filter(**add_prefix(id__gte=anchor))[:num_after] else: - num_after += 1 + num_after += num_extra_messages query_result = (last_n(num_before, query.filter(**add_prefix(id__lt=anchor))) + list(query.filter(**add_prefix(id__gte=anchor))[:num_after]))