narrow_state: Add pm_ids_set helper.

This provides a more easy to work with model of which users, if any,
are involved in the current DM conversation, if any.
This commit is contained in:
Tim Abbott 2024-02-06 13:21:20 -08:00
parent 772ed724c0
commit f4ad102d39
2 changed files with 11 additions and 0 deletions

View File

@ -186,6 +186,12 @@ export function pm_ids_string(): string | undefined {
return user_ids_string; return user_ids_string;
} }
export function pm_ids_set(): Set<number> {
const ids_string = pm_ids_string();
const pm_ids_list = ids_string ? people.user_ids_string_to_ids_array(ids_string) : [];
return new Set(pm_ids_list);
}
export function pm_emails_string(): string | undefined { export function pm_emails_string(): string | undefined {
if (current_filter === undefined) { if (current_filter === undefined) {
return undefined; return undefined;

View File

@ -304,18 +304,22 @@ test("pm_ids_string", () => {
// direct messages) with real users. // direct messages) with real users.
narrow_state.set_current_filter(undefined); narrow_state.set_current_filter(undefined);
assert.equal(narrow_state.pm_ids_string(), undefined); assert.equal(narrow_state.pm_ids_string(), undefined);
assert.deepStrictEqual(narrow_state.pm_ids_set(), new Set());
set_filter([ set_filter([
["stream", "Foo"], ["stream", "Foo"],
["topic", "Bar"], ["topic", "Bar"],
]); ]);
assert.equal(narrow_state.pm_ids_string(), undefined); assert.equal(narrow_state.pm_ids_string(), undefined);
assert.deepStrictEqual(narrow_state.pm_ids_set(), new Set());
set_filter([["dm", ""]]); set_filter([["dm", ""]]);
assert.equal(narrow_state.pm_ids_string(), undefined); assert.equal(narrow_state.pm_ids_string(), undefined);
assert.deepStrictEqual(narrow_state.pm_ids_set(), new Set());
set_filter([["dm", "bogus@foo.com"]]); set_filter([["dm", "bogus@foo.com"]]);
assert.equal(narrow_state.pm_ids_string(), undefined); assert.equal(narrow_state.pm_ids_string(), undefined);
assert.deepStrictEqual(narrow_state.pm_ids_set(), new Set());
const alice = { const alice = {
email: "alice@foo.com", email: "alice@foo.com",
@ -334,4 +338,5 @@ test("pm_ids_string", () => {
set_filter([["dm", "bob@foo.com,alice@foo.com"]]); set_filter([["dm", "bob@foo.com,alice@foo.com"]]);
assert.equal(narrow_state.pm_ids_string(), "444,555"); assert.equal(narrow_state.pm_ids_string(), "444,555");
assert.deepStrictEqual(narrow_state.pm_ids_set(), new Set([444, 555]));
}); });