Skip to content

Users API endpoint: add 'viewer' role to user roles array and dedupe return value #41707

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

Draft
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Feb 11, 2025

Proposed changes:

Where viewers exist for site, add 'viewer' to the user roles array so the WP Admin table can consume it.

E.g., user item:

            {
                "ID": 123,
                "login": "testuser",
                "email": false,
                "name": "testuser",
                "first_name": "",
                "last_name": "",
                "nice_name": "testuser",
                "URL": "http:\/\/testuser.xxx.com",
                "avatar_URL": "...",
                "profile_URL": "https:\/\/...",
                "ip_address": false,
                "site_ID": 321,
                "site_visible": true,
                "roles": [
                    "viewer"
                ]
            }

Furthermore, dedupe the return users list.

Why are these changes being made?

As part of Untangling Users, the API should return whether a WPCOM user has the viewer role in order that the WP Admin Users table can display correct labels. See 171239-ghe-Automattic/wpcom

Viewers that have been assigned a WordPress role, like Author, are not removed from the private blog table, and are therefore duplicated in both the found and users parameters.

This makes the found count inaccurate and also create duplicates in the users list.

  • If an existing Viewer’s role is changed for a site, e.g., they are assigned an Author/Editor/Admin role, remove their Viewer status by deleting the corresponding row in the custom table.

Testing instructions

Local tests

jetpack docker up
jetpack docker install
cd projects/plugins/jetpack
composer install
jetpack docker phpunit -- --filter=WP_Test_WPCOM_JSON_API_List_Users_Endpoint

WPCOM tests

173364-ghe-Automattic/wpcom

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?

@github-actions github-actions bot added [Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress labels Feb 11, 2025
Copy link
Contributor

github-actions bot commented Feb 11, 2025

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 a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ 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.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!


Jetpack plugin:

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

  • WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for none scheduled (scheduled code freeze on undefined).

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

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Feb 11, 2025
Copy link
Contributor

github-actions bot commented Feb 11, 2025

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 WoA dev 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 (Jetpack), and enable the try/add-viewer-to-user-roles-response branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack try/add-viewer-to-user-roles-response

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

@ramonjd ramonjd self-assigned this Feb 11, 2025
Copy link
Contributor

github-actions bot commented Feb 11, 2025

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/class.json-api.php 35/451 (7.76%) 0.89% -4 💚
projects/plugins/jetpack/class.json-api-endpoints.php 250/1258 (19.87%) 0.87% -11 💚
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-users-endpoint.php 57/156 (36.54%) 36.54% -43 💚

Full summary · PHP report · JS report

@ramonjd ramonjd force-pushed the try/add-viewer-to-user-roles-response branch from 8f989b7 to 74e3633 Compare February 12, 2025 02:15
@ramonjd ramonjd added the [Type] Feature Development of a new feature label Feb 12, 2025
@@ -0,0 +1,164 @@
<?php
/**
* Jetpack WPCOM JSON API `sites/%s/users` endpoint unit tests.
Copy link
Member Author

Choose a reason for hiding this comment

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

I added these basic tests because they weren't there.

Testing the viewer role might have to be done on WPCOM as it's gated behind the IS_WPCOM constant and calls internal methods, e.g., get_private_blog_users

@ramonjd ramonjd changed the title Users API endpoint: add 'view' to user roles array Users API endpoint: add 'viewer' role to user roles array Feb 13, 2025
},
$viewers
);
$combined_users = array_map(
Copy link
Member Author

Choose a reason for hiding this comment

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

This API does not dedupe users or the count where there are users with WordPress roles that were once viewers: they'll be included in the viewers list and merged as is.

See: #28317 (comment)

}

// Get viewers.
if ( $include_viewers ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I've refactored all this to test out grabbing, deduping and merging the user and viewer lists before we return arbitrary count values that are not accurate.

This would remove the need for frontend hacks like:

@ramonjd ramonjd changed the title Users API endpoint: add 'viewer' role to user roles array Users API endpoint: add 'viewer' role to user roles array and dedupe return value Feb 14, 2025
Copy link

jp-launch-control bot commented Feb 14, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-users-endpoint.php 0/149 (0.00%) 0.00% 7 💔

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

@ramonjd ramonjd force-pushed the try/add-viewer-to-user-roles-response branch from 31853ff to 298b8a0 Compare February 17, 2025 04:35
break;
}
}

return $return;
return $response;
Copy link
Member Author

@ramonjd ramonjd Feb 17, 2025

Choose a reason for hiding this comment

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

Something about these changes also screws with the /people/viewers/$wordpress_site/$user_id Calypso page. Check.

Copy link
Member Author

@ramonjd ramonjd Feb 27, 2025

Choose a reason for hiding this comment

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

It's this check: https://github.com/Automattic/wp-calypso/blob/trunk/client/my-sites/people/team-members/index.tsx#L114

It assumes that the roles array is empty for viewers.

It should be something like const type = user.roles?.length && user.roles?.includes( 'viewer' ) ? 'viewer' : 'email';

Copy link
Member Author

@ramonjd ramonjd Feb 27, 2025

Choose a reason for hiding this comment

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

Also watch out for double badges here:

https://github.com/Automattic/wp-calypso/blob/trunk/client/my-sites/people/people-profile/index.jsx#L320-L321

Screenshot 2025-02-27 at 5 46 05 pm

If this PR gets up, we can remove { type === 'viewer' && renderViewerRole() } because getRole() looks in the role array.

} else {
$viewer_count = count( $viewers );
}
$viewer_count = count( $viewers );
Copy link
Member Author

Choose a reason for hiding this comment

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

This won't be accurate due to legacy duplication. See: Automattic/wp-calypso#100674 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests [Type] Feature Development of a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant