refactor: Extract send_peer_subscriber_events.

We now use the same basic code to send peer_add
and peer_remove events.
This commit is contained in:
Steve Howell 2020-10-19 20:30:27 +00:00 committed by Tim Abbott
parent 2dc6d26ec6
commit 3961e69381
1 changed files with 32 additions and 31 deletions

View File

@ -2827,15 +2827,15 @@ def bulk_add_subscriptions(
subs_to_activate=subs_to_activate, subs_to_activate=subs_to_activate,
) )
new_stream_user_ids: Dict[int, Set[int]] = defaultdict(set) altered_user_dict: Dict[int, Set[int]] = defaultdict(set)
for sub_info in subs_to_add + subs_to_activate: for sub_info in subs_to_add + subs_to_activate:
new_stream_user_ids[sub_info.stream.id].add(sub_info.user.id) altered_user_dict[sub_info.stream.id].add(sub_info.user.id)
stream_dict = {stream.id: stream for stream in streams} stream_dict = {stream.id: stream for stream in streams}
new_streams = [ new_streams = [
stream_dict[stream_id] stream_dict[stream_id]
for stream_id in new_stream_user_ids for stream_id in altered_user_dict
] ]
subscriber_peer_info = bulk_get_subscriber_peer_info( subscriber_peer_info = bulk_get_subscriber_peer_info(
@ -2849,7 +2849,7 @@ def bulk_add_subscriptions(
send_stream_creation_events_for_private_streams( send_stream_creation_events_for_private_streams(
realm=realm, realm=realm,
stream_dict=stream_dict, stream_dict=stream_dict,
new_stream_user_ids=new_stream_user_ids, altered_user_dict=altered_user_dict,
) )
send_subscription_add_events( send_subscription_add_events(
@ -2858,9 +2858,10 @@ def bulk_add_subscriptions(
subscriber_dict=subscriber_peer_info.subscribed_ids, subscriber_dict=subscriber_peer_info.subscribed_ids,
) )
send_peer_add_events( send_peer_subscriber_events(
op="peer_add",
realm=realm, realm=realm,
new_stream_user_ids=new_stream_user_ids, altered_user_dict=altered_user_dict,
stream_dict=stream_dict, stream_dict=stream_dict,
peer_id_dict=subscriber_peer_info.peer_ids, peer_id_dict=subscriber_peer_info.peer_ids,
) )
@ -2913,9 +2914,9 @@ def bulk_add_subs_to_db_with_logging(
def send_stream_creation_events_for_private_streams( def send_stream_creation_events_for_private_streams(
realm: Realm, realm: Realm,
stream_dict: Dict[int, Stream], stream_dict: Dict[int, Stream],
new_stream_user_ids: Dict[int, Set[int]], altered_user_dict: Dict[int, Set[int]],
) -> None: ) -> None:
for stream_id, stream_users_ids in new_stream_user_ids.items(): for stream_id, stream_users_ids in altered_user_dict.items():
stream = stream_dict[stream_id] stream = stream_dict[stream_id]
if not stream.is_public(): if not stream.is_public():
@ -2931,16 +2932,20 @@ def send_stream_creation_events_for_private_streams(
if notify_user_ids: if notify_user_ids:
send_stream_creation_event(stream, notify_user_ids) send_stream_creation_event(stream, notify_user_ids)
def send_peer_add_events( def send_peer_subscriber_events(
op: str,
realm: Realm, realm: Realm,
stream_dict: Dict[int, Stream], stream_dict: Dict[int, Stream],
new_stream_user_ids: Dict[int, Set[int]], altered_user_dict: Dict[int, Set[int]],
peer_id_dict: Dict[int, Set[int]], peer_id_dict: Dict[int, Set[int]],
) -> None: ) -> None:
# Send peer_add events to other users who are tracking the # Send peer_add/peer_remove events to other users who are tracking the
# subscribers lists of streams in their browser; everyone for # subscribers lists of streams in their browser; everyone for
# public streams and only existing subscribers for private streams. # public streams and only existing subscribers for private streams.
for stream_id, altered_user_ids in new_stream_user_ids.items():
assert op in ["peer_add", "peer_remove"]
for stream_id, altered_user_ids in altered_user_dict.items():
stream = stream_dict[stream_id] stream = stream_dict[stream_id]
if stream.is_in_zephyr_realm and not stream.invite_only: if stream.is_in_zephyr_realm and not stream.invite_only:
@ -2950,9 +2955,12 @@ def send_peer_add_events(
if peer_user_ids: if peer_user_ids:
for new_user_id in altered_user_ids: for new_user_id in altered_user_ids:
event = dict(type="subscription", op="peer_add", event = dict(
stream_id=stream.id, type="subscription",
user_id=new_user_id) op=op,
stream_id=stream.id,
user_id=new_user_id,
)
send_event(realm, event, peer_user_ids) send_event(realm, event, peer_user_ids)
def send_peer_remove_events( def send_peer_remove_events(
@ -2960,26 +2968,19 @@ def send_peer_remove_events(
streams: List[Stream], streams: List[Stream],
altered_user_dict: Dict[int, Set[int]], altered_user_dict: Dict[int, Set[int]],
) -> None: ) -> None:
peer_dict = bulk_get_peers( peer_id_dict = bulk_get_peers(
realm=realm, realm=realm,
streams=streams, streams=streams,
) )
for stream in streams: stream_dict = {stream.id: stream for stream in streams}
if stream.is_in_zephyr_realm and not stream.invite_only:
continue
altered_user_ids = altered_user_dict[stream.id] send_peer_subscriber_events(
peer_user_ids = list(peer_dict[stream.id] - altered_user_ids) op="peer_remove",
realm=realm,
if peer_user_ids: stream_dict=stream_dict,
for removed_user_id in altered_user_ids: altered_user_dict=altered_user_dict,
event = dict( peer_id_dict=peer_id_dict,
type="subscription", )
op="peer_remove",
stream_id=stream.id,
user_id=removed_user_id,
)
send_event(realm, event, peer_user_ids)
def get_available_notification_sounds() -> List[str]: def get_available_notification_sounds() -> List[str]:
notification_sounds_path = static_path('audio/notification_sounds') notification_sounds_path = static_path('audio/notification_sounds')