refactor: Clean up can_mark_messages_read.

We now explicitly enumerate various cases, which
should make it easier to change this code.
This commit is contained in:
Steve Howell
2020-01-08 11:59:24 +00:00
committed by Tim Abbott
parent bb579f8823
commit 316eda071d
2 changed files with 44 additions and 7 deletions

View File

@@ -275,11 +275,17 @@ run_test('can_mark_messages_read', () => {
{ operator: 'pm-with', operand: 'joe@example.com,' },
];
const pm_with_negated = [
{ operator: 'pm-with', operand: 'joe@example.com,', negated: true},
];
const group_pm = [
{ operator: 'pm-with', operand: 'joe@example.com,STEVE@foo.com' },
];
filter = new Filter(pm_with);
assert(filter.can_mark_messages_read());
filter = new Filter(pm_with_negated);
assert(!filter.can_mark_messages_read());
filter = new Filter(group_pm);
assert(filter.can_mark_messages_read());
assert_not_mark_read_with_is_operands(group_pm);

View File

@@ -382,13 +382,44 @@ Filter.prototype = {
},
can_mark_messages_read: function () {
return _.every(this._operators, function (elem) {
return (_.contains(['stream', 'topic', 'pm-with'], elem.operator)
|| elem.operator === 'is' && elem.operand === 'private'
|| elem.operator === 'in' && elem.operand === 'all'
|| elem.operator === 'in' && elem.operand === 'home')
&& !elem.negated;
});
const term_types = this.sorted_term_types();
if (_.isEqual(term_types, ['stream', 'topic'])) {
return true;
}
if (_.isEqual(term_types, ['pm-with'])) {
return true;
}
// TODO: Some users really hate it when Zulip marks messages as read
// in interleaved views, so we will eventually have a setting
// that early-exits before the subsequent checks.
if (_.isEqual(term_types, ['stream'])) {
return true;
}
if (_.isEqual(term_types, ['is-private'])) {
return true;
}
if (_.isEqual(term_types, [])) {
// All view
return true;
}
if (this._operators.length === 1) {
const elem = this._operators[0];
if (elem.operator === 'in' && !elem.negated) {
if (elem.operand === 'home' || elem.operand === 'all') {
return true;
}
}
}
return false;
},
allow_use_first_unread_when_narrowing: function () {