Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_events: refactor the peer add/remove code path.
Palash Baderia45569539c5c7
diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.jsindex fd679ebe768..214207db367 100644--- a/web/src/server_events_dispatch.js+++ b/web/src/server_events_dispatch.js@@ -9,7 +9,6 @@ import * as bot_data from "./bot_data";import * as browser_history from "./browser_history";import {buddy_list} from "./buddy_list";import * as compose from "./compose";-import * as compose_fade from "./compose_fade";import * as compose_pm_pill from "./compose_pm_pill";import * as compose_recipient from "./compose_recipient";import * as composebox_typeahead from "./composebox_typeahead";@@ -590,13 +589,7 @@ export function dispatch_normal_event(event) {const user_ids = people.validate_user_ids(event.user_ids);peer_data.bulk_add_subscribers({stream_ids, user_ids});-- for (const stream_id of stream_ids) {- const sub = sub_store.get(stream_id);- stream_settings_ui.update_subscribers_ui(sub);- }-- compose_fade.update_faded_users();+ stream_events.process_subscriber_update(stream_ids);break;}case "peer_remove": {@@ -604,13 +597,7 @@ export function dispatch_normal_event(event) {const user_ids = people.validate_user_ids(event.user_ids);peer_data.bulk_remove_subscribers({stream_ids, user_ids});-- for (const stream_id of stream_ids) {- const sub = sub_store.get(stream_id);- stream_settings_ui.update_subscribers_ui(sub);- }-- compose_fade.update_faded_users();+ stream_events.process_subscriber_update(stream_ids);break;}case "remove":diff --git a/web/src/stream_events.js b/web/src/stream_events.jsindex 91aca7522d1..a72e7d99dc9 100644--- a/web/src/stream_events.js+++ b/web/src/stream_events.js@@ -2,6 +2,7 @@ import $ from "jquery";import * as blueslip from "./blueslip";import * as color_data from "./color_data";+import * as compose_fade from "./compose_fade";import * as compose_recipient from "./compose_recipient";import * as message_lists from "./message_lists";import * as message_view_header from "./message_view_header";@@ -197,3 +198,11 @@ export function remove_deactivated_user_from_all_streams(user_id) {}}}++export function process_subscriber_update(stream_ids) {+ for (const stream_id of stream_ids) {+ const sub = sub_store.get(stream_id);+ stream_settings_ui.update_subscribers_ui(sub);+ }+ compose_fade.update_faded_users();+}diff --git a/web/tests/dispatch_subs.test.js b/web/tests/dispatch_subs.test.jsindex b155ea96bb2..2fa0e05c8e7 100644--- a/web/tests/dispatch_subs.test.js+++ b/web/tests/dispatch_subs.test.js@@ -12,7 +12,6 @@ const {page_params} = require("./lib/zpage_params");const event_fixtures = events.fixtures;const test_user = events.test_user;-const compose_fade = mock_esm("../src/compose_fade");const message_lists = mock_esm("../src/message_lists");const narrow_state = mock_esm("../src/narrow_state");const overlays = mock_esm("../src/overlays");@@ -80,22 +79,17 @@ test("peer add/remove", ({override}) => {stream_id: event.stream_ids[0],});- const subs_stub = make_stub();- override(stream_settings_ui, "update_subscribers_ui", subs_stub.f);-- const compose_fade_stub = make_stub();- override(compose_fade, "update_faded_users", compose_fade_stub.f);+ const stream_stub = make_stub();+ override(stream_events, "process_subscriber_update", stream_stub.f);dispatch(event);- assert.equal(compose_fade_stub.num_calls, 1);- assert.equal(subs_stub.num_calls, 1);+ assert.equal(stream_stub.num_calls, 1);assert.ok(peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));event = event_fixtures.subscription__peer_remove;dispatch(event);- assert.equal(compose_fade_stub.num_calls, 2);- assert.equal(subs_stub.num_calls, 2);+ assert.equal(stream_stub.num_calls, 2);assert.ok(!peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));});@@ -143,7 +137,7 @@ test("add error handling", () => {});test("peer event error handling (bad stream_ids/user_ids)", ({override}) => {- override(compose_fade, "update_faded_users", () => {});+ override(stream_events, "process_subscriber_update", () => {});const add_event = {type: "subscription",diff --git a/web/tests/stream_events.test.js b/web/tests/stream_events.test.jsindex 3b9d8e3daff..3e2e1bc2082 100644--- a/web/tests/stream_events.test.js+++ b/web/tests/stream_events.test.js@@ -42,6 +42,7 @@ const people = zrequire("people");const stream_data = zrequire("stream_data");const stream_events = zrequire("stream_events");const compose_recipient = zrequire("compose_recipient");+const compose_fade = zrequire("compose_fade");const george = {email: "[email protected]",@@ -442,3 +443,23 @@ test("remove_deactivated_user_from_all_streams", () => {// verify that we issue a call to update subscriber count/list UIassert.equal(subs_stub.num_calls, 1);});++test("process_subscriber_update", () => {+ const subsStub = make_stub();+ stream_settings_ui.update_subscribers_ui = subsStub.f;++ const fadedUsersStub = make_stub();+ compose_fade.update_faded_users = fadedUsersStub.f;++ // Sample stream IDs+ const streamIds = [1, 2, 3];++ // Call the function being tested+ stream_events.process_subscriber_update(streamIds);++ // Assert that update_subscribers_ui is called for each stream ID+ assert.equal(subsStub.num_calls, streamIds.length);++ // Assert that update_faded_users is called once+ assert.equal(fadedUsersStub.num_calls, 1);+});