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

Core Data: Stop sending duplicate requests in canUser resolver #43480

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,34 +273,44 @@ export const getEmbedPreview =
* Checks whether the current user can perform the given action on the given
* REST resource.
*
* @param {string} action Action to check. One of: 'create', 'read', 'update',
* 'delete'.
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
* @param {?string} id ID of the rest resource to check.
* @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
* 'delete'.
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
* @param {?string} id ID of the rest resource to check.
*/
export const canUser =
( action, resource, id ) =>
async ( { dispatch } ) => {
const methods = {
create: 'POST',
read: 'GET',
update: 'PUT',
delete: 'DELETE',
};
( requestedAction, resource, id ) =>
async ( { dispatch, registry } ) => {
const { hasStartedResolution } = registry.select( STORE_NAME );

const resourcePath = id ? `${ resource }/${ id }` : resource;
const retrievedActions = id
? [ 'create', 'read', 'update', 'delete' ]
: [ 'create', 'read' ];

const method = methods[ action ];
if ( ! method ) {
throw new Error( `'${ action }' is not a valid action.` );
if ( ! retrievedActions.includes( requestedAction ) ) {
throw new Error( `'${ requestedAction }' is not a valid action.` );
}

const path = id
? `/wp/v2/${ resource }/${ id }`
: `/wp/v2/${ resource }`;
// Prevent resolving the same resource twice.
for ( const relatedAction of retrievedActions ) {
if ( relatedAction === requestedAction ) {
continue;
}
const isAlreadyResolving = hasStartedResolution( 'canUser', [
relatedAction,
resource,
id,
] );
if ( isAlreadyResolving ) {
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be built into resolvers, instead of modifying a specific resolver? Or is that not possible?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, got it, you're matching similar resolvers.


let response;
try {
response = await apiFetch( {
path,
path: `/wp/v2/${ resourcePath }`,
method: 'OPTIONS',
parse: false,
} );
Expand All @@ -314,10 +324,25 @@ export const canUser =
// return the expected result in the native version. Instead, API requests
// only return the result, without including response properties like the headers.
const allowHeader = response.headers?.get( 'allow' );
const key = [ action, resource, id ].filter( Boolean ).join( '/' );
const isAllowed =
allowHeader?.includes?.( method ) || allowHeader?.allow === method;
dispatch.receiveUserPermission( key, isAllowed );
const allowedMethods = allowHeader?.allow || allowHeader || '';

const permissions = {};
const methods = {
create: 'POST',
read: 'GET',
update: 'PUT',
delete: 'DELETE',
};
for ( const [ actionName, methodName ] of Object.entries( methods ) ) {
permissions[ actionName ] = allowedMethods.includes( methodName );
}

for ( const action of retrievedActions ) {
dispatch.receiveUserPermission(
`${ action }/${ resourcePath }`,
permissions[ action ]
);
}
};

/**
Expand Down
84 changes: 80 additions & 4 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,13 @@ describe( 'getEmbedPreview', () => {
} );

describe( 'canUser', () => {
let registry;
beforeEach( async () => {
registry = {
select: jest.fn( () => ( {
hasStartedResolution: () => false,
} ) ),
};
triggerFetch.mockReset();
} );

Expand All @@ -304,7 +310,7 @@ describe( 'canUser', () => {
Promise.reject( { status: 404 } )
);

await canUser( 'create', 'media' )( { dispatch } );
await canUser( 'create', 'media' )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/media',
Expand All @@ -324,7 +330,7 @@ describe( 'canUser', () => {
headers: new Map( [ [ 'allow', 'GET' ] ] ),
} ) );

await canUser( 'create', 'media' )( { dispatch } );
await canUser( 'create', 'media' )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/media',
Expand All @@ -347,7 +353,7 @@ describe( 'canUser', () => {
headers: new Map( [ [ 'allow', 'POST, GET, PUT, DELETE' ] ] ),
} ) );

await canUser( 'create', 'media' )( { dispatch } );
await canUser( 'create', 'media' )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/media',
Expand All @@ -370,7 +376,7 @@ describe( 'canUser', () => {
headers: new Map( [ [ 'allow', 'POST, GET, PUT, DELETE' ] ] ),
} ) );

await canUser( 'create', 'blocks', 123 )( { dispatch } );
await canUser( 'create', 'blocks', 123 )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp/v2/blocks/123',
Expand All @@ -383,6 +389,76 @@ describe( 'canUser', () => {
true
);
} );

it( 'runs apiFetch only once per resource', async () => {
const dispatch = Object.assign( jest.fn(), {
receiveUserPermission: jest.fn(),
} );

registry = {
select: () => ( {
hasStartedResolution: ( _, [ action ] ) => action === 'read',
} ),
};

triggerFetch.mockImplementation( () => ( {
headers: new Map( [ [ 'allow', 'POST, GET' ] ] ),
} ) );

await canUser( 'create', 'blocks' )( { dispatch, registry } );
await canUser( 'read', 'blocks' )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/blocks',
true
);
} );

it( 'runs apiFetch only once per resource ID', async () => {
const dispatch = Object.assign( jest.fn(), {
receiveUserPermission: jest.fn(),
} );

registry = {
select: () => ( {
hasStartedResolution: ( _, [ action ] ) => action === 'create',
} ),
};

triggerFetch.mockImplementation( () => ( {
headers: new Map( [ [ 'allow', 'POST, GET, PUT, DELETE' ] ] ),
} ) );

await canUser( 'create', 'blocks', 123 )( { dispatch, registry } );
await canUser( 'read', 'blocks', 123 )( { dispatch, registry } );
await canUser( 'update', 'blocks', 123 )( { dispatch, registry } );
await canUser( 'delete', 'blocks', 123 )( { dispatch, registry } );

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'update/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'delete/blocks/123',
true
);
} );
} );

describe( 'getAutosaves', () => {
Expand Down