Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actions: Refactor check_message to return a dataclass instead of Dict. #16786

Merged
merged 1 commit into from
Dec 21, 2020

Conversation

sahil839
Copy link
Collaborator

This PR refactors the check_message function in zerver/lib/actions.py to return a dataclass instead of Dict.
Also added few prep commits.

Testing plan: Modified the tests.

@sahil839 sahil839 changed the title [WIP]actions: Refactor check_message to return a dataclass instead of Dict. actions: Refactor check_message to return a dataclass instead of Dict. Nov 24, 2020
Comment on lines 1521 to 1528
already_sent_ids: List[int] = []
new_messages: List[MutableMapping[str, Any]] = []
for message in messages:
if isinstance(message['message'], int):
already_sent_ids.append(message['message'])
else:
new_messages.append(message)
messages = new_messages
Copy link
Member

@andersk andersk Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was added by commit 9c8a9ac with message “This was previously causing us to generate a traceback every time we hit a duplicated zephyr due to CC'ing.” Tim confirmed that these duplicate mirror attempts will still occur in case of CC’d Zephyrs, so we still need a way to handle them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andersk I have updated the PR. I have changed the caller of check_message to return the message-id directly when the exception is raised instead of returning it via do_send_messages as we were doing it previously to avoid using MessageInfo dataclass for this case.
The checks are failing because of coverage issue and I will fix this. Does this approach seem fine to you or we should do something else?

Copy link
Collaborator Author

@sahil839 sahil839 Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just wanted to confirm, we would not these checks for check_send_stream_message and check_send_private_message as these are just used by webhooks and tests, and from what I have read in the Integrations section of https://zulip.readthedocs.io/en/latest/subsystems/client.html, I can understand that client cannot be zephyr_mirror for messages sent through webhooks. So, the try..except block will be needed in check_send_message only and can be removed from other places.

@sahil839 sahil839 force-pushed the message-dict branch 2 times, most recently from 6a5cf87 to ce14df6 Compare November 25, 2020 09:33
@showell
Copy link
Contributor

showell commented Dec 1, 2020

LGTM.

@@ -1691,7 +1682,7 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str,
# returned. In practice, this shouldn't matter, as we only
# mirror single zephyr messages at a time and don't otherwise
# intermingle sending zephyr messages with other messages.
return already_sent_ids + [message['message'].id for message in messages]
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above comment is no longer accurate and can be deleted with this commit; I'll take care of that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, missed that.

@timabbott
Copy link
Sponsor Member

Overall, this looks great! I merged a few prep commits as the series ending with 37c8505. My only concern at present is the naming for this new structure.

I think we should do s/MessageInfo/SendMessageRequest/, with perhaps send_request as the local variable name. I would also do the renamings in the same commit as the rest (i.e. squash the follow-up commits), since we're touching those lines anyway and message.message reads really oddly in the intermediate commits. Can you make those changes and see how it looks? Thanks @sahil839!

@sahil839 sahil839 force-pushed the message-dict branch 3 times, most recently from 6ca8fa5 to b0e1220 Compare December 19, 2020 17:53
… Dict.

We change the return type of check_message to be dataclass instead of
Dict[str, Any]. This refactoring helps us to understand the context of the
data structure returned by check_message clearly which was not possible
when using Dict.

SendMessageRequest class is added in zerver/lib/message.py inspite of it
not being used in that file itself just to maintain consistency as other
TypedDicts and dataclasses are defined in that file and to avoid circular
dependency as SendMessageRequest is being used in lib/widget.py as well.

We also rename local variable to 'send_request' for accessing
SendMessageRequest objects.
@sahil839
Copy link
Collaborator Author

@timabbott Updated the PR. Renamed the class to SendMessageRequest and local variable to send_request.

@timabbott timabbott merged commit 2fa33be into zulip:master Dec 21, 2020
@timabbott
Copy link
Sponsor Member

Merged, thanks @sahil839! I really like this cleanup. @showell FYI in case you have proposals for next steps.

@sahil839 sahil839 deleted the message-dict branch December 22, 2020 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants