diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.js index 63e35c5974..d88a1a290a 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.contains_only_private_messages()); assert(filter.allow_use_first_unread_when_narrowing()); assert(filter.can_apply_locally()); @@ -81,6 +82,7 @@ run_test('basics', () => { assert(filter.is_search()); assert(!filter.can_mark_messages_read()); + assert(!filter.contains_only_private_messages()); assert(!filter.allow_use_first_unread_when_narrowing()); assert(!filter.can_apply_locally()); assert(!filter.is_exactly('stream')); @@ -92,6 +94,7 @@ run_test('basics', () => { {operator: 'stream', operand: 'exclude', negated: true}, ]; filter = new Filter(operators); + assert(!filter.contains_only_private_messages()); assert(!filter.has_operator('stream')); // Negated searches are just like positive searches for our purposes, since @@ -101,6 +104,7 @@ run_test('basics', () => { {operator: 'search', operand: 'stop_word', negated: true}, ]; filter = new Filter(operators); + assert(!filter.contains_only_private_messages()); assert(filter.has_operator('search')); assert(!filter.can_apply_locally()); @@ -116,6 +120,7 @@ run_test('basics', () => { {operator: 'streams', operand: 'public', negated: true}, ]; filter = new Filter(operators); + assert(!filter.contains_only_private_messages()); assert(!filter.has_operator('streams')); assert(filter.has_negated_operand('streams', 'public')); assert(!filter.can_apply_locally()); @@ -124,10 +129,34 @@ run_test('basics', () => { {operator: 'streams', operand: 'public'}, ]; filter = new Filter(operators); + assert(!filter.contains_only_private_messages()); assert(filter.has_operator('streams')); assert(!filter.has_negated_operand('streams', 'public')); assert(!filter.can_apply_locally()); + operators = [ + {operator: 'is', operand: 'private'}, + ]; + filter = new Filter(operators); + assert(filter.contains_only_private_messages()); + assert(!filter.has_operator('search')); + assert(filter.can_apply_locally()); + + operators = [ + {operator: 'pm-with', operand: 'joe@example.com'}, + ]; + filter = new Filter(operators); + assert(filter.contains_only_private_messages()); + assert(!filter.has_operator('search')); + assert(filter.can_apply_locally()); + + operators = [ + {operator: 'group-pm-with', operand: 'joe@example.com'}, + ]; + filter = new Filter(operators); + assert(filter.contains_only_private_messages()); + assert(!filter.has_operator('search')); + assert(filter.can_apply_locally()); }); run_test('show_first_unread', () => { var operators = [ diff --git a/static/js/filter.js b/static/js/filter.js index b110a3984e..3215372dbd 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -384,9 +384,16 @@ 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'); }, + + contains_only_private_messages: function () { + return this.has_operator("is") && this.operands("is")[0] === "private" || + this.has_operator("pm-with") || this.has_operator("group-pm-with"); + }, + 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 56f93f781d..c7b828db04 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -275,8 +275,7 @@ exports.activate = function (raw_operators, opts) { }); } - if (filter.has_operator("is") && filter.operands("is")[0] === "private" - || filter.has_operator("pm-with") || filter.has_operator("group-pm-with")) { + if (filter.contains_only_private_messages()) { compose.update_closed_compose_buttons_for_private(); } else { compose.update_closed_compose_buttons_for_stream();