def get_muted_stream_ids(user_profile: UserProfile) -> List[int]: rows = get_stream_subscriptions_for_user(user_profile).filter( active=True, is_muted=True, ).values('recipient__type_id') muted_stream_ids = [row['recipient__type_id'] for row in rows] return muted_stream_ids
def get_streams(self, user_profile: UserProfile) -> List[str]: """ Helper function to get the stream names for a user """ subs = get_stream_subscriptions_for_user(user_profile).filter( active=True, ) return [cast(str, get_display_recipient(sub.recipient)) for sub in subs]
def get_muted_stream_ids(user_profile): # type: (UserProfile) -> List[int] rows = get_stream_subscriptions_for_user(user_profile).filter( active=True, in_home_view=False, ).values('recipient__type_id') muted_stream_ids = [row['recipient__type_id'] for row in rows] return muted_stream_ids
def get_streams(self, email: str, realm: Realm) -> List[str]: """ Helper function to get the stream names for a user """ user_profile = get_user(email, realm) subs = get_stream_subscriptions_for_user(user_profile).filter( active=True, ) return [cast(str, get_display_recipient(sub.recipient)) for sub in subs]
def get_streams(self, email: Text, realm: Realm) -> List[Text]: """ Helper function to get the stream names for a user """ user_profile = get_user(email, realm) subs = get_stream_subscriptions_for_user(user_profile).filter( active=True, ) return [cast(Text, get_display_recipient(sub.recipient)) for sub in subs]
def get_inactive_recipient_ids(user_profile: UserProfile) -> List[int]: rows = get_stream_subscriptions_for_user(user_profile).filter( active=False, ).values( 'recipient_id', ) inactive_recipient_ids = [ row['recipient_id'] for row in rows] return inactive_recipient_ids
def get_inactive_recipient_ids(user_profile: UserProfile) -> List[int]: rows = get_stream_subscriptions_for_user(user_profile).filter( active=False, ).values( 'recipient_id' ) inactive_recipient_ids = [ row['recipient_id'] for row in rows] return inactive_recipient_ids
def get_muted_stream_ids(user_profile: UserProfile) -> List[int]: rows = get_stream_subscriptions_for_user(user_profile).filter( active=True, in_home_view=False, ).values( 'recipient__type_id' ) muted_stream_ids = [ row['recipient__type_id'] for row in rows] return muted_stream_ids
def gather_subscriptions_helper( user_profile: UserProfile, include_subscribers: bool = True, ) -> SubscriptionInfo: realm = user_profile.realm all_streams: QuerySet[RawStreamDict] = get_active_streams(realm).values( *Stream.API_FIELDS, # The realm_id and recipient_id are generally not needed in the API. "realm_id", "recipient_id", # email_token isn't public to some users with access to # the stream, so doesn't belong in API_FIELDS. "email_token", ) recip_id_to_stream_id: Dict[int, int] = { stream["recipient_id"]: stream["id"] for stream in all_streams } all_streams_map: Dict[int, RawStreamDict] = {stream["id"]: stream for stream in all_streams} sub_dicts_query: Iterable[RawSubscriptionDict] = ( get_stream_subscriptions_for_user(user_profile) .values( *Subscription.API_FIELDS, "recipient_id", "active", ) .order_by("recipient_id") ) # We only care about subscriptions for active streams. sub_dicts: List[RawSubscriptionDict] = [ sub_dict for sub_dict in sub_dicts_query if recip_id_to_stream_id.get(sub_dict["recipient_id"]) ] def get_stream_id(sub_dict: RawSubscriptionDict) -> int: return recip_id_to_stream_id[sub_dict["recipient_id"]] traffic_stream_ids = {get_stream_id(sub_dict) for sub_dict in sub_dicts} recent_traffic = get_streams_traffic(stream_ids=traffic_stream_ids) # Okay, now we finally get to populating our main results, which # will be these three lists. subscribed: List[SubscriptionStreamDict] = [] unsubscribed: List[SubscriptionStreamDict] = [] never_subscribed: List[NeverSubscribedStreamDict] = [] sub_unsub_stream_ids = set() for sub_dict in sub_dicts: stream_id = get_stream_id(sub_dict) sub_unsub_stream_ids.add(stream_id) raw_stream_dict = all_streams_map[stream_id] stream_dict = build_stream_dict_for_sub( user=user_profile, sub_dict=sub_dict, raw_stream_dict=raw_stream_dict, recent_traffic=recent_traffic, ) # is_active is represented in this structure by which list we include it in. is_active = sub_dict["active"] if is_active: subscribed.append(stream_dict) else: unsubscribed.append(stream_dict) if user_profile.can_access_public_streams(): never_subscribed_stream_ids = set(all_streams_map) - sub_unsub_stream_ids else: web_public_stream_ids = {stream["id"] for stream in all_streams if stream["is_web_public"]} never_subscribed_stream_ids = web_public_stream_ids - sub_unsub_stream_ids never_subscribed_streams = [ all_streams_map[stream_id] for stream_id in never_subscribed_stream_ids ] for raw_stream_dict in never_subscribed_streams: is_public = not raw_stream_dict["invite_only"] if is_public or user_profile.is_realm_admin: slim_stream_dict = build_stream_dict_for_never_sub( raw_stream_dict=raw_stream_dict, recent_traffic=recent_traffic ) never_subscribed.append(slim_stream_dict) if include_subscribers: # The highly optimized bulk_get_subscriber_user_ids wants to know which # streams we are subscribed to, for validation purposes, and it uses that # info to know if it's allowed to find OTHER subscribers. subscribed_stream_ids = { get_stream_id(sub_dict) for sub_dict in sub_dicts if sub_dict["active"] } subscriber_map = bulk_get_subscriber_user_ids( all_streams, user_profile, subscribed_stream_ids, ) for lst in [subscribed, unsubscribed]: for stream_dict in lst: assert isinstance(stream_dict["stream_id"], int) stream_id = stream_dict["stream_id"] stream_dict["subscribers"] = subscriber_map[stream_id] for slim_stream_dict in never_subscribed: assert isinstance(slim_stream_dict["stream_id"], int) stream_id = slim_stream_dict["stream_id"] slim_stream_dict["subscribers"] = subscriber_map[stream_id] subscribed.sort(key=lambda x: x["name"]) unsubscribed.sort(key=lambda x: x["name"]) never_subscribed.sort(key=lambda x: x["name"]) return SubscriptionInfo( subscriptions=subscribed, unsubscribed=unsubscribed, never_subscribed=never_subscribed, )