diff --git a/frontend_tests/node_tests/pm_list.js b/frontend_tests/node_tests/pm_list.js index 291eefd57c..d31603ab0d 100644 --- a/frontend_tests/node_tests/pm_list.js +++ b/frontend_tests/node_tests/pm_list.js @@ -255,3 +255,39 @@ run_test('get_active_user_ids_string', () => { pm_list.get_active_user_ids_string(), '101,102'); }); + +run_test('is_all_privates', () => { + narrow_state.filter = () => {}; + + assert.equal( + pm_list.is_all_privates(), + false); + + narrow_state.filter = () => { + return { + operands: (operand) => { + assert.equal(operand, 'pm-with'); + return ['alice@zulip.com']; + }, + }; + }; + assert.equal( + pm_list.is_all_privates(), + false); + + narrow_state.filter = () => { + return { + operands: (operand) => { + if (operand === 'pm-with') { + return []; + } + assert.equal(operand, 'is'); + return ['private', 'starred']; + }, + }; + }; + + assert.equal( + pm_list.is_all_privates(), + true); +}); diff --git a/static/js/pm_list.js b/static/js/pm_list.js index 7e7576749a..cda39735e9 100644 --- a/static/js/pm_list.js +++ b/static/js/pm_list.js @@ -135,27 +135,31 @@ exports.rebuild_recent = function () { resize.resize_stream_filters_container(); }; +exports.is_all_privates = function () { + const filter = narrow_state.filter(); + + if (!filter) { + return false; + } + + // Handle the edge case that somebody said pm-with:alice is:private. + // This should basically be treated like pm-with:alice. + const conversation = filter.operands('pm-with'); + if (conversation.length !== 0) { + return false; + } + + return _.contains(filter.operands('is'), "private"); +}; + exports.update_private_messages = function () { if (!narrow_state.active()) { return; } - let is_pm_filter = false; - const filter = narrow_state.filter(); - - if (filter) { - const conversation = filter.operands('pm-with'); - if (conversation.length === 0) { - is_pm_filter = _.contains(filter.operands('is'), "private"); - } - // We don't support having two pm-with: operands in a search - // (Group PMs are represented as a single pm-with operand - // containing a list). - } - exports.rebuild_recent(); - if (is_pm_filter) { + if (exports.is_all_privates()) { $(".top_left_private_messages").addClass('active-filter'); } };