refactor: Move subscribers logic up to caller.

The gather_subscriptions_helper function now updates
subscribers instead of delegating.
This commit is contained in:
Steve Howell
2021-01-14 17:25:10 +00:00
committed by Tim Abbott
parent d9740045a5
commit e735ce3f01

View File

@@ -5022,7 +5022,6 @@ def build_stream_dict_for_sub(
user: UserProfile, user: UserProfile,
sub: Subscription, sub: Subscription,
stream: Stream, stream: Stream,
subscribers: Optional[List[int]],
recent_traffic: Dict[int, int], recent_traffic: Dict[int, int],
) -> Dict[str, object]: ) -> Dict[str, object]:
# We first construct a dictionary based on the standard Stream # We first construct a dictionary based on the standard Stream
@@ -5058,14 +5057,11 @@ def build_stream_dict_for_sub(
result["email_address"] = encode_email_address_helper( result["email_address"] = encode_email_address_helper(
stream["name"], stream["email_token"], show_sender=True) stream["name"], stream["email_token"], show_sender=True)
if subscribers is not None: # Our caller may add a subscribers field.
result["subscribers"] = subscribers
return result return result
def build_stream_dict_for_never_sub( def build_stream_dict_for_never_sub(
stream: Stream, stream: Stream,
subscribers: Optional[List[int]],
recent_traffic: Dict[int, int], recent_traffic: Dict[int, int],
) -> Dict[str, object]: ) -> Dict[str, object]:
result = {} result = {}
@@ -5084,9 +5080,7 @@ def build_stream_dict_for_never_sub(
# Backwards-compatibility addition of removed field. # Backwards-compatibility addition of removed field.
result["is_announcement_only"] = stream["stream_post_policy"] == Stream.STREAM_POST_POLICY_ADMINS result["is_announcement_only"] = stream["stream_post_policy"] == Stream.STREAM_POST_POLICY_ADMINS
if subscribers is not None: # Our caller may add a subscribers field.
result["subscribers"] = subscribers
return result return result
# In general, it's better to avoid using .values() because it makes # In general, it's better to avoid using .values() because it makes
@@ -5137,10 +5131,6 @@ def gather_subscriptions_helper(user_profile: UserProfile,
user_profile, user_profile,
subscribed_stream_ids, subscribed_stream_ids,
) )
else:
# If we're not including subscribers, always return None,
# which the below code needs to check for anyway.
subscriber_map = defaultdict(lambda: None)
# Okay, now we finally get to populating our main results, which # Okay, now we finally get to populating our main results, which
# will be these three lists. # will be these three lists.
@@ -5158,10 +5148,12 @@ def gather_subscriptions_helper(user_profile: UserProfile,
user=user_profile, user=user_profile,
sub=sub, sub=sub,
stream=stream, stream=stream,
subscribers=subscriber_map[stream_id],
recent_traffic=recent_traffic, recent_traffic=recent_traffic,
) )
if include_subscribers:
stream_dict['subscribers'] = subscriber_map[stream_id]
# is_active is represented in this structure by which list we include it in. # is_active is represented in this structure by which list we include it in.
is_active = sub["active"] is_active = sub["active"]
if is_active: if is_active:
@@ -5185,10 +5177,12 @@ def gather_subscriptions_helper(user_profile: UserProfile,
if is_public or user_profile.is_realm_admin: if is_public or user_profile.is_realm_admin:
stream_dict = build_stream_dict_for_never_sub( stream_dict = build_stream_dict_for_never_sub(
stream=stream, stream=stream,
subscribers=subscriber_map[stream["id"]],
recent_traffic=recent_traffic recent_traffic=recent_traffic
) )
if include_subscribers:
stream_dict['subscribers'] = subscriber_map[stream["id"]]
never_subscribed.append(stream_dict) never_subscribed.append(stream_dict)
return (sorted(subscribed, key=lambda x: x['name']), return (sorted(subscribed, key=lambda x: x['name']),