user muting: Add helpers to filter out muted users.

This will allow us to avoid duplication of array filtering
logic of the form-
`Array.filter((user_id) => !muting.is_user_muted(user_id))` and
`Array.filter((person) => !muting.is_user_muted(person.user_id))`
This commit is contained in:
Abhijeet Prasad Bodas 2021-05-05 08:57:06 +05:30 committed by Tim Abbott
parent 615e1f9e05
commit 47764ded76
2 changed files with 36 additions and 0 deletions

View File

@ -92,6 +92,30 @@ test("add_and_remove_mutes", () => {
assert(!muting.is_user_muted(1)); assert(!muting.is_user_muted(1));
}); });
test("get_unmuted_users", () => {
const hamlet = {
user_id: 1,
full_name: "King Hamlet",
};
const cordelia = {
user_id: 2,
full_name: "Cordelia, Lear's Daughter",
};
const othello = {
user_id: 3,
full_name: "Othello, Moor of Venice",
};
muting.add_muted_user(hamlet.user_id);
muting.add_muted_user(cordelia.user_id);
assert.deepEqual(
muting.filter_muted_user_ids([hamlet.user_id, cordelia.user_id, othello.user_id]),
[othello.user_id],
);
assert.deepEqual(muting.filter_muted_users([hamlet, cordelia, othello]), [othello]);
});
test("get_mutes", () => { test("get_mutes", () => {
assert.deepEqual(muting.get_muted_topics(), []); assert.deepEqual(muting.get_muted_topics(), []);
muting.add_muted_topic(office.stream_id, "gossip", 1577836800); muting.add_muted_topic(office.stream_id, "gossip", 1577836800);

View File

@ -98,6 +98,18 @@ export function is_user_muted(user_id) {
return muted_users.has(user_id); return muted_users.has(user_id);
} }
export function filter_muted_user_ids(user_ids) {
// Returns a copy of the user ID list, after removing muted user IDs.
const base_user_ids = [...user_ids];
return base_user_ids.filter((user_id) => !is_user_muted(user_id));
}
export function filter_muted_users(persons) {
// Returns a copy of the people list, after removing muted users.
const base_users = [...persons];
return base_users.filter((person) => !is_user_muted(person.user_id));
}
export function get_muted_users() { export function get_muted_users() {
const users = []; const users = [];
for (const [id, date_muted] of muted_users) { for (const [id, date_muted] of muted_users) {