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

Social: Standardize the connection object in the recently added social endpoints #37390

Closed
wants to merge 47 commits into from

Conversation

spsiddarthan
Copy link
Contributor

@spsiddarthan spsiddarthan commented May 15, 2024

The newly added Jetpack Social endpoints don't return data in the same format as the initial state. This PR standardizes the response for the connection object. I've also updated the jetpack/v4/publicize/connections endpoint which isn't used anywhere.
We are getting rid of the site option and are going to rely on transient to cache the connections on the Jetpack site.

Proposed changes:

  • Updated the create and update publicize connections endpoint to call the newly added get_connections_for_user publicize function.
  • Updated the get publicize connections endpoint to call the existing get_all_connections_for_user publicize function.
  • Migrated the publicize_connections to a transient to avoid issues with caching of the option.
  • Added shared key to the connections object.
  • Added status to the connections object.
  • To refresh connection test results, add this query param run_test_results=true to your get request.
  • To clear connection cache, add this query param clear_cache =1 to your get request.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Don't sandbox and test against prod.
  • Boot up a JN site on this branch or checkout this branch on your local and ensure define( 'JETPACK_SOCIAL_USE_ADMIN_UI_V1', true ); is enabled or add the blog sticker to enable the feature.
  • Simulate the post, put and get requests to the following endpoints: POST wp-json/jetpack/v4/social/connections, PUT wp-json/jetpack/v4/social/connections/:id and GET wp-json/jetpack/v4/publicize/connections and observe that the API response.

For the GET request, this will be the response.

{
    "refresh_token": "",
    "type": "access",
    "user_id": "",
    "provider": "nextdoor:wordpressxxxx",
    "issued": "2023-10-25 05:29:34",
    "expires": 1747224005,
    "external_id": "",
    "external_name": "Sid Darthan",
    "external_display": "Sid Darthan",
    "connection_data": {
        "id": "",
        "token_id": "",
        "blog_id": "",
        "user_id": "",
        "meta": {
            "external_user_id": ""
        }
    },
    "profile_picture": "xxxx",
    "service_name": "nextdoor",
    "connection_id": "",
    "can_disconnect": true,
    "profile_link": "https://nextdoor.com/profile/",
    "shared": false,
    "status": "ok"
}

To get connection test results, include these query params wp-json/jetpack/v4/publicize/connections?run_test_results=true
When you create a connection by calling https://temporary-lemming.jurassic.ninja/wp-json/jetpack/v4/social/connections with the body {"keyring_connection_ID" : xxx, "shared" : false }, you will see a similar response. Likewise for globalizing/unglobalizing as well.

Copy link
Contributor

github-actions bot commented May 15, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the update/social-endpoints branch.

    • For jetpack-mu-wpcom changes, also add define( 'JETPACK_MU_WPCOM_LOAD_VIA_BETA_PLUGIN', true ); to your wp-config.php file.
  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack update/social-endpoints
    
    bin/jetpack-downloader test jetpack-mu-wpcom-plugin update/social-endpoints
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented May 15, 2024

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen daily.
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for June 4, 2024 (scheduled code freeze on June 3, 2024).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Backup plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Boost plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Search plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Social plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Starter Plugin plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Videopress plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Migration plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Mu Wpcom plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Inspect plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Automattic For agencies client plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels May 15, 2024
@github-actions github-actions bot added [Plugin] Automattic For Agencies Client [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Inspect [Plugin] Migration [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. labels May 15, 2024
@spsiddarthan spsiddarthan requested a review from a team May 15, 2024 16:10
@spsiddarthan spsiddarthan marked this pull request as ready for review May 15, 2024 16:10
@spsiddarthan spsiddarthan force-pushed the update/social-endpoints branch 2 times, most recently from 32d26cc to f4c2065 Compare May 20, 2024 15:54
Copy link
Contributor

@pablinos pablinos left a comment

Choose a reason for hiding this comment

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

I think this is a good change to be moving away from setting the option.

For the GET request, this will be the response. Notice that connection_health in Unknown. If you include the query param like this ?params=connection_health, the connection_result will be updated. Possible values are Unknown, Healthy and Unhealthy.

I'm not sure that we should be doing this. There is the _fields global parameter for the REST API which allows the caller to request which fields should be included in the response. We can have the default as not including the connection_states but use this core functionality to allow it to be requested when required.

projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
@@ -443,7 +450,7 @@ public function update_publicize_connection( $request ) {
$body['shared'] = $shared;
}

$response = Client::wpcom_json_api_request_as_user(
Client::wpcom_json_api_request_as_user(
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it matter if this is unsuccessful? I think we're changing the response so it always returns that it's successful, but tha actual connection may not have been modified. This means that the caller would need to check the connection data that has been returned and can't rely on the status code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added error handling here.

return rest_ensure_response( $this->make_proper_response( $response ) );

global $publicize;
return rest_ensure_response( $publicize->get_connections_for_user( (int) $connection_id ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have some way of forcing this to be uncached? I think what's happening is after the connection is updated an XMLRPC request will get made so the cache is busted and the connections data will be updated, we'll then get the new connection data here.

But, we could get into some race conditions there. If the XMLRPC request is delayed, then we'll be reading the old connection data at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a param here to force refresh. Is that better? When the page loads initially we can fire a call without the force refresh param. Behind the scenes, should we force refresh after the page loads?

Copy link
Contributor

@pablinos pablinos 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 thoughts. They probably don't stop us merging this.

projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
projects/packages/publicize/src/class-publicize.php Outdated Show resolved Hide resolved
Comment on lines 209 to 215

$connections = get_transient( self::JETPACK_SOCIAL_CONNECTIONS_TRANSIENT );

if ( $connections === false ) {
$connections = array();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about this some more, if the transient expires and we can't retrieve the connections because of a network error, then it will look the same as if they have no connections, and we won't inform the user of the error.

Currently we would always show a cached version of the connections from the options, which would then show as broken when the test results API call fails. I'm not sure how much of an issue this is, but it could be worth us implementing something so that we can determine the difference between a site having no connections and there being an error preventing us from retrieving them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps record an error on local whenever we receive an error? I can create a follow-up task for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think we should have an error state. There will definitely be errors, so we should cope with that and provide feedback - although I realise we don't cope well with it currently!

I think previously we were using a transient for that, but we could use this same transient and store an error state in it, so that gets sent to the front end and can be displayed.

Then we can have an option to force it to try again.

@@ -262,6 +279,8 @@ public function get_all_connections_for_user() {
'connection_id' => $connection['connection_data']['id'],
'can_disconnect' => current_user_can( 'edit_others_posts' ) || get_current_user_id() === $user_id,
'profile_link' => $this->get_profile_link( $service_name, $connection ),
'shared' => $connection['connection_data']['user_id'] === '0',
Copy link
Member

Choose a reason for hiding this comment

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

Should we use this to cater for numeric 0 here and also the Yoda conditional PHPCS error?

Suggested change
'shared' => $connection['connection_data']['user_id'] === '0',
'shared' => empty( $connection['connection_data']['user_id'] ),

@spsiddarthan
Copy link
Contributor Author

Merge #37510 first, and then rebase this PR.

@@ -252,7 +252,7 @@ public function get_all_connections_for_user( $args = array() ) {
array(
'service_name' => $service_name,
'connection_id' => $connection['connection_data']['id'],
'can_disconnect' => self::can_manage_connection( $connection['connection_data'] ),
'can_disconnect' => current_user_can( 'edit_others_posts' ) || get_current_user_id() === $user_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't remember making this change, so I didn't add it as a part of #37510

Copy link
Member

Choose a reason for hiding this comment

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

That change was made in #37501

@spsiddarthan
Copy link
Contributor Author

With #37529 getting merged, we can abandon this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[JS Package] Publicize Components [Package] Connection [Package] Publicize [Plugin] Automattic For Agencies Client [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Inspect [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Migration [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. RNA [Tests] Includes Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants