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

ComposeMenu.js: Handle null for fileName in response from ImagePicker. #3818

Merged
merged 2 commits into from
Jan 31, 2020

Conversation

chrisbobbe
Copy link
Contributor

Fixes #3813.

Copy link
Contributor

@rk-for-zulip rk-for-zulip left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for the writeup and investigation, in particular.

Will approve as-is if you ping me again, but I thought you might want to take another look at the import mechanism.

@@ -63,7 +63,7 @@ class ComposeMenu extends PureComponent<Props> {
// https://github.com/react-native-community/react-native-image-picker/blob/master/docs/Reference.md
error?: string | void | null | false,
uri: string,
fileName: string,
fileName: ?string,
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't have typechecked anyway – this isn't a supertype of Response!

Flow isn't catching this because of the $FlowFixMe up top: ImagePicker is defined as any. Changing the import to import * as ImagePicker (and removing the suppression comment) lets Flow see the definitions.

It seems that either import method works, at least on Android. Which is surprising, to say the least – but perhaps there's some React linkage magic going on. Does it work both ways on iOS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think I see what you mean -- using import * as ImagePicker reveals that the callback we're passing, this.handleImagePickerReponse, isn't accepted by Flow because of the types specified by react-native-image-picker (specifically, .launchCamera expects to pass a Response to that callback, which doesn't include fileName being null, and ours does). I remember being vaguely surprised that no Flow errors were introduced when I made this change, and that I didn't have to add any suppressions. I should probably have at least made a note of my surprise there. Turns out it's because of this very general suppression, as you point out.

I wonder if we can avoid doing such a general suppression. If you get a moment, would you like to go over this in person? I've poked around a bit here and I'm not sure how best to use a modified Response type and a modified type for ImagePicker.launchCamera without making a local copy of react-native-image-picker, which would complicate staying up to date with newer versions. (Maybe we'd just need to make that local copy anyway.)

@chrisbobbe
Copy link
Contributor Author

OK, I've updated the pull request to reflect our chat earlier.

@rk-for-zulip
Copy link
Contributor

Haven't tested yet, but looks good codewise.

  • The change itself needs an inline comment, lest someone come along one day and unknowingly undo all your hard work by tightening up the the callback's type to match the flow-typed declarations.
  • When describing a change in the commit message body, it should also use the to-less infinitive, to match the summary.
  • The line lengths in your commit summaries are a bit long – one of them is 81 characters. Our standard is 70.

@chrisbobbe
Copy link
Contributor Author

The line lengths in your commit summaries are a bit long – one of them is 81 characters. Our standard is 70.

Indeed, and I should know 🤣. I have been meaning to configure Nano to do this for me automatically, but I wasn't sure what any available shortcuts might be to ease enforcing different limits on the summary and the body; I'll ping you or Greg tomorrow on this, and address your other comments.

Thank you!

Chris Bobbe added 2 commits January 29, 2020 16:38
Remove $FlowFixMe above import of ImagePicker; change to
`import * as ImagePicker` so ImagePicker is no longer treated as
`any`.

This was hiding an error caused by the `response` param in
this.handleImagePickerResponse not being $ReadOnly, so, make it
$ReadOnly so no inconsistencies can be introduced by modifying it.

Also revealed was the need to type `fileName` to string | void to
match the Response type in react-native-image-picker; do.
Fixes: zulip#3813.

On iOS, it appears that the fileName is not included in the response
given by ImagePicker.launchCamera if the user has not granted read
access to their photo library. The case where fileName is undefined
was handled correctly, but it turns out it's coming through as null,
which was not handled, so, handle null.

Neither the types nor the docs from react-native-image-picker are
clear on this point, so that was filed as
react-native-image-picker/react-native-image-picker#1271.

Do this without creating a local copy of react-native-image-picker
with our own, correct type, since it's very likely that we'll need
to upgrade to later versions frequently as we're on an early version
of this library which is known to have limitations and bugs.

Add tests for fileName being undefined and being null.
@chrisbobbe
Copy link
Contributor Author

OK, this should be ready for another review, when convenient. 🙂

@rk-for-zulip
Copy link
Contributor

✔️

@rk-for-zulip rk-for-zulip merged commit 5886bac into zulip:master Jan 31, 2020
@chrisbobbe
Copy link
Contributor Author

Thanks for the review and merge!

@chrisbobbe chrisbobbe deleted the issue-3813 branch November 6, 2020 03:07
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.

[iOS]: Crash on adding photo from camera if “Access to photos” permission not granted
2 participants