Skip to content

Commit

Permalink
Add filter.is_valid_id_from().
Browse files Browse the repository at this point in the history
We will use this to find the first id from a list of
message ids that matches a filter.  (This will help us
during narrowing to determine whether we have at least
one good message locally, so that we can render something
useful before waiting for the server.)
  • Loading branch information
Steve Howell authored and timabbott committed May 8, 2018
1 parent a176380 commit ea581c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions frontend_tests/node_tests/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ zrequire('people');
zrequire('Handlebars', 'handlebars');
zrequire('Filter', 'js/filter');

set_global('message_store', {});
set_global('page_params', {});
set_global('feature_flags', {});

Expand Down Expand Up @@ -800,6 +801,32 @@ function make_sub(name, stream_id) {
assert.deepEqual(term_types, ['stream', 'topic', 'sender']);
}());

(function test_first_valid_id_from() {
const terms = [
{operator: 'is', operand: 'alerted'},
];

const filter = new Filter(terms);

const messages = {
5: { id: 5, alerted: true },
10: { id: 10 },
20: { id: 20, alerted: true },
30: { id: 30, type: 'stream' },
40: { id: 40, alerted: false },
};

const msg_ids = [10, 20, 30, 40];

message_store.get = () => {};

assert.equal(filter.first_valid_id_from(msg_ids), undefined);

message_store.get = (msg_id) => messages[msg_id];

assert.equal(filter.first_valid_id_from(msg_ids), 20);
}());

(function test_update_email() {
var terms = [
{operator: 'pm-with', operand: '[email protected]'},
Expand Down
16 changes: 16 additions & 0 deletions static/js/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,22 @@ Filter.prototype = {
return _.isEqual(term_types, wanted_term_types);
},

first_valid_id_from: function (msg_ids) {
var predicate = this.predicate();

var first_id = _.find(msg_ids, function (msg_id) {
var message = message_store.get(msg_id);

if (message === undefined) {
return false;
}

return predicate(message);
});

return first_id;
},

update_email: function (user_id, new_email) {
_.each(this._operators, function (term) {
switch (term.operator) {
Expand Down

0 comments on commit ea581c5

Please sign in to comment.