diff --git a/frontend_tests/node_tests/narrow_unread.js b/frontend_tests/node_tests/narrow_unread.js index 08c196d02a..4ec577ff69 100644 --- a/frontend_tests/node_tests/narrow_unread.js +++ b/frontend_tests/node_tests/narrow_unread.js @@ -47,6 +47,7 @@ function set_filter(terms) { set_filter(terms); unread_ids = narrow_state.get_unread_ids(); assert.equal(unread_ids, undefined); + assert.equal(narrow_state.get_first_unread_id(), undefined); terms = [ {operator: 'stream', operand: 'bogus'}, @@ -61,6 +62,7 @@ function set_filter(terms) { set_filter(terms); unread_ids = narrow_state.get_unread_ids(); assert.deepEqual(unread_ids, []); + assert.equal(narrow_state.get_first_unread_id(), undefined); msg = { id: 101, @@ -74,6 +76,7 @@ function set_filter(terms) { unread_ids = narrow_state.get_unread_ids(); assert.deepEqual(unread_ids, [msg.id]); + assert.equal(narrow_state.get_first_unread_id(), msg.id); terms = [ {operator: 'stream', operand: 'bogus'}, diff --git a/static/js/narrow_state.js b/static/js/narrow_state.js index 9a54ebce10..9da9bfc19c 100644 --- a/static/js/narrow_state.js +++ b/static/js/narrow_state.js @@ -169,6 +169,21 @@ exports.pm_string = function () { return user_ids_string; }; +exports.get_first_unread_id = function () { + // This function may return undefined for two different + // reasons: we have no unread messages, or we don't have + // an easy, inexpensive way to calculate them. See the + // comment in get_unread_ids() for more details. + + var unread_ids = exports.get_unread_ids(); + + if (unread_ids === undefined) { + return; + } + + return unread_ids[0]; +}; + exports.get_unread_ids = function () { // This function currently only returns valid results for // certain types of narrows, mostly left sidebar narrows.