mute user: Add frontend functions to maintain data.

Muted users are stored in a map with key as user ID and
the value as the timestamp of muting.
Names can be easily fetched from existing functions
in `people.js` and hence not stored.
This commit is contained in:
Abhijeet Prasad Bodas 2021-03-27 17:12:16 +05:30 committed by Tim Abbott
parent 3bfcaa3968
commit 1de9444242
6 changed files with 133 additions and 2 deletions

View File

@ -239,6 +239,17 @@ run_test("muted_topics", (override) => {
assert_same(args.muted_topics, event.muted_topics);
});
run_test("muted_users", (override) => {
const event = event_fixtures.muted_users;
const stub = make_stub();
override(muting_ui, "handle_user_updates", stub.f);
dispatch(event);
assert.equal(stub.num_calls, 1);
const args = stub.get_args("muted_users");
assert_same(args.muted_users, event.muted_users);
});
run_test("presence", (override) => {
const event = event_fixtures.presence;

View File

@ -167,6 +167,20 @@ exports.fixtures = {
],
},
muted_users: {
type: "muted_users",
muted_users: [
{
id: 5,
timestamp: fake_then,
},
{
id: 23,
timestamp: fake_now,
},
],
},
presence: {
type: "presence",
email: "alice@example.com",

View File

@ -50,9 +50,12 @@ function test(label, f) {
test("edge_cases", () => {
// private messages
assert(!muting.is_topic_muted(undefined, undefined));
// invalid user
assert(!muting.is_user_muted(undefined));
});
test("basics", () => {
test("add_and_remove_mutes", () => {
assert(!muting.is_topic_muted(devel.stream_id, "java"));
muting.add_muted_topic(devel.stream_id, "java");
assert(muting.is_topic_muted(devel.stream_id, "java"));
@ -71,9 +74,24 @@ test("basics", () => {
// test unknown stream is harmless too
muting.remove_muted_topic(unknown.stream_id, "java");
assert(!muting.is_topic_muted(unknown.stream_id, "java"));
assert(!muting.is_user_muted(1));
muting.add_muted_user(1);
assert(muting.is_user_muted(1));
// test idempotentcy
muting.add_muted_user(1);
assert(muting.is_user_muted(1));
muting.remove_muted_user(1);
assert(!muting.is_user_muted(1));
// test idempotentcy
muting.remove_muted_user(1);
assert(!muting.is_user_muted(1));
});
test("basics", () => {
test("get_mutes", () => {
assert.deepEqual(muting.get_muted_topics(), []);
muting.add_muted_topic(office.stream_id, "gossip", 1577836800);
muting.add_muted_topic(devel.stream_id, "java", 1577836700);
@ -95,6 +113,23 @@ test("basics", () => {
topic: "gossip",
},
]);
assert.deepEqual(muting.get_muted_users(), []);
muting.add_muted_user(6, 1577836800);
muting.add_muted_user(4, 1577836800);
const muted_users = muting.get_muted_users().sort((a, b) => a.date_muted - b.date_muted);
assert.deepEqual(muted_users, [
{
date_muted: 1577836800000,
date_muted_str: "Jan\u00A001,\u00A02020",
id: 6,
},
{
date_muted: 1577836800000,
date_muted_str: "Jan\u00A001,\u00A02020",
id: 4,
},
]);
});
test("unknown streams", () => {
@ -105,6 +140,10 @@ test("unknown streams", () => {
["design", "typography", 1577836800],
["BOGUS STREAM", "whatever", 1577836800],
];
page_params.muted_users = [
{id: 3, timestamp: 1577836800},
{id: 2, timestamp: 1577836800},
];
muting.initialize();
assert.deepEqual(muting.get_muted_topics().sort(), [
@ -123,6 +162,19 @@ test("unknown streams", () => {
topic: "typography",
},
]);
assert.deepEqual(muting.get_muted_users().sort(), [
{
date_muted: 1577836800000,
date_muted_str: "Jan\u00A001,\u00A02020",
id: 3,
},
{
date_muted: 1577836800000,
date_muted_str: "Jan\u00A001,\u00A02020",
id: 2,
},
]);
});
test("case_insensitivity", () => {

View File

@ -5,6 +5,7 @@ import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
const muted_topics = new Map();
const muted_users = new Map();
function get_time_from_date_muted(date_muted) {
if (date_muted === undefined) {
@ -76,6 +77,51 @@ export function set_muted_topics(tuples) {
}
}
export function add_muted_user(user_id, date_muted) {
const time = get_time_from_date_muted(date_muted);
if (user_id) {
muted_users.set(user_id, time);
}
}
export function remove_muted_user(user_id) {
if (user_id) {
muted_users.delete(user_id);
}
}
export function is_user_muted(user_id) {
if (user_id === undefined) {
return false;
}
return muted_users.has(user_id);
}
export function get_muted_users() {
const users = [];
for (const [id, date_muted] of muted_users) {
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
users.push({
id,
date_muted,
date_muted_str,
});
}
return users;
}
export function set_muted_users(list) {
muted_users.clear();
for (const user of list) {
if (user !== undefined && user.id !== undefined) {
add_muted_user(user.id, user.timestamp);
}
}
}
export function initialize() {
set_muted_topics(page_params.muted_topics);
set_muted_users(page_params.muted_users);
}

View File

@ -157,3 +157,7 @@ export function toggle_topic_mute(message) {
mute_topic(stream_id, topic);
}
}
export function handle_user_updates(muted_user_ids) {
muting.set_muted_users(muted_user_ids);
}

View File

@ -133,6 +133,10 @@ export function dispatch_normal_event(event) {
muting_ui.handle_topic_updates(event.muted_topics);
break;
case "muted_users":
muting_ui.handle_user_updates(event.muted_users);
break;
case "presence":
activity.update_presence_info(event.user_id, event.presence, event.server_timestamp);
break;