def gather_subscriptions_helper(user_profile: UserProfile, include_subscribers: bool=True) -> SubHelperT:
    sub_dicts = get_stream_subscriptions_for_user(user_profile).values(*Subscription.API_FIELDS, 'recipient_id').order_by('recipient_id')
    sub_dicts = list(sub_dicts)
    sub_recipient_ids = [sub['recipient_id'] for sub in sub_dicts]
    stream_recipient = StreamRecipientMap()
    stream_recipient.populate_for_recipient_ids(sub_recipient_ids)
    stream_ids: Set[int] = set()
    for sub in sub_dicts:
        sub['stream_id'] = stream_recipient.stream_id_for(sub['recipient_id'])
        stream_ids.add(sub['stream_id'])
    recent_traffic = get_streams_traffic(stream_ids=stream_ids)
    all_streams = get_active_streams(user_profile.realm).select_related('realm').values(*Stream.API_FIELDS, 'date_created', 'realm_id', 'recipient_id', 'email_token')
    stream_dicts = [stream for stream in all_streams if (stream['id'] in stream_ids)]
    stream_hash = {}
    for stream in stream_dicts:
        stream_hash[stream['id']] = stream
    all_streams_id = [stream['id'] for stream in all_streams]
    subscribed = []
    unsubscribed = []
    never_subscribed = []
    streams = [stream_hash[sub['stream_id']] for sub in sub_dicts if (sub['stream_id'] in stream_hash)]
    streams_subscribed_map = {sub['stream_id']: sub['active'] for sub in sub_dicts}
    streams_subscribed_map.update({stream['id']: False for stream in all_streams if (stream not in streams)})
    if include_subscribers:
        subscriber_map: Mapping[(int, Optional[List[int]])] = bulk_get_subscriber_user_ids(all_streams, user_profile, streams_subscribed_map, stream_recipient)
    else:
        subscriber_map = defaultdict((lambda : None))
    sub_unsub_stream_ids = set()
    for sub in sub_dicts:
        sub_unsub_stream_ids.add(sub['stream_id'])
        stream = stream_hash.get(sub['stream_id'])
        if (not stream):
            continue
        stream_dict = {}
        for field_name in Stream.API_FIELDS:
            if (field_name == 'id'):
                stream_dict['stream_id'] = stream['id']
                continue
            stream_dict[field_name] = stream[field_name]
        for field_name in Subscription.API_FIELDS:
            stream_dict[field_name] = sub[field_name]
        stream_dict['in_home_view'] = (not stream_dict['is_muted'])
        stream_dict['is_announcement_only'] = (stream['stream_post_policy'] == Stream.STREAM_POST_POLICY_ADMINS)
        stream_dict['stream_weekly_traffic'] = get_average_weekly_stream_traffic(stream['id'], stream['date_created'], recent_traffic)
        stream_dict['email_address'] = encode_email_address_helper(stream['name'], stream['email_token'], show_sender=True)
        subscribers: Optional[List[int]] = subscriber_map[stream['id']]
        if (stream['invite_only'] and (not (sub['active'] or user_profile.is_realm_admin))):
            subscribers = None
        if ((not sub['active']) and user_profile.is_guest):
            subscribers = None
        if (subscribers is not None):
            stream_dict['subscribers'] = subscribers
        is_active = stream_dict.pop('active')
        if is_active:
            subscribed.append(stream_dict)
        else:
            unsubscribed.append(stream_dict)
    all_streams_id_set = set(all_streams_id)
    if user_profile.can_access_public_streams():
        never_subscribed_stream_ids = (all_streams_id_set - sub_unsub_stream_ids)
    else:
        never_subscribed_stream_ids = set()
    never_subscribed_streams = [ns_stream_dict for ns_stream_dict in all_streams if (ns_stream_dict['id'] in never_subscribed_stream_ids)]
    for stream in never_subscribed_streams:
        is_public = (not stream['invite_only'])
        if (is_public or user_profile.is_realm_admin):
            stream_dict = {}
            for field_name in Stream.API_FIELDS:
                if (field_name == 'id'):
                    stream_dict['stream_id'] = stream['id']
                    continue
                stream_dict[field_name] = stream[field_name]
            stream_dict['stream_weekly_traffic'] = get_average_weekly_stream_traffic(stream['id'], stream['date_created'], recent_traffic)
            stream_dict['is_announcement_only'] = (stream['stream_post_policy'] == Stream.STREAM_POST_POLICY_ADMINS)
            if (is_public or user_profile.is_realm_admin):
                subscribers = subscriber_map[stream['id']]
                if (subscribers is not None):
                    stream_dict['subscribers'] = subscribers
            never_subscribed.append(stream_dict)
    return (sorted(subscribed, key=(lambda x: x['name'])), sorted(unsubscribed, key=(lambda x: x['name'])), sorted(never_subscribed, key=(lambda x: x['name'])))
def gather_subscriptions_helper(user_profile: UserProfile, include_subscribers: bool=True) -> SubHelperT:
    sub_dicts = get_stream_subscriptions_for_user(user_profile).values(*Subscription.API_FIELDS, 'recipient_id').order_by('recipient_id')
    sub_dicts = list(sub_dicts)
    sub_recipient_ids = [sub['recipient_id'] for sub in sub_dicts]
    stream_recipient = StreamRecipientMap()
    stream_recipient.populate_for_recipient_ids(sub_recipient_ids)
    stream_ids: Set[int] = set()
    for sub in sub_dicts:
        sub['stream_id'] = stream_recipient.stream_id_for(sub['recipient_id'])
        stream_ids.add(sub['stream_id'])
    recent_traffic = get_streams_traffic(stream_ids=stream_ids)
    all_streams = get_active_streams(user_profile.realm).select_related('realm').values(*Stream.API_FIELDS, 'date_created', 'realm_id', 'recipient_id', 'email_token')
    stream_dicts = [stream for stream in all_streams if (stream['id'] in stream_ids)]
    stream_hash = {}
    for stream in stream_dicts:
        stream_hash[stream['id']] = stream
    all_streams_id = [stream['id'] for stream in all_streams]
    subscribed = []
    unsubscribed = []
    never_subscribed = []
    streams = [stream_hash[sub['stream_id']] for sub in sub_dicts if (sub['stream_id'] in stream_hash)]
    streams_subscribed_map = {sub['stream_id']: sub['active'] for sub in sub_dicts}
    streams_subscribed_map.update({stream['id']: False for stream in all_streams if (stream not in streams)})
    if include_subscribers:
        subscriber_map: Mapping[(int, Optional[List[int]])] = bulk_get_subscriber_user_ids(all_streams, user_profile, streams_subscribed_map, stream_recipient)
    else:
        subscriber_map = defaultdict((lambda : None))
    sub_unsub_stream_ids = set()
    for sub in sub_dicts:
        sub_unsub_stream_ids.add(sub['stream_id'])
        stream = stream_hash.get(sub['stream_id'])
        if (not stream):
            continue
        stream_dict = {}
        for field_name in Stream.API_FIELDS:
            if (field_name == 'id'):
                stream_dict['stream_id'] = stream['id']
                continue
            stream_dict[field_name] = stream[field_name]
        for field_name in Subscription.API_FIELDS:
            stream_dict[field_name] = sub[field_name]
        stream_dict['in_home_view'] = (not stream_dict['is_muted'])
        stream_dict['is_announcement_only'] = (stream['stream_post_policy'] == Stream.STREAM_POST_POLICY_ADMINS)
        stream_dict['stream_weekly_traffic'] = get_average_weekly_stream_traffic(stream['id'], stream['date_created'], recent_traffic)
        stream_dict['email_address'] = encode_email_address_helper(stream['name'], stream['email_token'], show_sender=True)
        subscribers: Optional[List[int]] = subscriber_map[stream['id']]
        if (stream['invite_only'] and (not (sub['active'] or user_profile.is_realm_admin))):
            subscribers = None
        if ((not sub['active']) and user_profile.is_guest):
            subscribers = None
        if (subscribers is not None):
            stream_dict['subscribers'] = subscribers
        is_active = stream_dict.pop('active')
        if is_active:
            subscribed.append(stream_dict)
        else:
            unsubscribed.append(stream_dict)
    all_streams_id_set = set(all_streams_id)
    if user_profile.can_access_public_streams():
        never_subscribed_stream_ids = (all_streams_id_set - sub_unsub_stream_ids)
    else:
        never_subscribed_stream_ids = set()
    never_subscribed_streams = [ns_stream_dict for ns_stream_dict in all_streams if (ns_stream_dict['id'] in never_subscribed_stream_ids)]
    for stream in never_subscribed_streams:
        is_public = (not stream['invite_only'])
        if (is_public or user_profile.is_realm_admin):
            stream_dict = {}
            for field_name in Stream.API_FIELDS:
                if (field_name == 'id'):
                    stream_dict['stream_id'] = stream['id']
                    continue
                stream_dict[field_name] = stream[field_name]
            stream_dict['stream_weekly_traffic'] = get_average_weekly_stream_traffic(stream['id'], stream['date_created'], recent_traffic)
            stream_dict['is_announcement_only'] = (stream['stream_post_policy'] == Stream.STREAM_POST_POLICY_ADMINS)
            if (is_public or user_profile.is_realm_admin):
                subscribers = subscriber_map[stream['id']]
                if (subscribers is not None):
                    stream_dict['subscribers'] = subscribers
            never_subscribed.append(stream_dict)
    return (sorted(subscribed, key=(lambda x: x['name'])), sorted(unsubscribed, key=(lambda x: x['name'])), sorted(never_subscribed, key=(lambda x: x['name'])))