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

Gutenberg - async media upload Post background processing #9132

Merged
merged 16 commits into from
Jan 29, 2019

Conversation

mzorz
Copy link
Contributor

@mzorz mzorz commented Jan 28, 2019

Fixes part of wordpress-mobile/gutenberg-mobile#475

Follow up to #9129 (media upload progress reattach). That PR handles the visual updates, while this other PR handles the updates to a Gutenberg Post when an image upload finishes but the user is not currently using the Gutenberg editor.

This PR introduces a MediaUploadReadyProcessor implementation for Gutenberg Image blocks based on search & replace, and also makes Post<-> media associations aware of Gutenberg posts in purgeMediaToPostAssociationsIfNotInPostAnymore.

For the first (MediaUploadReadyProcessor), I've been considering some possible paths and have made up my mind for the easiest to implement for now, but may as well not be the most reliable as it makes several assumptions regarding the structure of an Image block as it currently is.

However, the PR works as per the current state of affairs, and I believe we shouldn't expect there to be breaking changes to the block structure in the future given it's one of the core blocks.

For further discussion

Other possibilities:

By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. You can pass a fourth boolean argument to control this behaviour.

(this is indeed to be used only when the GB Editor is not on the foreground, but we should also consider situations in which we have already launched the background task and the user is also trying to bring GB editor up in the middle.)

  • use an HTML parser at least to make sense of the HTML tags to make url replacement a little bit more intelligent (or, HTML aware)

  • use some kind of regex or string replacement like this one here.

To test:

Pretty much we can follow the test cases described in #5780, writing here the most simple case (situation described in CASE B there):

  1. start a new draft
  2. insert an image block and choose an from device
  3. tap the back arrow to exit the editor, observe the image continues to upload
  4. also tap back again to exit the Posts list, back to the main app screen (as the posts list screen gets updated more frequently and can change the effect of this test). Even send the app to the background if you want.
  5. once the image is done uploading a notification will appear.
  6. go to the web and open the Post
  7. confirm the image block has no validation errors, and the Post opens and displays the image correctly.

Update release notes:

  • If there are user facing changes, I have added an item to RELEASE-NOTES.txt.


if (showAztecEditor) {
if (showGutenbergEditor) {
// TODO check if anything needs be done in Gutenberg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This // TODO mark left here on purpose

@mzorz mzorz changed the base branch from gb/media_upload_progress_reattach to gb/feature_upload_media_file January 28, 2019 14:21
Matcher m = p.matcher(postContent.substring(iStartOfImgTag));
if (m.find()) {
String src = m.group();
int startIndex = src.indexOf("src=") + 5;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we put magic number 5 in some variable so that we can remember what's the idea behind it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 addressed in 6c5ec0a

Copy link
Contributor Author

Choose a reason for hiding this comment

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

er, re-wrote it in 924725e to a class constant so it's easier to read

// TODO: replace the URL
if (!mediaFile.isVideo()) {
// replace gutenberg block id holder with serverMediaId, and url_holder with remoteUrl
String oldImgBlockHeader = String.format("<!-- wp:image {\"id\":%s} -->", localMediaId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we can put this <!-- wp:image {\"id\":%s} --> in some constant ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in a321f5e

@@ -368,6 +371,55 @@ public static boolean contentContainsGutenbergBlocks(String postContent) {
return (postContent != null && postContent.contains(GUTENBERG_BLOCK_START));
}

public static String replaceMediaFileWithUrl(Context context, @NonNull String postContent,
Copy link
Contributor

Choose a reason for hiding this comment

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

As this is a static method, maybe it's a good candidate for some Util or Helper?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea, I was following the same pattern as we had for Aztec: in Aztec the class that can handle / is responsible for editing a Post is AztecEditorFragment, and we needed a static method as this manipulation of the Post's content happens asynchronously (that is, when the AztecEditorFragment is not instanced).
For Gutenberg instead, this responsibility can be split into another class as you suggested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed this change in 7472379

@mzorz
Copy link
Contributor Author

mzorz commented Jan 29, 2019

Thanks for your feedback, this is ready for another round @marecar3 🙇

Copy link
Contributor

@marecar3 marecar3 left a comment

Choose a reason for hiding this comment

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

Nice Job @mzorz!

@mzorz mzorz merged commit 04479bb into gb/feature_upload_media_file Jan 29, 2019
@mzorz mzorz deleted the gb/media_upload_background_process branch January 29, 2019 19:59
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.

2 participants