mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 08:26:11 +00:00
typing: Inline check_typing_notification.
I actually like this pattern:
def check_send_typing_notification(...):
typing_notification = check_typing_notification(...)
do_send_typing_notification(...)
It can help divide responsibilities nicely and make it easy
to write detailed unit tests against each of the two helpers.
Unfortunately, the good things didn't really happen here, and
instead we got the worst aspects of the pattern:
- The responsibilities for validation leaked into
the second function.
- Both functions were doing sane things individually
that became not-so-sane in the big picture (namely,
we ended up making Recipient objects for no reason,
but if you read each of the helpers, it was just one
step that seemed reasonable).
- Passing around dictionaries for results can be annoying.
Also, the pattern made a lot more sense when the validation
for typing was a lot more complicated. My prior commit makes
it so that we only ever deal with a list of user_ids.
Anyway, now I'm inlining it. :)
Subsequent commits will clean up the more substantive issue
here, which is that we are building Recipients for no reason.
This commit is contained in:
@@ -1745,20 +1745,29 @@ def do_remove_reaction(user_profile: UserProfile, message: Message,
|
|||||||
reaction.delete()
|
reaction.delete()
|
||||||
notify_reaction_update(user_profile, message, reaction, "remove")
|
notify_reaction_update(user_profile, message, reaction, "remove")
|
||||||
|
|
||||||
def do_send_typing_notification(realm: Realm, notification: Dict[str, Any]) -> None:
|
def do_send_typing_notification(
|
||||||
recipient_user_profiles = get_typing_user_profiles(notification['recipient'],
|
realm: Realm,
|
||||||
notification['sender'].id)
|
sender: UserProfile,
|
||||||
|
recipient: Recipient,
|
||||||
|
operator: str) -> None:
|
||||||
|
|
||||||
|
recipient_user_profiles = get_typing_user_profiles(
|
||||||
|
recipient,
|
||||||
|
sender.id,
|
||||||
|
)
|
||||||
# Only deliver the notification to active user recipients
|
# Only deliver the notification to active user recipients
|
||||||
user_ids_to_notify = [profile.id for profile in recipient_user_profiles if profile.is_active]
|
user_ids_to_notify = [profile.id for profile in recipient_user_profiles if profile.is_active]
|
||||||
sender_dict = {'user_id': notification['sender'].id, 'email': notification['sender'].email}
|
|
||||||
|
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
|
# 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}
|
recipient_dicts = [{'user_id': profile.id, 'email': profile.email}
|
||||||
for profile in recipient_user_profiles]
|
for profile in recipient_user_profiles]
|
||||||
event = dict(
|
event = dict(
|
||||||
type = 'typing',
|
type='typing',
|
||||||
op = notification['op'],
|
op=operator,
|
||||||
sender = sender_dict,
|
sender=sender_dict,
|
||||||
recipients = recipient_dicts)
|
recipients=recipient_dicts,
|
||||||
|
)
|
||||||
|
|
||||||
send_event(realm, event, user_ids_to_notify)
|
send_event(realm, event, user_ids_to_notify)
|
||||||
|
|
||||||
@@ -1766,15 +1775,8 @@ def do_send_typing_notification(realm: Realm, notification: Dict[str, Any]) -> N
|
|||||||
# Checks the typing notification and sends it
|
# Checks the typing notification and sends it
|
||||||
def check_send_typing_notification(sender: UserProfile, notification_to: Union[Sequence[str], Sequence[int]],
|
def check_send_typing_notification(sender: UserProfile, notification_to: Union[Sequence[str], Sequence[int]],
|
||||||
operator: str) -> None:
|
operator: str) -> None:
|
||||||
typing_notification = check_typing_notification(sender, notification_to, operator)
|
|
||||||
do_send_typing_notification(sender.realm, typing_notification)
|
|
||||||
|
|
||||||
# check_typing_notification:
|
realm = sender.realm
|
||||||
# Returns typing notification ready for sending with do_send_typing_notification on success
|
|
||||||
# or the error message (string) on error.
|
|
||||||
def check_typing_notification(sender: UserProfile,
|
|
||||||
notification_to: Union[Sequence[str], Sequence[int]],
|
|
||||||
operator: str) -> Dict[str, Any]:
|
|
||||||
if len(notification_to) == 0:
|
if len(notification_to) == 0:
|
||||||
raise JsonableError(_('Missing parameter: \'to\' (recipient)'))
|
raise JsonableError(_('Missing parameter: \'to\' (recipient)'))
|
||||||
elif operator not in ('start', 'stop'):
|
elif operator not in ('start', 'stop'):
|
||||||
@@ -1791,7 +1793,13 @@ def check_typing_notification(sender: UserProfile,
|
|||||||
assert isinstance(e.messages[0], str)
|
assert isinstance(e.messages[0], str)
|
||||||
raise JsonableError(e.messages[0])
|
raise JsonableError(e.messages[0])
|
||||||
assert recipient.type != Recipient.STREAM
|
assert recipient.type != Recipient.STREAM
|
||||||
return {'sender': sender, 'recipient': recipient, 'op': operator}
|
|
||||||
|
do_send_typing_notification(
|
||||||
|
realm=realm,
|
||||||
|
sender=sender,
|
||||||
|
recipient=recipient,
|
||||||
|
operator=operator,
|
||||||
|
)
|
||||||
|
|
||||||
def send_stream_creation_event(stream: Stream, user_ids: List[int]) -> None:
|
def send_stream_creation_event(stream: Stream, user_ids: List[int]) -> None:
|
||||||
event = dict(type="stream", op="create",
|
event = dict(type="stream", op="create",
|
||||||
|
|||||||
Reference in New Issue
Block a user