stream_events: refactor the peer add/remove code path.

This commit is a prep commit for implementing the live update of
user profile streams list. Since the code for both adding and
removing peers is the same after adding or removing the peer data,
it is better to extract that code to reduce duplication in a new
function called 'process_subscriber_update'.
This commit is contained in:
Palash Baderia 2022-10-11 17:50:27 +05:30 committed by Tim Abbott
parent bb6fe0385e
commit 45569539c5
4 changed files with 37 additions and 26 deletions

View File

@ -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":

View File

@ -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();
}

View File

@ -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",

View File

@ -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: "george@zulip.com",
@ -442,3 +443,23 @@ test("remove_deactivated_user_from_all_streams", () => {
// verify that we issue a call to update subscriber count/list UI
assert.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);
});