Add narrow_state.pm_string().

This commit is contained in:
Steve Howell
2018-02-09 16:26:45 -05:00
committed by Tim Abbott
parent 6ea2765c9f
commit 13abed229c
2 changed files with 65 additions and 0 deletions

View File

@@ -212,3 +212,44 @@ function set_filter(operators) {
assert.equal(narrow_state.stream(), undefined);
}());
(function test_pm_string() {
// This function will return undefined unless we're clearly
// narrowed to a specific PM (including huddles) with real
// users.
narrow_state.set_current_filter(undefined);
assert.equal(narrow_state.pm_string(), undefined);
set_filter([['stream', 'Foo'], ['topic', 'Bar']]);
assert.equal(narrow_state.pm_string(), undefined);
set_filter([['pm-with', '']]);
assert.equal(narrow_state.pm_string(), undefined);
var called = false;
blueslip.warn = function (error) {
assert.equal(error, 'Unknown emails: bogus@foo.com');
called = true;
};
set_filter([['pm-with', 'bogus@foo.com']]);
assert.equal(narrow_state.pm_string(), undefined);
assert(called);
var alice = {
email: 'alice@foo.com',
user_id: 444,
full_name: 'Alice',
};
var bob = {
email: 'bob@foo.com',
user_id: 555,
full_name: 'Bob',
};
people.add(alice);
people.add(bob);
set_filter([['pm-with', 'bob@foo.com,alice@foo.com']]);
assert.equal(narrow_state.pm_string(), '444,555');
}());

View File

@@ -120,6 +120,30 @@ exports.topic = function () {
return undefined;
};
exports.pm_string = function () {
// If you are narrowed to a PM conversation
// with users 4, 5, and 99, this will return "4,5,99"
if (current_filter === undefined) {
return;
}
var operands = current_filter.operands("pm-with");
if (operands.length !== 1) {
return;
}
var emails_string = operands[0];
if (!emails_string) {
return;
}
var user_ids_string = people.emails_strings_to_user_ids_string(emails_string);
return user_ids_string;
};
// Are we narrowed to PMs: all PMs or PMs with particular people.
exports.narrowed_to_pms = function () {
if (current_filter === undefined) {