Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
actions: Modify gather_subscriptions() to also gather web-public streams.
Clara Dantas6a1d9480bc6e
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex a0bbe2bb03f..65ca306a853 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -4881,6 +4881,7 @@ def gather_subscriptions_helper(user_profile: UserProfile,*Stream.API_FIELDS,# The realm_id and recipient_id are generally not needed in the API."realm_id",+ "is_web_public","recipient_id",# email_token isn't public to some users with access to# the stream, so doesn't belong in API_FIELDS.@@ -4888,9 +4889,15 @@ def gather_subscriptions_helper(user_profile: UserProfile,stream_dicts = [stream for stream in all_streams if stream['id'] in stream_ids]stream_hash = {}+ web_public_stream_ids = [stream['id'] for stream in all_streams if stream['is_web_public']]for stream in stream_dicts:stream_hash[stream["id"]] = stream+ for sub in sub_dicts:+ stream = stream_hash.get(sub["stream_id"])+ if stream:+ sub["is_web_public"] = stream.get("is_web_public", False)+all_streams_id = [stream["id"] for stream in all_streams]subscribed = []@@ -4963,8 +4970,9 @@ def gather_subscriptions_helper(user_profile: UserProfile,# and this user isn't on it anymore (or a realm administrator).if stream["invite_only"] and not (sub["active"] or user_profile.is_realm_admin):subscribers = None- # Guest users lose access to subscribers when they are unsubscribed.- if not sub["active"] and user_profile.is_guest:+ # Guest users lose access to subscribers when they are unsubscribed if the stream+ # is not web-public.+ if not sub["active"] and user_profile.is_guest and not sub["is_web_public"]:subscribers = Noneif subscribers is not None:stream_dict['subscribers'] = subscribers@@ -4980,7 +4988,7 @@ def gather_subscriptions_helper(user_profile: UserProfile,if user_profile.can_access_public_streams():never_subscribed_stream_ids = all_streams_id_set - sub_unsub_stream_idselse:- never_subscribed_stream_ids = set()+ never_subscribed_stream_ids = set(web_public_stream_ids) - sub_unsub_stream_idsnever_subscribed_streams = [ns_stream_dict for ns_stream_dict in all_streamsif ns_stream_dict['id'] in never_subscribed_stream_ids]diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex a823314865a..edf3fdcf161 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -3854,7 +3854,8 @@ class GetSubscribersTest(ZulipTestCase):def test_never_subscribed_streams(self) -> None:"""- Check never_subscribed streams are fetched correctly and not include invite_only streams.+ Check never_subscribed streams are fetched correctly and not include invite_only streams,+ or invite_only and public streams to guest users."""realm = get_realm("zulip")users_to_subscribe = [@@ -3875,6 +3876,11 @@ class GetSubscribersTest(ZulipTestCase):'test_stream_invite_only_2',]+ web_public_streams = [+ 'test_stream_web_public_1',+ 'test_stream_web_public_2',+ ]+def create_public_streams() -> None:for stream_name in public_streams:self.make_stream(stream_name, realm=realm)@@ -3887,6 +3893,19 @@ class GetSubscribersTest(ZulipTestCase):create_public_streams()+ def create_web_public_streams() -> None:+ for stream_name in web_public_streams:+ self.make_stream(stream_name, realm=realm, is_web_public=True)++ ret = self.common_subscribe_to_streams(+ self.user_profile,+ web_public_streams,+ dict(principals=ujson.dumps(users_to_subscribe))+ )+ self.assert_json_success(ret)++ create_web_public_streams()+def create_private_streams() -> None:self.common_subscribe_to_streams(self.user_profile,@@ -3913,7 +3932,7 @@ class GetSubscribersTest(ZulipTestCase):never_subscribed = get_never_subscribed()# Invite only stream should not be there in never_subscribed streams- self.assertEqual(len(never_subscribed), len(public_streams))+ self.assertEqual(len(never_subscribed), len(public_streams) + len(web_public_streams))for stream_dict in never_subscribed:name = stream_dict['name']self.assertFalse('invite_only' in name)@@ -3927,13 +3946,29 @@ class GetSubscribersTest(ZulipTestCase):self.assertEqual(len(never_subscribed),- len(public_streams) + len(private_streams),+ len(public_streams) + len(private_streams) + len(web_public_streams),)for stream_dict in never_subscribed:self.assertTrue(len(stream_dict["subscribers"]) == len(users_to_subscribe))test_admin_case()+ def test_guest_user_case() -> None:+ self.user_profile.role = UserProfile.ROLE_GUEST+ sub, unsub, never_sub = gather_subscriptions_helper(self.user_profile)++ # It's +1 because of the stream Rome.+ self.assertEqual(len(never_sub), len(web_public_streams) + 1)+ sub_ids = list(map(lambda stream: stream["stream_id"], sub))+ unsub_ids = list(map(lambda stream: stream["stream_id"], unsub))++ for stream_dict in never_sub:+ self.assertTrue(stream_dict["is_web_public"])+ self.assertTrue(stream_dict["stream_id"] not in sub_ids)+ self.assertTrue(stream_dict["stream_id"] not in unsub_ids)++ test_guest_user_case()+def test_gather_subscribed_streams_for_guest_user(self) -> None:guest_user = self.example_user("polonius")@@ -3964,29 +3999,29 @@ class GetSubscribersTest(ZulipTestCase):self.assertEqual(len(sub["subscribers"]), 2)self.assertTrue(expected_stream_exists)- # Guest users don't get info about unsubscribed public stream's subscribers- expected_stream_exists = False- for unsub in unsubs:- if unsub["name"] == stream_name_unsub:- expected_stream_exists = True- self.assertNotIn("subscribers", unsub)- self.assertTrue(expected_stream_exists)+ # Guest user only get data about never subscribed streams if they're+ # web-public.+ for stream in neversubs:+ self.assertTrue(stream['is_web_public'])- # Guest user don't get data about never subscribed public stream's data- self.assertEqual(len(neversubs), 0)+ # Guest user only get data about never subscribed web-public streams+ self.assertEqual(len(neversubs), 1)def test_previously_subscribed_private_streams(self) -> None:admin_user = self.example_user("iago")non_admin_user = self.example_user("cordelia")+ guest_user = self.example_user("polonius")stream_name = "private_stream"self.make_stream(stream_name, realm=get_realm("zulip"), invite_only=True)self.subscribe(admin_user, stream_name)self.subscribe(non_admin_user, stream_name)+ self.subscribe(guest_user, stream_name)self.subscribe(self.example_user("othello"), stream_name)self.unsubscribe(admin_user, stream_name)self.unsubscribe(non_admin_user, stream_name)+ self.unsubscribe(guest_user, stream_name)# Test admin user gets previously subscribed private stream's subscribers.sub_data = gather_subscriptions_helper(admin_user)@@ -4000,6 +4035,11 @@ class GetSubscribersTest(ZulipTestCase):self.assertEqual(len(unsubscribed_streams), 1)self.assertFalse('subscribers' in unsubscribed_streams[0])+ sub_data = gather_subscriptions_helper(guest_user)+ unsubscribed_streams = sub_data[1]+ self.assertEqual(len(unsubscribed_streams), 1)+ self.assertFalse('subscribers' in unsubscribed_streams[0])+def test_gather_subscriptions_mit(self) -> None:"""gather_subscriptions returns correct results with only 3 queriesdiff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 234134d6fc0..d01808d78df 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1147,9 +1147,10 @@ class UserProfileTest(ZulipTestCase):polonius = self.example_user("polonius")self.login('polonius')self.assertTrue(polonius.is_guest)+ self.assertTrue(stream.is_web_public)- result = self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}")- self.assert_json_error(result, "Invalid stream id")+ result = ujson.loads(self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}").content)+ self.assertTrue(result['is_subscribed'])class ActivateTest(ZulipTestCase):def test_basics(self) -> None: