From 238bc386cb892c791e3c91c8292ed2cb7b3a76e0 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 2 Feb 2020 17:42:12 -0800 Subject: [PATCH] actions: Deduplicate parts of get_web_public_subs. This has the side of effect of making new fields we add to Stream be automatically included, which will help maintain this code as we upgrade it. This commit adds is_web_public, history_public_to_subscribers, and email_notifications fields to the dictionary. --- zerver/lib/actions.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index fab0b78b73..5fa3ba1501 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -4708,26 +4708,27 @@ def get_web_public_subs(realm: Realm) -> SubHelperT: color_idx = (color_idx + 1) % len(STREAM_ASSIGNMENT_COLORS) return color - subscribed = [ - {'name': stream.name, - 'is_muted': False, - 'invite_only': False, - 'is_announcement_only': stream.is_announcement_only, - 'color': get_next_color(), - 'desktop_notifications': True, - 'audible_notifications': True, - 'push_notifications': False, - 'pin_to_top': False, - 'stream_id': stream.id, - 'description': stream.description, - 'rendered_description': stream.rendered_description, - 'is_old_stream': is_old_stream(stream.date_created), - 'first_message_id': stream.first_message_id, - 'stream_weekly_traffic': get_average_weekly_stream_traffic(stream.id, - stream.date_created, - {}), - 'email_address': ''} - for stream in Stream.objects.filter(realm=realm, is_web_public=True, deactivated=False)] + subscribed = [] + for stream in Stream.objects.filter(realm=realm, is_web_public=True, deactivated=False): + stream_dict = stream.to_dict() + + # Add versions of the Subscription fields based on a simulated + # new user subscription set. + stream_dict['is_muted'] = False + stream_dict['color'] = get_next_color() + stream_dict['desktop_notifications'] = True + stream_dict['audible_notifications'] = True + stream_dict['push_notifications'] = True + stream_dict['email_notifications'] = True + stream_dict['pin_to_top'] = False + stream_dict['is_old_stream'] = is_old_stream(stream.date_created) + stream_weekly_traffic = get_average_weekly_stream_traffic(stream.id, + stream.date_created, + {}) + stream_dict['stream_weekly_traffic'] = stream_weekly_traffic + stream_dict['email_address'] = '' + subscribed.append(stream_dict) + return (subscribed, [], []) # In general, it's better to avoid using .values() because it makes