From ddbfbdc3866664ad9c12eed6a81d708939bb57f4 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Sun, 21 Jun 2020 01:05:41 +0530 Subject: [PATCH] stream: Modify stream_data.add_subscriber to use stream_id. Thsi commit changes stream_data.add_subscriber to use stream_id instead of stream name. We are using stream ids so that we can avoid bugs related to live update after stream rename. --- frontend_tests/node_tests/stream_data.js | 18 +++++++++--------- frontend_tests/node_tests/stream_events.js | 2 +- frontend_tests/node_tests/typeahead_helper.js | 6 +++--- static/js/server_events_dispatch.js | 2 +- static/js/stream_data.js | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/frontend_tests/node_tests/stream_data.js b/frontend_tests/node_tests/stream_data.js index d22a4e493b..86f4a9eb2e 100644 --- a/frontend_tests/node_tests/stream_data.js +++ b/frontend_tests/node_tests/stream_data.js @@ -225,7 +225,7 @@ run_test('subscribers', () => { assert(!stream_data.is_user_subscribed('Rome', brutus.user_id)); // add - let ok = stream_data.add_subscriber('Rome', brutus.user_id); + let ok = stream_data.add_subscriber(sub.stream_id, brutus.user_id); assert(ok); assert(stream_data.is_user_subscribed('Rome', brutus.user_id)); sub = stream_data.get_sub('Rome'); @@ -236,7 +236,7 @@ run_test('subscribers', () => { assert.equal(sub.email_address, sub_email); // verify that adding an already-added subscriber is a noop - stream_data.add_subscriber('Rome', brutus.user_id); + stream_data.add_subscriber(sub.stream_id, brutus.user_id); assert(stream_data.is_user_subscribed('Rome', brutus.user_id)); sub = stream_data.get_sub('Rome'); stream_data.update_subscribers_count(sub); @@ -274,19 +274,19 @@ run_test('subscribers', () => { // can be undefined. stream_data.set_subscribers(sub); stream_data.add_sub(sub); - stream_data.add_subscriber('Rome', brutus.user_id); + stream_data.add_subscriber(sub.stream_id, brutus.user_id); sub.subscribed = true; assert(stream_data.is_user_subscribed('Rome', brutus.user_id)); // Verify that we noop and don't crash when unsubscribed. sub.subscribed = false; stream_data.update_calculated_fields(sub); - ok = stream_data.add_subscriber('Rome', brutus.user_id); + ok = stream_data.add_subscriber(sub.stream_id, brutus.user_id); assert(ok); assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true); stream_data.remove_subscriber('Rome', brutus.user_id); assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), false); - stream_data.add_subscriber('Rome', brutus.user_id); + stream_data.add_subscriber(sub.stream_id, brutus.user_id); assert.equal(stream_data.is_user_subscribed('Rome', brutus.user_id), true); blueslip.expect( @@ -302,13 +302,13 @@ run_test('subscribers', () => { blueslip.expect( 'warn', 'We got an add_subscriber call for a non-existent stream.'); - ok = stream_data.add_subscriber('UNKNOWN', brutus.user_id); + ok = stream_data.add_subscriber(9999999, brutus.user_id); assert(!ok); // Verify that we don't crash and return false for a bad user id. blueslip.expect('error', 'Unknown user_id in get_by_user_id: 9999999'); blueslip.expect('error', 'We tried to add invalid subscriber: 9999999'); - ok = stream_data.add_subscriber('Rome', 9999999); + ok = stream_data.add_subscriber(sub.stream_id, 9999999); assert(!ok); }); @@ -613,7 +613,7 @@ run_test('get_subscriber_count', () => { user_id: 101, }; people.add_active_user(fred); - stream_data.add_subscriber('India', 102); + stream_data.add_subscriber(india.stream_id, 102); assert.equal(stream_data.get_subscriber_count('India'), 1); const george = { email: 'george@zulip.com', @@ -621,7 +621,7 @@ run_test('get_subscriber_count', () => { user_id: 103, }; people.add_active_user(george); - stream_data.add_subscriber('India', 103); + stream_data.add_subscriber(india.stream_id, 103); assert.equal(stream_data.get_subscriber_count('India'), 2); const sub = stream_data.get_sub_by_name('India'); diff --git a/frontend_tests/node_tests/stream_events.js b/frontend_tests/node_tests/stream_events.js index 2bfa37c2bd..cdd8266f89 100644 --- a/frontend_tests/node_tests/stream_events.js +++ b/frontend_tests/node_tests/stream_events.js @@ -382,7 +382,7 @@ run_test('remove_deactivated_user_from_all_streams', () => { dev_help.can_access_subscribers = true; // verify that deactivating user should unsubscribe user from all streams - assert(stream_data.add_subscriber(dev_help.name, george.user_id)); + assert(stream_data.add_subscriber(dev_help.stream_id, george.user_id)); assert(dev_help.subscribers.has(george.user_id)); stream_events.remove_deactivated_user_from_all_streams(george.user_id); diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.js index 4fd73db9cd..0477b7c3c4 100644 --- a/frontend_tests/node_tests/typeahead_helper.js +++ b/frontend_tests/node_tests/typeahead_helper.js @@ -240,9 +240,9 @@ run_test('sort_recipients', () => { const subscriber_email_1 = "b_user_2@zulip.net"; const subscriber_email_2 = "b_user_3@zulip.net"; const subscriber_email_3 = "b_bot@example.com"; - stream_data.add_subscriber("Dev", people.get_user_id(subscriber_email_1)); - stream_data.add_subscriber("Dev", people.get_user_id(subscriber_email_2)); - stream_data.add_subscriber("Dev", people.get_user_id(subscriber_email_3)); + stream_data.add_subscriber(1, people.get_user_id(subscriber_email_1)); + stream_data.add_subscriber(1, people.get_user_id(subscriber_email_2)); + stream_data.add_subscriber(1, people.get_user_id(subscriber_email_3)); const dev_sub = stream_data.get_sub("Dev"); const linux_sub = stream_data.get_sub("Linux"); diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index df5e29b49f..8fb222faca 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -340,7 +340,7 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) { return; } - if (!stream_data.add_subscriber(sub.name, user_id)) { + if (!stream_data.add_subscriber(stream_id, user_id)) { blueslip.warn('Cannot process peer_add event'); return; } diff --git a/static/js/stream_data.js b/static/js/stream_data.js index 52609097ab..efeb34576c 100644 --- a/static/js/stream_data.js +++ b/static/js/stream_data.js @@ -157,7 +157,7 @@ exports.rename_sub = function (sub, new_name) { exports.subscribe_myself = function (sub) { const user_id = people.my_current_user_id(); - exports.add_subscriber(sub.name, user_id); + exports.add_subscriber(sub.stream_id, user_id); sub.subscribed = true; sub.newly_subscribed = true; stream_info.set_true(sub.name, sub); @@ -628,8 +628,8 @@ exports.set_subscribers = function (sub, user_ids) { sub.subscribers = new LazySet(user_ids || []); }; -exports.add_subscriber = function (stream_name, user_id) { - const sub = exports.get_sub(stream_name); +exports.add_subscriber = function (stream_id, user_id) { + const sub = exports.get_sub_by_id(stream_id); if (typeof sub === 'undefined') { blueslip.warn("We got an add_subscriber call for a non-existent stream."); return false;