Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
refactor: Extract 'get_key' out of functions in 'typing_data.ts'.
Dinesh3bc448c1e09b
diff --git a/web/src/typing_data.ts b/web/src/typing_data.tsindex d6303064d8d..7546c1b3a5b 100644--- a/web/src/typing_data.ts+++ b/web/src/typing_data.ts@@ -11,13 +11,12 @@ export function clear_for_testing(): void {inbound_timer_dict.clear();}-function get_key(group: number[]): string {+export function get_direct_message_conversation_key(group: number[]): string {const ids = util.sorted_ids(group);return ids.join(",");}-export function add_typist(group: number[], typist: number): void {- const key = get_key(group);+export function add_typist(key: string, typist: number): void {const current = typist_dct.get(key) ?? [];if (!current.includes(typist)) {current.push(typist);@@ -25,8 +24,7 @@ export function add_typist(group: number[], typist: number): void {typist_dct.set(key, util.sorted_ids(current));}-export function remove_typist(group: number[], typist: number): boolean {- const key = get_key(group);+export function remove_typist(key: string, typist: number): boolean {let current = typist_dct.get(key) ?? [];if (!current.includes(typist)) {@@ -40,7 +38,7 @@ export function remove_typist(group: number[], typist: number): boolean {}export function get_group_typists(group: number[]): number[] {- const key = get_key(group);+ const key = get_direct_message_conversation_key(group);const user_ids = typist_dct.get(key) ?? [];return muted_users.filter_muted_user_ids(user_ids);}@@ -53,8 +51,7 @@ export function get_all_direct_message_typists(): number[] {// The next functions aren't pure data, but it is easy// enough to mock the setTimeout/clearTimeout functions.-export function clear_inbound_timer(group: number[]): void {- const key = get_key(group);+export function clear_inbound_timer(key: string): void {const timer = inbound_timer_dict.get(key);if (timer) {clearTimeout(timer);@@ -62,13 +59,8 @@ export function clear_inbound_timer(group: number[]): void {}}-export function kickstart_inbound_timer(- group: number[],- delay: number,- callback: () => void,-): void {- const key = get_key(group);- clear_inbound_timer(group);+export function kickstart_inbound_timer(key: string, delay: number, callback: () => void): void {+ clear_inbound_timer(key);const timer = setTimeout(callback, delay);inbound_timer_dict.set(key, timer);}diff --git a/web/src/typing_events.js b/web/src/typing_events.jsindex 3d30d428947..3ce0aa4fe3b 100644--- a/web/src/typing_events.js+++ b/web/src/typing_events.js@@ -73,9 +73,10 @@ export function hide_notification(event) {const recipients = event.recipients.map((user) => user.user_id);recipients.sort();- typing_data.clear_inbound_timer(recipients);+ const conversation_key = typing_data.get_direct_message_conversation_key(recipients);+ typing_data.clear_inbound_timer(conversation_key);- const removed = typing_data.remove_typist(recipients, event.sender.user_id);+ const removed = typing_data.remove_typist(conversation_key, event.sender.user_id);if (removed) {render_notifications_for_narrow();@@ -89,11 +90,12 @@ export function display_notification(event) {const sender_id = event.sender.user_id;event.sender.name = people.get_by_user_id(sender_id).full_name;- typing_data.add_typist(recipients, sender_id);+ const conversation_key = typing_data.get_direct_message_conversation_key(recipients);+ typing_data.add_typist(conversation_key, sender_id);render_notifications_for_narrow();- typing_data.kickstart_inbound_timer(recipients, typing_started_expiry_period, () => {+ typing_data.kickstart_inbound_timer(conversation_key, typing_started_expiry_period, () => {hide_notification(event);});}diff --git a/web/tests/example8.test.js b/web/tests/example8.test.jsindex 085ce236e5d..2e9d6ae7273 100644--- a/web/tests/example8.test.js+++ b/web/tests/example8.test.js@@ -76,6 +76,7 @@ run_test("typing_events.render_notifications_for_narrow", ({override, mock_templ// Narrow to a group direct message with four users.override(page_params, "user_id", anna.user_id);const group = [anna.user_id, vronsky.user_id, levin.user_id, kitty.user_id];+ const conversation_key = typing_data.get_direct_message_conversation_key(group);const group_emails = `${anna.email},${vronsky.email},${levin.email},${kitty.email}`;narrow_state.set_current_filter(new Filter([{operator: "dm", operand: group_emails}]));@@ -84,8 +85,8 @@ run_test("typing_events.render_notifications_for_narrow", ({override, mock_templ// MAX_USERS_TO_DISPLAY_NAME) or 'Several people are typing…'// For now, set two of the users as being typists.- typing_data.add_typist(group, anna.user_id);- typing_data.add_typist(group, vronsky.user_id);+ typing_data.add_typist(conversation_key, anna.user_id);+ typing_data.add_typist(conversation_key, vronsky.user_id);const two_typing_users_rendered_html = "Two typing users rendered html stub";@@ -131,8 +132,8 @@ run_test("typing_events.render_notifications_for_narrow", ({override, mock_templ// Change to having four typists and verify the rendered html has// 'Several people are typing…' but not the list of users.- typing_data.add_typist(group, levin.user_id);- typing_data.add_typist(group, kitty.user_id);+ typing_data.add_typist(conversation_key, levin.user_id);+ typing_data.add_typist(conversation_key, kitty.user_id);typing_events.render_notifications_for_narrow();assert.ok($typing_notifications.html().includes("Several people are typing…"));diff --git a/web/tests/typing_data.test.js b/web/tests/typing_data.test.jsindex 707cce84444..38bc2ee3b3e 100644--- a/web/tests/typing_data.test.js+++ b/web/tests/typing_data.test.js@@ -20,49 +20,57 @@ test("basics", () => {// The typing_data needs to be robust with lists of// user ids being in arbitrary sorting order. So all// the apparent randomness in these tests has a purpose.- typing_data.add_typist([5, 10, 15], 15);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);assert.deepEqual(typing_data.get_group_typists([15, 10, 5]), [15]);// test that you can add twice- typing_data.add_typist([5, 10, 15], 15);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);// add another id to our first group- typing_data.add_typist([5, 10, 15], 10);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 10);assert.deepEqual(typing_data.get_group_typists([10, 15, 5]), [10, 15]);// start adding to a new group- typing_data.add_typist([7, 15], 7);- typing_data.add_typist([7, 15], 15);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 7);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 15);// test get_all_direct_message_typistsassert.deepEqual(typing_data.get_all_direct_message_typists(), [7, 10, 15]);// test basic removal- assert.ok(typing_data.remove_typist([15, 7], 7));+ assert.ok(+ typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 7),+ );assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);// test removing an id that is not there- assert.ok(!typing_data.remove_typist([15, 7], 7));+ assert.ok(+ !typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 7),+ );assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);assert.deepEqual(typing_data.get_all_direct_message_typists(), [10, 15]);// remove user from one group, but "15" will still be among// "all typists"- assert.ok(typing_data.remove_typist([15, 7], 15));+ assert.ok(+ typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 15),+ );assert.deepEqual(typing_data.get_all_direct_message_typists(), [10, 15]);// now remove from the other group- assert.ok(typing_data.remove_typist([5, 15, 10], 15));+ assert.ok(+ typing_data.remove_typist(typing_data.get_direct_message_conversation_key([5, 15, 10]), 15),+ );assert.deepEqual(typing_data.get_all_direct_message_typists(), [10]);// test duplicate ids in a groups- typing_data.add_typist([20, 40, 20], 20);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([20, 40, 20]), 20);assert.deepEqual(typing_data.get_group_typists([20, 40]), [20]);});test("muted_typists_excluded", () => {- typing_data.add_typist([5, 10, 15], 5);- typing_data.add_typist([5, 10, 15], 10);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 5);+ typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 10);// Nobody is muted.assert.deepEqual(typing_data.get_group_typists([5, 10, 15]), [5, 10]);@@ -102,12 +110,18 @@ test("timers", () => {function kickstart() {reset_events();- typing_data.kickstart_inbound_timer(stub_group, stub_delay, stub_f);+ typing_data.kickstart_inbound_timer(+ typing_data.get_direct_message_conversation_key(stub_group),+ stub_delay,+ stub_f,+ );}function clear() {reset_events();- typing_data.clear_inbound_timer(stub_group);+ typing_data.clear_inbound_timer(+ typing_data.get_direct_message_conversation_key(stub_group),+ );}set_global("setTimeout", set_timeout);diff --git a/web/tests/typing_events.test.js b/web/tests/typing_events.test.jsindex ec96743a896..ddb315571d3 100644--- a/web/tests/typing_events.test.js+++ b/web/tests/typing_events.test.js@@ -45,6 +45,7 @@ people.add_active_user(kitty);run_test("render_notifications_for_narrow", ({override, mock_template}) => {override(page_params, "user_id", anna.user_id);const group = [anna.user_id, vronsky.user_id, levin.user_id, kitty.user_id];+ const conversation_key = typing_data.get_direct_message_conversation_key(group);const group_emails = `${anna.email},${vronsky.email},${levin.email},${kitty.email}`;narrow_state.set_current_filter(new Filter([{operator: "dm", operand: group_emails}]));@@ -54,8 +55,8 @@ run_test("render_notifications_for_narrow", ({override, mock_template}) => {// Having only two(<MAX_USERS_TO_DISPLAY_NAME) typists, both of them// should be rendered but not 'Several people are typing…'- typing_data.add_typist(group, anna.user_id);- typing_data.add_typist(group, vronsky.user_id);+ typing_data.add_typist(conversation_key, anna.user_id);+ typing_data.add_typist(conversation_key, vronsky.user_id);typing_events.render_notifications_for_narrow();assert.ok($typing_notifications.visible());assert.ok($typing_notifications.html().includes(`${anna.full_name} is typing…`));@@ -63,7 +64,7 @@ run_test("render_notifications_for_narrow", ({override, mock_template}) => {assert.ok(!$typing_notifications.html().includes("Several people are typing…"));// Having 3(=MAX_USERS_TO_DISPLAY_NAME) typists should also display only names- typing_data.add_typist(group, levin.user_id);+ typing_data.add_typist(conversation_key, levin.user_id);typing_events.render_notifications_for_narrow();assert.ok($typing_notifications.visible());assert.ok($typing_notifications.html().includes(`${anna.full_name} is typing…`));@@ -72,7 +73,7 @@ run_test("render_notifications_for_narrow", ({override, mock_template}) => {assert.ok(!$typing_notifications.html().includes("Several people are typing…"));// Having 4(>MAX_USERS_TO_DISPLAY_NAME) typists should display "Several people are typing…"- typing_data.add_typist(group, kitty.user_id);+ typing_data.add_typist(conversation_key, kitty.user_id);typing_events.render_notifications_for_narrow();assert.ok($typing_notifications.visible());assert.ok($typing_notifications.html().includes("Several people are typing…"));@@ -82,10 +83,10 @@ run_test("render_notifications_for_narrow", ({override, mock_template}) => {assert.ok(!$typing_notifications.html().includes(`${kitty.full_name} is typing…`));// #typing_notifications should be hidden when there are no typists.- typing_data.remove_typist(group, anna.user_id);- typing_data.remove_typist(group, vronsky.user_id);- typing_data.remove_typist(group, levin.user_id);- typing_data.remove_typist(group, kitty.user_id);+ typing_data.remove_typist(conversation_key, anna.user_id);+ typing_data.remove_typist(conversation_key, vronsky.user_id);+ typing_data.remove_typist(conversation_key, levin.user_id);+ typing_data.remove_typist(conversation_key, kitty.user_id);typing_events.render_notifications_for_narrow();assert.ok(!$typing_notifications.visible());});