8000 Check if `default_payload` is a dict before using it as a dict by H-Shay · Pull Request #292 · matrix-org/sygnal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Check if default_payload is a dict before using it as a dict #292

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

Merged
merged 16 commits into from
Jan 25, 2022

Conversation

H-Shay
Copy link
Contributor
@H-Shay H-Shay commented Jan 5, 2022

Fixes #291.

@H-Shay H-Shay requested a review from a team as a code owner January 5, 2022 22:43
@DMRobertson
Copy link
Contributor

I think this is waiting on changes to reject the pushkey(?)

@H-Shay
Copy link
Contributor Author
H-Shay commented Jan 20, 2022

Yes I've add changes which reject the pushkey-I am not convinced of their elegance but I figured best to get some feedback on my approach. Once the gcmpushkin and apsnpushkin changes look good, I can change webpushkin as well.

@H-Shay H-Shay requested a review from a team January 20, 2022 18:34
Copy link
Contributor
@DMRobertson DMRobertson left a comment

Choose a reason for hiding this comment

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

A few things.

Thank you for the test cases! Is it worth having one for the webpushkin too?

Comment on lines 354 to 364
payload.update(device.data.get("default_payload", {}))
default_payload = device.data.get("default_payload", {})
payload.update(default_payload)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this use of default_payload need checking for not being a dict too? (Not sure of the bigger picture here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking on this was that we do the check on default_payload several lines before the only call to the function where this code is located (_get_payload_event_id_only), and the check returns/stops execution if the default payload is not a dict, so afaict the execution should never get to this stage if default_payload is not a dict.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think your reasoning is correct; but I have in mind:

  • maintainability: in the future, we might call _get_payload_event_id_only with a device whose default_payload hasn't been validated
  • ease of reading: it'd be nice to arrange this in a way that the reader doesn't need to repeat this reasoning every time we come to look at default_payload.

(Warning: the following is splitting hairs.)

Looking at the body of _get_payload_event_id_only, I note that the only time we use the device argument is to retrieve the default_payload (see also #127). I'm tempted to suggest:

  • remove _get_payload_event_id_only's device argument
  • have it take a default_payload: Dict[str, Any] instead
  • change the caller to pass default_payload directly, handling the case where device.data is None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great I will do that, thanks for outlining your reasoning!

Copy link
Contributor

Choose a reason for hiding this comment

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

(To emphasise: this is fine as it is and I am going a little too extreme here. I think this is great as written and definitely mergeable. But I wanted to outline the thinking behind the question.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha i just re-implmented it with your changes, you let me know if seems better-I honestly think both ways feel a little clunky, but that's because the surrounding code is a little clunky (lots of conditional branches, probably unavoidable).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, I had something slightly different in mind. I was thinking:

  • above line 288 add default_payload = {}
  • this means we can remove lines 297 through 301, and always use 302 (because we always have a default payload)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right that makes more sense, have updated.

@H-Shay H-Shay requested a review from DMRobertson January 21, 2022 21:27
Copy link
Contributor
@DMRobertson DMRobertson left a comment

Choose a reason for hiding this comment

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

LGTM, and apologies again for my nitpicking.

Comment on lines +356 to +357
if data is None:
failed.append(device.pushkey)
Copy link
Member

Choose a reason for hiding this comment

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

looks like this will go on to attempt delivery anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It sure does look like that-which probably doesn't make sense. I can fix that along with downgrading the error to a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: 'NoneType' object is not iterable in apnspushkin.py and gcmpushkin.py
5 participants
0