Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/session/cookies.js
Show First 20 Lines • Show All 800 Lines • ▼ Show 20 Lines | ) { | ||||
const query = SQL` | const query = SQL` | ||||
UPDATE cookies SET signed_identity_keys = ${JSON.stringify( | UPDATE cookies SET signed_identity_keys = ${JSON.stringify( | ||||
signedIdentityKeysBlob, | signedIdentityKeysBlob, | ||||
)} WHERE id = ${cookieID} | )} WHERE id = ${cookieID} | ||||
`; | `; | ||||
await dbQuery(query); | await dbQuery(query); | ||||
} | } | ||||
// Returns `true` if row with `id = cookieID` exists AND | |||||
// `signed_identity_keys` is `NULL`. Otherwise, returns `false`. | |||||
async function isCookieMissingSignedIdentityKeysBlob( | |||||
cookieID: string, | |||||
): Promise<boolean> { | |||||
const query = SQL` | |||||
SELECT signed_identity_keys | |||||
FROM cookies | |||||
WHERE id = ${cookieID} | |||||
`; | |||||
const [queryResult] = await dbQuery(query); | |||||
if ( | |||||
queryResult.length === 1 && | |||||
queryResult[0].signed_identity_keys === null | |||||
) { | |||||
return true; | |||||
} | |||||
return false; | |||||
ashoat: I'm not a fan of `if () return true else return false` constructions. This sort of construction… | |||||
atulAuthorUnsubmitted Done Inline ActionsPersonally prefer being explicit instead of the casting trick... but I'll update this diff. atul: Personally prefer being explicit instead of the casting trick... but I'll update this diff. | |||||
atulAuthorUnsubmitted Done Inline Actions
It's totally impossible for more than one row to have the same id so I think it's fine to check === 1 return !!( queryResult.length === 1 && queryResult[0].signed_identity_keys === null ); Shouldn't this just be return ( queryResult.length === 1 && queryResult[0].signed_identity_keys === null ); atul: > Separately, I might consider either making the comparison > 0 or factoring out the check that… | |||||
ashoatUnsubmitted Not Done Inline ActionsYeah ashoat: Yeah | |||||
} | |||||
async function setCookiePlatform( | async function setCookiePlatform( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
platform: Platform, | platform: Platform, | ||||
): Promise<void> { | ): Promise<void> { | ||||
const newPlatformDetails = { ...viewer.platformDetails, platform }; | const newPlatformDetails = { ...viewer.platformDetails, platform }; | ||||
viewer.setPlatformDetails(newPlatformDetails); | viewer.setPlatformDetails(newPlatformDetails); | ||||
const query = SQL` | const query = SQL` | ||||
UPDATE cookies | UPDATE cookies | ||||
Show All 32 Lines | export { | ||||
fetchViewerForSocket, | fetchViewerForSocket, | ||||
createNewAnonymousCookie, | createNewAnonymousCookie, | ||||
createNewUserCookie, | createNewUserCookie, | ||||
setNewSession, | setNewSession, | ||||
extendCookieLifespan, | extendCookieLifespan, | ||||
addCookieToJSONResponse, | addCookieToJSONResponse, | ||||
addCookieToHomeResponse, | addCookieToHomeResponse, | ||||
setCookieSignedIdentityKeysBlob, | setCookieSignedIdentityKeysBlob, | ||||
isCookieMissingSignedIdentityKeysBlob, | |||||
setCookiePlatform, | setCookiePlatform, | ||||
setCookiePlatformDetails, | setCookiePlatformDetails, | ||||
}; | }; |
I'm not a fan of if () return true else return false constructions. This sort of construction can always be rewritten:
Separately, I might consider either making the comparison > 0 or factoring out the check that there aren't 2 or more results into an invariant