From 4f4e982ed1f4a15dfe7f5a16539b05db347adacb Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Thu, 25 Apr 2013 13:38:21 -0400 Subject: [PATCH] Don't do narrows with search operators on the client (imported from commit 98e6a06f5c10a384e6295b3281d23a061ecdecab) --- zephyr/static/js/narrow.js | 34 +++++++++++++++------------- zephyr/static/js/zephyr.js | 46 +++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/zephyr/static/js/narrow.js b/zephyr/static/js/narrow.js index c486cfdafb..b027ce9770 100644 --- a/zephyr/static/js/narrow.js +++ b/zephyr/static/js/narrow.js @@ -32,6 +32,16 @@ Filter.prototype = { } }, + can_apply_locally: function Filter_can_apply_locally() { + var retval = true; + $.each(this._operators, function (idx, elem) { + if (elem[0] === "search") { + retval = false; + return false; + }}); + return retval; + }, + _canonicalize_operators: function Filter__canonicalize_operators(operators_mixed_case) { var new_operators = []; // We don't use $.map because it flattens returned arrays. @@ -98,19 +108,6 @@ Filter.prototype = { message.reply_to.toLowerCase() !== operand.split(',').sort().join(',')) return false; break; - - case 'search': - var words = operand.trim().split(/\s+/); - var j; - for (j = 0; j < words.length; ++j) { - if (message.content.toLowerCase().indexOf(words[j]) === -1) { - if ((message.type !== 'stream') || - (message.subject.toLowerCase().indexOf(words[j]) === -1)) { - return false; - } - } - } - break; } } @@ -126,8 +123,12 @@ exports.active = function () { return current_filter !== undefined; }; +exports.filter = function () { + return current_filter; +}; + exports.predicate = function () { - if (current_filter === undefined) { + if (current_filter === undefined || ! current_filter.can_apply_locally()) { return function () { return true; }; } return current_filter.predicate(); @@ -371,8 +372,9 @@ exports.activate = function (operators, opts) { } // Don't bother populating a message list when it won't contain - // the message we want anyway - if (all_msg_list.get(then_select_id) !== undefined) { + // the message we want anyway or if the filter can't be applied + // locally. + if (all_msg_list.get(then_select_id) !== undefined && current_filter.can_apply_locally()) { add_messages(all_msg_list.all(), narrowed_msg_list); } diff --git a/zephyr/static/js/zephyr.js b/zephyr/static/js/zephyr.js index 77855f490f..1fddc91829 100644 --- a/zephyr/static/js/zephyr.js +++ b/zephyr/static/js/zephyr.js @@ -670,6 +670,46 @@ function deduplicate_messages(messages) { }); } +function maybe_add_narrowed_messages(messages, msg_list) { + var ids = []; + $.each(messages, function (idx, elem) { + ids.push(elem.id); + }); + + $.ajax({ + type: 'POST', + url: '/json/messages_in_narrow', + data: {msg_ids: JSON.stringify(ids), + narrow: JSON.stringify(narrow.public_operators())}, + dataType: 'json', + timeout: 5000, + success: function (data) { + if (msg_list !== current_msg_list) { + // We unnarrowed in the mean time + return; + } + + messages = $.grep(messages, function (elem) { + return ($.inArray(elem.id, data.msg_ids) !== -1); + }); + + add_messages(messages, msg_list); + process_visible_unread_messages(); + compose.update_faded_messages(); + }, + error: function (xhr) { + // We might want to be more clever here + $('#connection-error').show(); + setTimeout(function () { + if (msg_list === current_msg_list) { + // Don't actually try again if we unnarrowed + // while waiting + maybe_add_narrowed_messages(messages, msg_list); + } + }, 5000); + }}); +} + var get_updates_xhr; var get_updates_timeout; function get_updates(options) { @@ -755,7 +795,11 @@ function get_updates(options) { messages = deduplicate_messages(messages); if (narrow.active()) { - add_messages(messages, narrowed_msg_list); + if (narrow.filter().can_apply_locally()) { + add_messages(messages, narrowed_msg_list); + } else { + maybe_add_narrowed_messages(messages, narrowed_msg_list); + } } add_messages(messages, all_msg_list); add_messages(messages, home_msg_list);