From 47764ded76095fc9a8dd204bf7d4fbf252817bf6 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Bodas Date: Wed, 5 May 2021 08:57:06 +0530 Subject: [PATCH] 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))` --- frontend_tests/node_tests/muting.js | 24 ++++++++++++++++++++++++ static/js/muting.js | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/frontend_tests/node_tests/muting.js b/frontend_tests/node_tests/muting.js index f6c177da27..dbd09c7cba 100644 --- a/frontend_tests/node_tests/muting.js +++ b/frontend_tests/node_tests/muting.js @@ -92,6 +92,30 @@ test("add_and_remove_mutes", () => { 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", () => { assert.deepEqual(muting.get_muted_topics(), []); muting.add_muted_topic(office.stream_id, "gossip", 1577836800); diff --git a/static/js/muting.js b/static/js/muting.js index 291a6beb55..eeaf4a9e65 100644 --- a/static/js/muting.js +++ b/static/js/muting.js @@ -98,6 +98,18 @@ export function is_user_muted(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() { const users = []; for (const [id, date_muted] of muted_users) {