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

Post / Page creation: Don't send default Post.authorID to the API #19717

Merged
merged 3 commits into from
Dec 9, 2022

Conversation

twstokes
Copy link
Contributor

@twstokes twstokes commented Dec 3, 2022

Fixes #19589

Description

This PR resolves an issue where the app could send an invalid author ID for Posts and Pages.

Problem

When the app doesn't have a Blog.userID set and the user creates a new Post or Page, the app is setting the author ID to 0, which is the default value for a BasePost's author ID in the Core Data model. This 0 gets sent to the API which is an invalid author ID.

The main reason Blog.userID might not be set is because the updateMultiAuthor method of BlogService hasn't ran, or had a failure on the server side.

See technical summary

There currently exists a race where a new Post or Page can be created before the blog's user ID is populated. The call can also just outright fail, like any API call:

if ([user.username isEqualToString:blog.username]) {
blog.userID = user.userID;
break;

If a new Post or Page is created before the blog's user ID is populated, the post's author ID never gets set

if let userID = userID, let author = getAuthorWith(id: userID) {
post.authorID = author.userID
post.author = author.displayName
}

So it sticks with the default value assigned when instantiating (in this case) the Post:

let post = NSEntityDescription.insertNewObject(forEntityName: NSStringFromClass(Post.self), into: context) as! Post

At first glance this doesn't appear to be a problem, since:

  • It's OK to not send an author to the API - it will default to the authenticated user creating the post
  • The Post / Page's authorID is an optional NSNumber, so we assume it would default to nil
  • Author IDs that aren't set aren't sent to the API [REST] [XML-RPC]

The issue is that BasePost.authorID defaults to 0 in the Core Data model.

So in these cases we're sending a 0 for the author ID instead of not sending the value, which can result in errors like:

<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>404</int></value>
        </member>
        <member>
          <name>faultString</name>
          <value><string>Invalid author ID.</string></value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>

What worsens the issue is that once a Post or Page has been created by the app, the invalid author ID of 0 is stuck to it. Even if the blog's user ID is synced later on, it will only fix the issue for new Posts and Pages.

Another potential solution would be to remove the default value of 0 from the Core Data model, but this could introduce risk to existing logic.

Potential workarounds

  • Add another user to the site so it becomes multi-author and unlocks the author picker in the Post and Page settings. Selecting the correct author should update the value and allow the user to publish.
  • For user roles who can't query other authors (lower than Editor), temporarily elevate their permissions.

Solution

If a Post or Page's author ID hasn't been set locally, we shouldn't send a value to the API.

Testing

Both the WordPress and Jetpack apps should be tested.

Self-hosted site publishing

  1. Clean install the app
  2. Spin up a new self-hosted site, not connected to Jetpack
  3. Quickly create a Post or Page after logging into the site in the app
  4. Publish it
  5. Expect the Post or Page to successfully publish

Self-hosted site connected to Jetpack

  1. Clean install the app
  2. Quickly create a Post or Page after logging into the site in the app
  3. Publish it
  4. Expect the Post or Page to successfully publish

WordPress.com site

  1. Clean install the app
  2. Quickly create a Post or Page after logging into the site in the app
  3. Publish it
  4. Expect the Post or Page to successfully publish

Multi-user sites as an Admin or Editor role

This test should be ran with an Admin or Editor role.

  1. On a site with multiple users, create a new Post or Page (If a site only has one user, add at least one more)
  2. In the settings for the Page or Post, change the author and publish it
  3. Expect the Post or Page to successfully publish
  4. Expect the Post or Page to be assigned to the author in step 2

Multi-user sites as an Author or Contributor role

This test should be ran with an Author or Contributor role.

  1. On a site with multiple users, create a new Post or Page (If a site only has one user, add at least one more)
  2. Expect to not see the option to change the author in the Post or Page settings
  3. Expect the Post or Page to successfully publish if user is an Author (note: Contributors cannot publish)

Regression Notes

  1. Potential unintended areas of impact

    • Publishing Posts and Pages
    • Author selection in Post / Page settings for multi-author sites
  2. What I did to test those areas of impact (or what existing automated tests I relied on)

    • Manual testing and existing automated tests.
  3. What automated tests I added (or what prevented me from doing so)

    • Added tests for constructing a RemotePost (e905ae5)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@twstokes twstokes self-assigned this Dec 3, 2022
@twstokes twstokes changed the title When creating a new post explicitly set the author ID to nil in the absence of a user ID and or author Post creation: Explicitly set the author ID to nil in the absence of a user ID and or author Dec 3, 2022
@twstokes twstokes force-pushed the bug/publish-failures branch from cb9c1bd to 466c3f7 Compare December 3, 2022 02:57
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Dec 3, 2022

You can test the changes in Jetpack from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr19717-8a92382 on your iPhone

If you need access to App Center, please ask a maintainer to add you.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Dec 3, 2022

You can test the changes in WordPress from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr19717-8a92382 on your iPhone

If you need access to App Center, please ask a maintainer to add you.

@twstokes twstokes changed the title Post creation: Explicitly set the author ID to nil in the absence of a user ID and or author Post / Page creation: Explicitly set the author ID to nil in the absence of a user ID and or author Dec 3, 2022
@twstokes twstokes added this to the 21.4 milestone Dec 5, 2022
@twstokes twstokes force-pushed the bug/publish-failures branch 2 times, most recently from da6d734 to 0484cca Compare December 7, 2022 21:53
@twstokes twstokes changed the title Post / Page creation: Explicitly set the author ID to nil in the absence of a user ID and or author Post / Page creation: Remove default value for BasePost.authorID Dec 7, 2022
@twstokes twstokes force-pushed the bug/publish-failures branch from 0484cca to 4144e97 Compare December 8, 2022 04:12
@twstokes twstokes changed the title Post / Page creation: Remove default value for BasePost.authorID Post / Page creation: Don't send default or nil Post.authorID to the API Dec 8, 2022
@twstokes twstokes marked this pull request as ready for review December 8, 2022 05:00
@twstokes twstokes changed the title Post / Page creation: Don't send default or nil Post.authorID to the API Post / Page creation: Don't send default Post.authorID to the API Dec 8, 2022
@twstokes twstokes force-pushed the bug/publish-failures branch from 8f7c973 to 8a92382 Compare December 8, 2022 05:12
@twstokes twstokes requested a review from alpavanoglu December 8, 2022 05:14
@alpavanoglu
Copy link
Contributor

  • Self-hosted site publishing ✅
  • Self-hosted site connected to Jetpack ❌ (Upload fails here when I try to publish it)

I'll continue testing the rest after your input @twstokes as I assume it might have affect on the other cases too?

@twstokes
Copy link
Contributor Author

twstokes commented Dec 8, 2022

Thank you for the thorough testing @alpavanoglu! 🙇

  • Self-hosted site connected to Jetpack ❌ (Upload fails here when I try to publish it)
  1. Was this a clean install of the app, or did you reuse the install from the non-Jetpack connected self-hosted site?
  2. Do you happen to have any of the errors from this? (locally saved log, Xcode debugger output)
  3. Was this the WordPress or Jetpack app?

@alpavanoglu
Copy link
Contributor

  1. It was a clean install.
  2. No but I can test it again. I assume it works on your case then right?
  3. Jetpack app

@twstokes
Copy link
Contributor Author

twstokes commented Dec 8, 2022

  1. No but I can test it again. I assume it works on your case then right?

I haven't had any issues yet, but here's what I did:

  1. Set up a new self-hosted site
  2. Connected that site to Jetpack with a personal WordPress.com account
  3. Logged into the Jetpack app with the .com account
  4. Selected the site and created a Post
  5. Published the Post without error

@alpavanoglu
Copy link
Contributor

What's strange is when I launched the app again in the same state, a pop-up with an error appeared immediately stating that publish update has failed. I'll try the same steps again and see if it happens again, if so let you know what I can find out.

@twstokes
Copy link
Contributor Author

twstokes commented Dec 8, 2022

What's strange is when I launched the app again in the same state, a pop-up with an error appeared immediately stating that publish update has failed. I'll try the same steps again and see if it happens again, if so let you know what I can find out.

😬 Thank you! Any logs from when the error occurred would be helpful.

@derekblank
Copy link
Contributor

derekblank commented Dec 9, 2022

I was able to test this build during some downtime as well. If it helps for feedback, I was able to successfully test all four scenarios using the Jetpack app following the instructions for each:

  • Self-hosted site publishing ✅
  • Self-hosted site connected to Jetpack ✅
  • WordPress.com site ✅
  • Multi-author site (including switching post author between Administrator and Author roles) ✅

I can continue to help test next week, if needed.

@twstokes
Copy link
Contributor Author

twstokes commented Dec 9, 2022

I was able to test this build during some downtime as well. If it helps for feedback, I was able to successfully test all four scenarios using the Jetpack app following the instructions for each:

I can continue to help test next week, if needed.

Thanks a bunch @derekblank for the extra testing! 🙇 Great to know they all worked for you.

@twstokes twstokes requested a review from salimbraksa December 9, 2022 14:50
@peril-wordpress-mobile
Copy link

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 4 days Please, make sure to get it merged by then or assign it to a later expiring milestone

Generated by 🚫 dangerJS

@salimbraksa
Copy link
Contributor

salimbraksa commented Dec 9, 2022

All scenarios work as expected. LGTM! 🚀

Self-hosted site publishing ✅

  1. Created this self-hosted site: https://clean-scarlet.jurassic.ninja
  2. Clean installed the WordPress app ( deleted an existing build in Simulator )
  3. Logged into my self-hosted site and published a post successfully

Self-hosted site connected to Jetpack ✅

  1. Re-installed the WordPress app
  2. Connected the previous site to Jetpack
  3. Logged into my self-hosted site and published a post successfully

WordPress.com site ✅

  1. Re-installed the WordPress app
  2. Logged into my wordpress.com account and published a post successfully

Multi-user sites as an Admin or Editor role ✅

  1. Re-installed the WordPress app
  2. Added a new user fakesalimbraksa with an Editor role to clean-scarlet.jurassic.ninja site
  3. Logged into my self-hosted site and published a post successfully
  4. Verified the post's author is fakesalimbraksa

Multi-user sites as an Author or Contributor role ✅

  1. Changed fakesalimbraksa role to Author
  2. Re-installed the WordPress app
  3. Logged into my self-hosted site and published a post successfully
  4. Verified the post's author is fakesalimbraksa

@twstokes
Copy link
Contributor Author

twstokes commented Dec 9, 2022

Thanks a bunch @salimbraksa, @alpavanoglu, and @derekblank! I'll merge this and make sure it lands in the CfT as well. 🙇

@twstokes twstokes merged commit 249927c into trunk Dec 9, 2022
@twstokes twstokes deleted the bug/publish-failures branch December 9, 2022 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Publish content not possible anymore on self-hosted site
5 participants