Skip to content

Conversation

@hafizrahman
Copy link
Contributor

@hafizrahman hafizrahman commented Nov 18, 2024

Closes: #14432

Description

When logging in with application password, the Media Library only fetches 10 images regardless. From investigating and comparing with Android, the API request in iOS uses number parameter.

According to the API doc, number parameter does not exist and instead per_page is needed: https://developer.wordpress.org/rest-api/reference/media/#arguments

Android also uses per_page and thus do not see this issue:

https://github.com/wordpress-mobile/WordPress-FluxC-Android/blob/cc17141a0e910d5b808ba6bbfb4b7d393d3517a0/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpapi/media/BaseWPV2MediaRestClient.kt#L231

This PR updates the parameter to the correct one.

The above should also apply the same for Jetpack CP site.

For full Jetpack site, the parameter number should still be used, and this PR differentiates between the two usage.

Reference:

  1. .org API https://developer.wordpress.org/rest-api/reference/media/#arguments
  2. .com API https://developer.wordpress.com/docs/api/1.2/get/sites/%24site/media/

Steps to reproduce

a. No Jetpack case (Application Password)

  1. Have a .org site with no Jetpack,
  2. Upload more than 25 images in wp-admin Media > Library,
  3. In the app, login to site with application password,
  4. Go to Products > Add Product > Add Product Image > WordPress Media Library
  5. Ensure all the images are loaded, not just 10.

b. Jetpack CP site case

  1. Have a .org site with no Jetpack but with a plugin with Jetpack CP (I use "Blaze Ads"),
  2. Setup the Jetpack CP plugin and connect it to your WordPress.com account,
  3. Upload more than 25 images in wp-admin Media > Library,
  4. In the app, login to site with WordPress.com account,
  5. Go to Products > Add Product > Add Product Image > WordPress Media Library
  6. Ensure all the images are loaded, not just 10.

c. WordPress.com / full Jetpack site case

  1. Have a .org site with Jetpack plugin,
  2. Setup the Jetpack plugin and connect it to your WordPress.com account,
  3. Upload more than 25 images in wp-admin Media > Library,
  4. In the app, login to site with WordPress.com account,
  5. Go to Products > Add Product > Add Product Image > WordPress Media Library
  6. Ensure all the images are loaded, not just 10.

New: "media_type" fix test

  1. Have a .org site with no Jetpack,
  2. Upload some images and at least one video to media library,
  3. In the app, login to site with application password,
  4. Go to Products > Add Product > Add Product Image > WordPress Media Library
  5. Ensure all the images are loaded, but the video is not shown

Testing information

I tested this on three type of sites above, on iPhone simulator with iOS 18.


  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on all devices (phone/tablet) and no regressions are added.

@hafizrahman hafizrahman added type: bug A confirmed bug. feature: REST API Authenticating requests using application password and using REST API instead of Jetpack tunnel. labels Nov 18, 2024
@hafizrahman hafizrahman added this to the 21.2 milestone Nov 18, 2024
@hafizrahman hafizrahman marked this pull request as ready for review November 18, 2024 10:10
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Nov 18, 2024

WooCommerce iOS📲 You can test the changes from this Pull Request in WooCommerce iOS by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS WooCommerce iOS
Build Numberpr14444-43c0679
Version21.1
Bundle IDcom.automattic.alpha.woocommerce
Commit43c0679
App Center BuildWooCommerce - Prototype Builds #11666
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

private enum ParameterKey {
static let pageNumber: String = "page"
static let pageSize: String = "number"
static let pageSize: String = "per_page"
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for working on this, @hafizrahman! I will wait for @hichamboushaba to review this.


I am jumping in as this parameter key change will affect both loadMediaLibrary and loadMediaLibraryFromWordPressSite methods in this MediaRemote file.

The number parameter key is from the following documentation. https://developer.wordpress.com/docs/api/1.2/get/sites/%24site/media/

Please make sure to test the following login cases and update the PR testing instructions accordingly.

  1. Jetpack
  2. JCP
  3. No Jetpack. (Application password)

I am afraid that one of the above cases could be broken/affected by this change.


Nice to have: It would be nice to add a unit test to ensure that the parameter key is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh good catch, I tested in WordPress.com console and indeed it wants number. Let me refactor this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 7215568

Good for another review.

@hichamboushaba
Copy link
Member

@hafizrahman just sharing this bug that I identified in Android last week, it's similar to this one, we use the mime_type parameter for all implementations, while it should be media_type for loadMediaLibraryFromWordPressSite, do you think it makes sense to fix it on the same PR?

@hafizrahman
Copy link
Contributor Author

hafizrahman commented Nov 20, 2024

@hichamboushaba thanks for the ping! I'll have it added here.

@hafizrahman
Copy link
Contributor Author

Added media_type fix on 02bd74d and updated test instruction at the top with this scenario.

@selanthiraiyan selanthiraiyan self-assigned this Nov 21, 2024
Copy link
Contributor

@selanthiraiyan selanthiraiyan left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, Hafiz!

b. Jetpack CP site case

Unfortunately, the above case doesn't work. I used Blaze Ads plugin to connect my WPCOM account with the JN site.

While trying to load images from the media library, I end up seeing the following error alert.

To debug this,

I looked into proxyman, and I see that I do receive a success response from the store for the following request.

https://public-api.wordpress.com/wp/v2/sites/1234/media?_fields=id%2Cdate_gmt%2Cslug%2Cmime_type%2Csource_url%2Calt_text%2Cmedia_details%2Ctitle&media_type=image&page=1&per_page=25

I looked into Xcode logs, and I see the following decoding error.

<> Mapping Error: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_CodingKey(stringValue: "Index 6", intValue: 6), CodingKeys(stringValue: "media_details", intValue: nil), CodingKeys(stringValue: "sizes", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

@hichamboushaba
Copy link
Member

hichamboushaba commented Nov 21, 2024

I looked into Xcode logs, and I see the following decoding error.

<> Mapping Error: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_CodingKey(stringValue: "Index 6", intValue: 6), CodingKeys(stringValue: "media_details", intValue: nil), CodingKeys(stringValue: "sizes", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

This looks like the media_details.sizes property is returning a dictionary instead of the expected object, I had this happen in my tests when I worked on this in Android, but it was only for non-image types, which was happening because we weren't passing the correct argument media_type and we were using the mime_type one.
(This issue shouldn't be because of the PR's changes, and should happen also on trunk I think)

I tried to replicate this, but I couldn't on a test site that I have right now. It's the EOD for me, but I'll try to take a look at this if needed tomorrow, if it's an issue then it would impact Android as well.

@selanthiraiyan
Copy link
Contributor

Thanks for taking a look, @hichamboushaba!

I tried to replicate this, but I couldn't on a test site that I have right now.

I have sent invites to my JN store to both you and @hafizrahman. (To your A8C emails)

@wpmobilebot wpmobilebot modified the milestones: 21.2, 21.3 Nov 22, 2024
@wpmobilebot
Copy link
Collaborator

Version 21.2 has now entered code-freeze, so the milestone of this PR has been updated to 21.3.

@hichamboushaba
Copy link
Member

Using your site @selanthiraiyan, I can see that an image item has the sizes property empty, and when using the WPCom endpoint, this item is returned as an array instead of an empty object (I think this is something that happens a lot with WordPress, in FluxC we have an old class just to handle similar cases).

So, my assumption that sizes won't be empty for images is wrong, we need to fix this in both platforms then, we should:

  1. Either gracefully handle the switch to empty array (this is what I'll do in Android).
  2. Remove the property from the Networking model, this is an easier solution if we don't use the sizes property in the app.

@hichamboushaba
Copy link
Member

An update on why this is happening on your site @selanthiraiyan, one of the images in your site is pretty small (4x2 pixels...), when an image is small, WordPress skips generating any other alternative sizes (as they are generally smaller than the original one), so this leads to the empty object.

Copy link
Contributor

@selanthiraiyan selanthiraiyan left a comment

Choose a reason for hiding this comment

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

👋 @hafizrahman I am approving this PR as Hicham's explanation makes sense to me.

Could you please clone this woocommerce/woocommerce-android#12989 for iOS and merge this PR? Thanks!

@hafizrahman
Copy link
Contributor Author

Could you please clone this woocommerce/woocommerce-android#12989 for iOS and merge this PR? Thanks!

Sure: #14516

@hafizrahman hafizrahman merged commit 459a09d into trunk Nov 26, 2024
17 of 18 checks passed
@hafizrahman hafizrahman deleted the issue/14432-media-library-missing-images branch November 26, 2024 09:40
@selanthiraiyan
Copy link
Contributor

@hafizrahman The release note for this PR is mentioned under version 21.2. The PR is targeting 21.3, though.

@hafizrahman
Copy link
Contributor Author

@selanthiraiyan good catch, thanks, fixing in #14521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: REST API Authenticating requests using application password and using REST API instead of Jetpack tunnel. type: bug A confirmed bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing images in media library

5 participants