Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
actions: Split out zerver.actions.typing.
Anders Kaseorg62d3b5bfd511
diff --git a/zerver/actions/typing.py b/zerver/actions/typing.pynew file mode 100644index 00000000000..6d384d5c3d1--- /dev/null+++ b/zerver/actions/typing.py@@ -0,0 +1,84 @@+from typing import List++from django.utils.translation import gettext as _++from zerver.lib.exceptions import JsonableError+from zerver.lib.stream_subscription import get_user_ids_for_streams+from zerver.models import Realm, Stream, UserProfile, get_user_by_id_in_realm_including_cross_realm+from zerver.tornado.django_api import send_event+++def do_send_typing_notification(+ realm: Realm, sender: UserProfile, recipient_user_profiles: List[UserProfile], operator: str+) -> None:++ sender_dict = {"user_id": sender.id, "email": sender.email}++ # Include a list of recipients in the event body to help identify where the typing is happening+ recipient_dicts = [+ {"user_id": profile.id, "email": profile.email} for profile in recipient_user_profiles+ ]+ event = dict(+ type="typing",+ message_type="private",+ op=operator,+ sender=sender_dict,+ recipients=recipient_dicts,+ )++ # Only deliver the notification to active user recipients+ user_ids_to_notify = [user.id for user in recipient_user_profiles if user.is_active]++ send_event(realm, event, user_ids_to_notify)+++# check_send_typing_notification:+# Checks the typing notification and sends it+def check_send_typing_notification(sender: UserProfile, user_ids: List[int], operator: str) -> None:+ realm = sender.realm++ if sender.id not in user_ids:+ user_ids.append(sender.id)++ # If any of the user_ids being sent in are invalid, we will+ # just reject the whole request, since a partial list of user_ids+ # can create confusion related to huddles. Plus it's a good+ # sign that a client is confused (or possibly even malicious) if+ # we get bad user_ids.+ user_profiles = []+ for user_id in user_ids:+ try:+ # We include cross-bot realms as possible recipients,+ # so that clients can know which huddle conversation+ # is relevant here.+ user_profile = get_user_by_id_in_realm_including_cross_realm(user_id, sender.realm)+ except UserProfile.DoesNotExist:+ raise JsonableError(_("Invalid user ID {}").format(user_id))+ user_profiles.append(user_profile)++ do_send_typing_notification(+ realm=realm,+ sender=sender,+ recipient_user_profiles=user_profiles,+ operator=operator,+ )+++def do_send_stream_typing_notification(+ sender: UserProfile, operator: str, stream: Stream, topic: str+) -> None:++ sender_dict = {"user_id": sender.id, "email": sender.email}++ event = dict(+ type="typing",+ message_type="stream",+ op=operator,+ sender=sender_dict,+ stream_id=stream.id,+ topic=topic,+ )++ user_ids_to_notify = get_user_ids_for_streams({stream.id})[stream.id]++ send_event(sender.realm, event, user_ids_to_notify)diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex a2348cb6988..ba09ea482d7 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -144,7 +144,6 @@ from zerver.lib.stream_subscription import (get_subscribed_stream_ids_for_user,get_subscriptions_for_send_message,get_used_colors_for_user_ids,- get_user_ids_for_streams,num_subscribers_for_stream_id,subscriber_ids_with_stream_history_access,)@@ -282,7 +281,6 @@ from zerver.models import (get_stream_by_id_in_realm,get_system_bot,get_user_by_delivery_email,- get_user_by_id_in_realm_including_cross_realm,get_user_profile_by_id,is_cross_realm_bot_email,linkifiers_for_realm,@@ -2550,82 +2548,6 @@ def do_remove_reaction(notify_reaction_update(user_profile, message, reaction, "remove")-def do_send_typing_notification(- realm: Realm, sender: UserProfile, recipient_user_profiles: List[UserProfile], operator: str-) -> None:-- sender_dict = {"user_id": sender.id, "email": sender.email}-- # Include a list of recipients in the event body to help identify where the typing is happening- recipient_dicts = [- {"user_id": profile.id, "email": profile.email} for profile in recipient_user_profiles- ]- event = dict(- type="typing",- message_type="private",- op=operator,- sender=sender_dict,- recipients=recipient_dicts,- )-- # Only deliver the notification to active user recipients- user_ids_to_notify = [user.id for user in recipient_user_profiles if user.is_active]-- send_event(realm, event, user_ids_to_notify)---# check_send_typing_notification:-# Checks the typing notification and sends it-def check_send_typing_notification(sender: UserProfile, user_ids: List[int], operator: str) -> None:- realm = sender.realm-- if sender.id not in user_ids:- user_ids.append(sender.id)-- # If any of the user_ids being sent in are invalid, we will- # just reject the whole request, since a partial list of user_ids- # can create confusion related to huddles. Plus it's a good- # sign that a client is confused (or possibly even malicious) if- # we get bad user_ids.- user_profiles = []- for user_id in user_ids:- try:- # We include cross-bot realms as possible recipients,- # so that clients can know which huddle conversation- # is relevant here.- user_profile = get_user_by_id_in_realm_including_cross_realm(user_id, sender.realm)- except UserProfile.DoesNotExist:- raise JsonableError(_("Invalid user ID {}").format(user_id))- user_profiles.append(user_profile)-- do_send_typing_notification(- realm=realm,- sender=sender,- recipient_user_profiles=user_profiles,- operator=operator,- )---def do_send_stream_typing_notification(- sender: UserProfile, operator: str, stream: Stream, topic: str-) -> None:-- sender_dict = {"user_id": sender.id, "email": sender.email}-- event = dict(- type="typing",- message_type="stream",- op=operator,- sender=sender_dict,- stream_id=stream.id,- topic=topic,- )-- user_ids_to_notify = get_user_ids_for_streams({stream.id})[stream.id]-- send_event(sender.realm, event, user_ids_to_notify)--def ensure_stream(realm: Realm,stream_name: str,diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex cc101f34c94..26b62bad678 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -13,6 +13,7 @@ from unittest import mockimport orjsonfrom django.utils.timezone import now as timezone_now+from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notificationfrom zerver.actions.user_groups import (bulk_add_members_to_user_group,check_add_user_group,@@ -26,7 +27,6 @@ from zerver.lib.actions import (bulk_add_subscriptions,bulk_remove_subscriptions,check_add_realm_emoji,- check_send_typing_notification,do_add_alert_words,do_add_default_stream,do_add_linkifier,@@ -82,7 +82,6 @@ from zerver.lib.actions import (do_rename_stream,do_revoke_multi_use_invite,do_revoke_user_invite,- do_send_stream_typing_notification,do_set_realm_authentication_methods,do_set_realm_message_editing,do_set_realm_notifications_stream,diff --git a/zerver/views/typing.py b/zerver/views/typing.pyindex 161eca9593b..446448ee4a2 100644--- a/zerver/views/typing.py+++ b/zerver/views/typing.py@@ -3,7 +3,7 @@ from typing import List, Optionalfrom django.http import HttpRequest, HttpResponsefrom django.utils.translation import gettext as _-from zerver.lib.actions import check_send_typing_notification, do_send_stream_typing_notification+from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notificationfrom zerver.lib.exceptions import JsonableErrorfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.response import json_success