def bulk_get_private_peers( realm: Realm, private_streams: List[Stream], ) -> Dict[int, Set[int]]: if not private_streams: return {} for stream in private_streams: # Our caller should only pass us private streams. assert stream.invite_only peer_ids: Dict[int, Set[int]] = {} realm_admin_ids = {user.id for user in realm.get_admin_users_and_bots()} stream_ids = {stream.id for stream in private_streams} stream_user_ids = get_user_ids_for_streams(stream_ids) for stream in private_streams: # This is the same business rule as we use in # bulk_get_subscriber_peer_info. Realm admins can see all private # stream subscribers. subscribed_user_ids = stream_user_ids.get(stream.id, set()) peer_ids[stream.id] = subscribed_user_ids | realm_admin_ids return peer_ids
def bulk_get_peers( realm: Realm, streams: List[Stream], ) -> Dict[int, Set[int]]: # This is almost a subset of bulk_get_subscriber_peer_info, # with the nuance that we don't have to query subscribers # for public streams. (The other functions tries to save # a query hop.) peer_ids = {} private_stream_ids = {stream.id for stream in streams if stream.invite_only} public_stream_ids = {stream.id for stream in streams if not stream.invite_only} if private_stream_ids: realm_admin_ids = {user.id for user in realm.get_admin_users_and_bots()} stream_user_ids = get_user_ids_for_streams(private_stream_ids) for stream_id in private_stream_ids: subscribed_user_ids = stream_user_ids.get(stream_id, set()) peer_ids[stream_id] = subscribed_user_ids | realm_admin_ids if public_stream_ids: non_guests = active_non_guest_user_ids(realm.id) for stream_id in public_stream_ids: peer_ids[stream_id] = set(non_guests) return peer_ids
def bulk_get_subscriber_peer_info( realm: Realm, streams: List[Stream], ) -> SubscriberPeerInfo: """ Glossary: subscribed_ids: This shows the users who are actually subscribed to the stream, which we generally send to the person subscribing to the stream. peer_ids: These are the folks that need to know about a new subscriber. It's usually a superset of the subscribers. """ subscribed_ids = {} peer_ids = {} private_stream_ids = { stream.id for stream in streams if stream.invite_only } public_stream_ids = { stream.id for stream in streams if not stream.invite_only } stream_user_ids = get_user_ids_for_streams(private_stream_ids | public_stream_ids) if private_stream_ids: realm_admin_ids = { user.id for user in realm.get_admin_users_and_bots() } for stream_id in private_stream_ids: subscribed_user_ids = stream_user_ids.get(stream_id, set()) subscribed_ids[stream_id] = subscribed_user_ids peer_ids[stream_id] = subscribed_user_ids | realm_admin_ids if public_stream_ids: non_guests = active_non_guest_user_ids(realm.id) for stream_id in public_stream_ids: subscribed_user_ids = stream_user_ids.get(stream_id, set()) subscribed_ids[stream_id] = subscribed_user_ids peer_ids[stream_id] = set(non_guests) return SubscriberPeerInfo( subscribed_ids=subscribed_ids, peer_ids=peer_ids, )
def bulk_get_subscriber_peer_info( realm: Realm, streams: List[Stream], ) -> SubscriberPeerInfo: """ Glossary: subscribed_ids: This shows the users who are actually subscribed to the stream, which we generally send to the person subscribing to the stream. private_peer_dict: These are the folks that need to know about a new subscriber. It's usually a superset of the subscribers. Note that we only compute this for PRIVATE streams. We let other code handle peers for public streams, since the peers for all public streams are actually the same group of users, and downstream code can use that property of public streams to avoid extra work. """ subscribed_ids = {} private_peer_dict = {} private_stream_ids = {stream.id for stream in streams if stream.invite_only} public_stream_ids = {stream.id for stream in streams if not stream.invite_only} stream_user_ids = get_user_ids_for_streams(private_stream_ids | public_stream_ids) if private_stream_ids: realm_admin_ids = {user.id for user in realm.get_admin_users_and_bots()} for stream_id in private_stream_ids: # This is the same business rule as we use in # bulk_get_private_peers. Realm admins can see all private stream # subscribers. subscribed_user_ids = stream_user_ids.get(stream_id, set()) subscribed_ids[stream_id] = subscribed_user_ids private_peer_dict[stream_id] = subscribed_user_ids | realm_admin_ids for stream_id in public_stream_ids: subscribed_user_ids = stream_user_ids.get(stream_id, set()) subscribed_ids[stream_id] = subscribed_user_ids return SubscriberPeerInfo( subscribed_ids=subscribed_ids, private_peer_dict=private_peer_dict, )
def send_stream_creation_events_for_private_streams( realm: Realm, stream_dict: Dict[int, Stream], altered_user_dict: Dict[int, Set[int]], ) -> None: for stream_id, stream_users_ids in altered_user_dict.items(): stream = stream_dict[stream_id] if not stream.is_public(): # Users newly added to invite-only streams # need a `create` notification. The former, because # they need the stream to exist before # they get the "subscribe" notification, and the latter so # they can manage the new stream. # Realm admins already have all created private streams. realm_admin_ids = {user.id for user in realm.get_admin_users_and_bots()} notify_user_ids = list(stream_users_ids - realm_admin_ids) if notify_user_ids: send_stream_creation_event(stream, notify_user_ids)
def notify_invites_changed(realm: Realm) -> None: event = dict(type="invites_changed") admin_ids = [user.id for user in realm.get_admin_users_and_bots()] send_event(realm, event, admin_ids)