Repository navigation
Use connected publication for RRM datapoints - #14124
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the Reader Revenue Manager module to automatically use the connected publication from the saved settings for CTA and publication-related actions and selectors, rather than passing publication identifiers (publicationID and organizationID) as parameters. On the JavaScript side, getCTAs, createCTA, getPublication, and updatePublication have been updated to no longer accept these identifiers, and new actions/selectors like clearCTAs, resetCTAs, getSavedPublicationID, and getSavedOrganizationID have been introduced. On the PHP side, a new Connected_Publication_Datapoint base class has been added to handle retrieving the connected publication settings, simplifying the Create_CTA, Get_CTAs, Get_Publication, and Update_Publication datapoints, as well as the synchronization crons. All corresponding tests and stories have been updated to align with these API changes. I have no further feedback to provide as no review comments were submitted.
|
Size Change: 0 B Total Size: 3.55 MB ℹ️ View Unchanged
|
JakePT
left a comment
There was a problem hiding this comment.
Thanks @nfmohit! It looks like there's some potential issues with clearCTAs() and possible conditions where the CTA list can be incorrect or for the wrong publication.
They don't seem likely to occur in practice, but the issues look like they can be avoided if we keep CTAs keyed by publication ID internally in state even though we're not accepting the parameter anymore. That could eliminate the need for separate clearCTAs() and resetCTAs() methods and a strict order in which things are called. It could be even more robust if we return the publicationID that the server used with the response.
I had a 🤖 summarise the cases and solution in more detail:
CTA state can go stale or get stuck when the publication changes
getCTAs() holds a single list with no record of which publication it belongs to. Two calls keep it in sync: clearCTAs() in selectPublication() and resetCTAs() in submitChanges() (settings.js:208-214). Three cases slip through:
- The list gets stuck empty. With A saved and its CTAs loaded, select B, then select A again (or cancel the edit).
clearCTAs()emptied the list but left thegetCTAsresolver marked as finished. The publication ID no longer differs from the saved one, soresetCTAs()never runs, andgetCTAs()returnsundefineduntil the page reloads. - The previous publication's CTAs can come back. If
getCTAshadn't resolved beforeclearCTAs(), the next read fetches from the server. The server answers for the saved publication, so the list holds the previous publication's CTAs, despite what theclearCTAs()docblock says. - A late response can overwrite the list.
RESET_CTASdoesn't cancel a request that's already running, so a response that arrives after the reset writes the previous publication's CTAs back into the store.
Suggestion: key the CTAs by saved publication ID internally
Store ctas as Record< publicationID, CTA[] >, filled by an internal selector and resolver keyed by the saved publication ID. Keep getCTAs() without arguments. It should return undefined while the selected publication differs from the saved one, and the saved publication's list otherwise.
This covers all three cases:
- Selecting the saved publication again shows its list.
- Each response is stored under the publication it was requested for.
- After a save, the first read fetches the new publication's CTAs without any manual invalidation.
clearCTAs(), the resetCTAs() call in submitChanges() and the comparison in selectPublication() can then be removed.
Optional: return the publication ID from GET:ctas
A narrow race remains. A save that lands while the request is running means the server answers for the new publication, but the client stores the result under the old one. CTAs only identify their publication through name, so an empty list can't be identified. Returning { publicationID, ctas } from the datapoint closes this gap. Resolving the publication once in Get_CTAs would also stop parse_response() from syncing one publication's CTAs into another's configuredCTAs.
|
Thanks @JakePT! The CTAs are keyed by publication ID again. I took the optional suggestion too. An empty list has no CTA names to read the publication from.
|
JakePT
left a comment
There was a problem hiding this comment.
Thanks @nfmohit! It looks like we might still have a couple of gaps. I think the PHP-side one will need to be covered but the other suggestion that would let us get rid of resetCTAs is probably not as important. Let me know what you think.
| $publication = $this->get_connected_publication(); | ||
|
|
There was a problem hiding this comment.
@nfmohit I think we still have an issue here where this connected publication may not be the same one that was used in create_request() if the publication had been changed during the request.
It looks like similar issues have been solved elsewhere by returning a closure from create_request(), which is what Batch_Search_Analytics seems to do. If we did that we would just need to avoid calling synchronize() if the publication from create_request() is no longer the connected publication at the time of parse_response().
| it( 'should clear the CTAs when a different publication is selected', async () => { | ||
| // Reading the cleared CTAs starts their resolver, which | ||
| // requests the saved publication's CTAs. |
There was a problem hiding this comment.
@nfmohit I don't think we're clearing CTAs anymore so this is probably better described as "should not return the CTAs while a different publication is selected"
| it( 'should keep the CTAs when the connected publication is selected again', async () => { | ||
| connectPublicationWithCTAs(); | ||
|
|
||
| await registry | ||
| .dispatch( MODULES_READER_REVENUE_MANAGER ) | ||
| .selectPublication( { | ||
| publicationId: 'publication-1', | ||
| onboardingState: 'ONBOARDING_COMPLETE', | ||
| } ); | ||
|
|
||
| expect( | ||
| registry.select( MODULES_READER_REVENUE_MANAGER ).getCTAs() | ||
| ).toEqual( [ cta ] ); |
There was a problem hiding this comment.
@nfmohit I think we should also cover that switching to another publication and back should return the first publication's CTAs again.
JakePT
left a comment
There was a problem hiding this comment.
@nfmohit It looks like we still have some small gaps, but I wanted to check your feelings on them before I send them back for execution since they're not necessarily blockers and I know we're going long on this already.
The issue is that getPublicationCTAs is treated as resolved for the requested publication ID even if it responds for a different publication than we expected because the saved publication ID was different on the server by the time the request was received. If I save the publication as ABC123, getCTAs() will fetch getPublicationCTAs( 'ABC123' ) but if the saved publication changes to DEF456 before the server builds the request it will respond with the CTAs for DEF456. The latest changes mean that the returned CTAs will be correctly filed against DEF456, which is good, but getPublicationCTAs will still be treated as resolved for 'ABC123'. So if I change the publication back to ABC123 then getCTAs will return undefined and not attempt to fetch the CTAs again until the page is reloaded.
It seems like it's very unlikely that this will cause problems in practice, because it would require another user or tab to be saving settings at the same time, but it's still a flaw with how the datastore is structured. I can't help but feel that the choice to always use the server's saved publication is the problem. Fundamentally what we're fighting is that with this approach we are making requests assuming a certain publication ID but we actually have no idea what publication ID the server is going to respond for, so the datastore needs to account for this uncertainty, which complicates things.
It seems like things would be far cleaner on the JavaScript side if we went the opposite direction to the AC and always required the parameters, and updated the Datapoints to require them. Then we have a far cleaner 1:1 relationship between what we think we're fetching and what we're receiving that avoids all of these issues.
A smaller fix for now would be to call invalidateResolutionForStoreSelector() for getPublicationCTAs inside submitChanges(), just like we currently do with getPublication. It still leaves us the problem of depending on certain call orders etc. that I've been trying to avoid with my feedback, but it solves the practical issue for now in a way that's consistent with other selectors for the time being.
Let me know what you think.
Thanks, and sorry for dragging this out!
ccb6ad5 to
582d952
Compare
nfmohit
left a comment
There was a problem hiding this comment.
Thanks for your feedback, @JakePT!
We went with your suggestion of requiring the parameters. The datapoints now require organizationID and publicationID, and the data store reads them from the settings before each request, so callers still pass nothing. @aaemnnosttv agreed to it, and the acceptance criteria on #13429 are updated.
That closes the case you found. The request says which publication it wants and the server responds for that one, and the resolution is keyed on the same identifiers, so changing the publication and changing back both work without any invalidation.
clearCTAs(), resetCTAs(), the { publicationID, ctas } response shape, the property on the PHP datapoint that recorded which publication a request used, and the guard comparing the selected publication against the saved one are all gone.
Your two test comments from the last round are in, though in ctas.test.ts now, since getCTAs is what they cover.
The branch was reset to develop and force-pushed, so the history is new. I've also folded in publicationsByID, because without it getPublication() writes the publication it fetched into the publications list and the connect-publication step then offers only the connected publication. That and the keyed selectors are why the diff grew to +1614/-1030 from +1012/-934.
Thanks for pushing on it.
JakePT
left a comment
There was a problem hiding this comment.
Thanks @nfmohit! Looking good! I just flagged a couple of pretty minor things that need attention.
Also, the QAB is using resolveSelect with getPublication and getCTAs, which don't have resolvers anymore. Can you update the QAB to use select() and cover that they may need to be called twice to see the results of a fetch? Alternatively we could add thin resolvers for each so they can be used with resolveSelect.
| switch ( type ) { | ||
| case 'RESET_PUBLICATIONS': | ||
| state.publications = baseInitialState.publications; | ||
| state.publicationsByID = baseInitialState.publicationsByID; |
There was a problem hiding this comment.
@nfmohit I don't think we want to wipe publicationsByID here. resetPublications() only invalidates getPublications(), so wiping publicationsByID here means getPublicationByID stays resolved and won't re-fetch.
I think we can simply remove this line as there's no harm in keeping the publications indexed by ID when resetting publications because it's only used for looking up the selected publication, which is reset by refreshPublication().
There was a problem hiding this comment.
Good catch, removed, thanks!
| } ); | ||
| } ); | ||
|
|
||
| describe( 'getCTAs', () => { |
There was a problem hiding this comment.
@nfmohit Can we add an equivalent to the 'should associate a publication fetch error with the request identifiers' test that getPublication() has?
| $this->synchronization = new CTA_Synchronization( $this->settings ); | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
@nfmohit Nit: An extra line break snuck in here.
There was a problem hiding this comment.
🦅 👁️ Removed, thanks!
| ).toEqual( [ otherCTA ] ); | ||
| } ); | ||
|
|
||
| it( 'should not return the CTAs while a different publication is selected', async () => { |
There was a problem hiding this comment.
@nfmohit Nit: This wording is a bit misleading. I think what is actually being tested is "should return undefined after selecting a publication that needs to load its CTAs" or similar.
There was a problem hiding this comment.
Renamed to should return undefined after selecting a publication whose CTAs are not loaded, thanks!
|
I've addressed the comments and updated the QAB, thanks @JakePT ! |
Summary
Related issue(s):
Relevant technical choices
The datapoints require
organizationIDandpublicationID, and the data store reads them from the module's settings before each request, so callers pass nothing.Settings saved before express setup hold a publication ID without an organization ID.
getPublicationParams()reads the organization from the publications list in that case.getPublication()andgetCTAs()take no arguments and read through keyed selectors that carry the resolvers, so changing the publication requests the new one's data on its own. There are inline comments on both.Publications fetched one at a time are stored under their ID, separately from the publications list. Before,
getPublication()wrote the publication it fetched into the list, sogetPublications()saw a loaded list of one and never requested the real one, and the connect-publication step offered only the connected publication.The branch was reset to develop and force-pushed, so the history is new. The earlier commits are kept at the
archive/13429-connected-onlytag.Deviations from IB
Missing_Required_Setting_Exceptionis deleted. A missing identifier is a missing parameter now.Synchronization::get_datapoint_data()is kept. The IB has it deleted. The crons pass the identifiers the datapoints now require, soSynchronization.phpis unchanged.submitChanges()no longer touches the CTAs or the publication. Keyed resolution requests the new publication's data on its own.SynchronizationTest. The datapoint tests cover that each request carries the identifiers, andCronTestcovers that a cron fires it.PR Author Checklist
Do not alter or remove anything below. The following sections will be managed by moderators only.
Code Reviewer Checklist
Merge Reviewer Checklist