diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.js index e3076e3ca5..ec8a45b52e 100644 --- a/frontend_tests/node_tests/filter.js +++ b/frontend_tests/node_tests/filter.js @@ -69,6 +69,7 @@ run_test('basics', () => { assert(!filter.is_search()); assert(filter.can_mark_messages_read()); + assert(filter.allow_use_first_unread_when_narrowing()); assert(filter.can_apply_locally()); operators = [ @@ -80,6 +81,7 @@ run_test('basics', () => { assert(filter.is_search()); assert(!filter.can_mark_messages_read()); + assert(!filter.allow_use_first_unread_when_narrowing()); assert(!filter.can_apply_locally()); assert(!filter.is_exactly('stream')); @@ -110,7 +112,32 @@ run_test('basics', () => { assert(filter.has_operator('has')); assert(!filter.can_apply_locally()); }); +run_test('show_first_unread', () => { + var operators = [ + {operator: 'is', operand: 'any'}, + ]; + var filter = new Filter(operators); + assert(filter.allow_use_first_unread_when_narrowing()); + operators = [ + {operator: 'search', operand: 'query to search'}, + ]; + filter = new Filter(operators); + assert(!filter.allow_use_first_unread_when_narrowing()); + + filter = new Filter(); + filter.can_mark_messages_read = () => true; + assert(filter.allow_use_first_unread_when_narrowing()); + + // Side case + operators = [ + {operator: 'is', operand: 'any'}, + ]; + filter = new Filter(operators); + filter.can_mark_messages_read = () => false; + assert(filter.allow_use_first_unread_when_narrowing()); + +}); run_test('topic_stuff', () => { var operators = [ {operator: 'stream', operand: 'foo'}, diff --git a/static/js/filter.js b/static/js/filter.js index 016236df07..d3db1dea00 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -378,7 +378,9 @@ Filter.prototype = { can_mark_messages_read: function () { return !this.has_operator('search'); }, - + allow_use_first_unread_when_narrowing: function () { + return this.can_mark_messages_read() || this.has_operator('is'); + }, can_apply_locally: function () { if (this.is_search()) { // The semantics for matching keywords are implemented diff --git a/static/js/narrow.js b/static/js/narrow.js index e067cc85ba..56f93f781d 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -226,9 +226,12 @@ exports.activate = function (raw_operators, opts) { if (id_info.final_select_id !== undefined) { anchor = id_info.final_select_id; use_first_unread = false; - } else { + } else if (narrow_state.filter().allow_use_first_unread_when_narrowing()) { anchor = -1; use_first_unread = true; + } else { + anchor = 10000000000000000; + use_first_unread = false; } message_fetch.load_messages_for_narrow({