Page MenuHomePhabricator

[keyserver] add logic to update acknowledgment table
ClosedPublic

Authored by kamil on Oct 6 2022, 4:33 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:29 PM
Unknown Object (File)
Sun, Dec 15, 8:20 PM

Details

Summary

The function should be called after detecting login from a proper source.

Functionality:

  • a. if there is no record with the given user and policy it means that this is a new one - so It adds a new row
  • b. if there is a record with a given user and policy but confirmed is set to 0 it means that policy was updated, so it updated confirmed and date
  • c. in any other case do nothing
Test Plan

Check if described in Summary logic works for:

  1. new policy
  2. updated policy
  3. policy accepted by user previously

Diff Detail

Repository
rCOMM Comm
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

kamil held this revision as a draft.
kamil edited the test plan for this revision. (Show Details)
kamil added a reviewer: atul.
kamil published this revision for review.Oct 6 2022, 4:51 AM
ashoat requested changes to this revision.Oct 6 2022, 6:17 AM

Clean up spacing / indentation in queries please!

keyserver/src/updaters/user-acknowledgment-updater.js
1 ↗(On Diff #17382)

Newline after // @flow

20 ↗(On Diff #17382)

I think this would be cleaner with policies.map. What do you think?

23–41 ↗(On Diff #17382)

We can just use a single query with the ON DUPLICATE KEY construction

This revision now requires changes to proceed.Oct 6 2022, 6:17 AM
marcin added a reviewer: tomek.
keyserver/src/updaters/user-acknowledgment-updater.js
13 ↗(On Diff #17382)

We could ORDER BY policy here and sort policies table. Then we could just iterate over either policies table or userPolicies and use the same index to find corresponding entry in another table (no need to call find later). However I am not sure how much optimization is needed for this script or how much optimization would this change introduce. Therefore I am not going to block revision on this change, but please answer it before landing.

kamil edited the summary of this revision. (Show Details)

use ON DUPLICATE KEY, use map, add newline

keyserver/src/updaters/user-acknowledgment-updater.js
13 ↗(On Diff #17382)

We could ORDER BY policy here and sort policies table. Then we could just iterate over either policies table or userPolicies and use the same index to find corresponding entry in another table (no need to call find later). However I am not sure how much optimization is needed for this script or how much optimization would this change introduce. Therefore I am not going to block revision on this change, but please answer it before landing.

Thanks for the suggestion, I don't think we will gain a lot from this solution, I expect a lot of users (which means a lot of entries in policy_acknowledgments) and a relatively small number of policies, so I believe using find on a small array will be still faster than calling ORDER BY on the table with a lot of entries (the table do not have a separate index for policy and because it is using BTREE will be fast for sorting only by tuple (user, policy) and leftmost prefix).

tomek requested changes to this revision.Oct 12 2022, 5:40 AM
tomek added inline comments.
keyserver/src/updaters/user-acknowledgment-updater.js
22–24 ↗(On Diff #17509)

When !confirmed we're returning so maybe we can avoid selecting it in the first place? Also, we're interested only with policies from policies array - it also can be used in a select where condition.

27–32 ↗(On Diff #17509)

We don't expect having too many policies, so it's fine as it is, but there are some optimization which could be done if we had more of them:

  1. For every policy we have a separate query - it is expensive and we could use an insert with multiple values https://www.mariadbtutorial.com/mariadb-basics/mariadb-insert-multiple-rows/
  2. We're computing some things on JS side, but it feels like this whole operation can be made using a single query which selects and inserts... but that query would be a lot more complicated. Additional benefit would be that the query would be a single operation on the db, so we would avoid a scenario in which content of the table is updated between select and insert.
This revision now requires changes to proceed.Oct 12 2022, 5:40 AM
ashoat requested changes to this revision.Oct 12 2022, 8:18 AM
ashoat added inline comments.
keyserver/src/updaters/user-acknowledgment-updater.js
8 ↗(On Diff #17509)

Something I just realized here...

The idea of having separate kinds of policies in the table is meant to reflect policies appearing in different places in the app. The idea would be that we might have Terms when you log in, and maybe a separate set of Terms for some other feature.

For that reason I think we should require the caller here to specify a specific policy. This will also simplify the code.

Sorry I didn't call this out sooner!

27 ↗(On Diff #17509)

Can you apply the nits from D5310 here too?

rebase, force caller to specify policy type

kamil edited the test plan for this revision. (Show Details)
tomek added inline comments.
keyserver/src/updaters/user-acknowledgment-updater.js
8

We can be more precise with the name. In most of the places viewer updates some info about user: they might be the same person, but doesn't have to. To avoid confusion with other places, we can change function's name to indicate that it's viewer who gets updated. Other option is to change this method to allow updating any user, but that brings the question about permissions: who can update whom... I don't think it is a good idea to focus on this now.

This revision is now accepted and ready to land.Nov 17 2022, 9:40 AM