Skip to content

Commit

Permalink
[lib] Update convertClientDBThreadInfoToRawThreadInfo to validate `…
Browse files Browse the repository at this point in the history
…ThreadCurrentUserInfo`

Summary:
Similar to D10313 and D10298, this diff addresses what we learned when testing D10297 that it's important that we validate the stringified JSON that we retrieve from `ClientDBThreadInfo`. Specifically, it's possible for `ClientDBThreadInfo` to have either stringified `MinimallyEncodedThreadCurrentInfo` or `ThreadCurrentInfo` (yes, we didn't end up renaming this to `LegacyThreadCurrentInfo` because we didn't end up needing a union type, but I will change the name for symmetry) stored in the `currentUser` field, and we should A. validate that the data is correctly formed and B. determine what format the data is in.

---

Depends on D10313

Test Plan: Will be tested along with changes in D10297 and D10298 and D10313.

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: ashoat

Differential Revision: https://phab.comm.dev/D10314
  • Loading branch information
atulsmadhugiri committed Dec 13, 2023
1 parent 4407654 commit 7b262ca
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/utils/thread-ops-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import invariant from 'invariant';
import {
minimallyEncodedMemberInfoValidator,
minimallyEncodedRoleInfoValidator,
minimallyEncodedThreadCurrentUserInfoValidator,
} from '../permissions/minimally-encoded-thread-permissions-validators.js';
import type {
MinimallyEncodedMemberInfo,
Expand All @@ -15,6 +16,7 @@ import {
decodeMinimallyEncodedRawThreadInfo,
minimallyEncodeMemberInfo,
minimallyEncodeRoleInfo,
minimallyEncodeThreadCurrentUserInfo,
} from '../types/minimally-encoded-thread-permissions-types.js';
import { assertThreadType } from '../types/thread-types-enum.js';
import {
Expand All @@ -23,6 +25,7 @@ import {
type LegacyRawThreadInfo,
legacyRoleInfoValidator,
type RawThreadInfo,
threadCurrentUserInfoValidator,
} from '../types/thread-types.js';

function convertRawThreadInfoToClientDBThreadInfo(
Expand Down Expand Up @@ -75,6 +78,17 @@ function convertClientDBThreadInfoToRawThreadInfo(
{},
);

// 3. Validate and potentially minimally encode `rawCurrentUser`.
const rawCurrentUser = JSON.parse(clientDBThreadInfo.currentUser);
invariant(
minimallyEncodedThreadCurrentUserInfoValidator.is(rawCurrentUser) ||
threadCurrentUserInfoValidator.is(rawCurrentUser),
'rawCurrentUser must be valid [MinimallyEncoded]ThreadCurrentUserInfo',
);
const minimallyEncodedCurrentUser = rawCurrentUser.minimallyEncoded
? rawCurrentUser
: minimallyEncodeThreadCurrentUserInfo(rawCurrentUser);

let rawThreadInfo: MinimallyEncodedRawThreadInfo = {
minimallyEncoded: true,
id: clientDBThreadInfo.id,
Expand All @@ -88,7 +102,7 @@ function convertClientDBThreadInfoToRawThreadInfo(
community: clientDBThreadInfo.community,
members: minimallyEncodedMembers,
roles: minimallyEncodedRoles,
currentUser: JSON.parse(clientDBThreadInfo.currentUser),
currentUser: minimallyEncodedCurrentUser,
repliesCount: clientDBThreadInfo.repliesCount,
pinnedCount: clientDBThreadInfo.pinnedCount,
};
Expand Down

0 comments on commit 7b262ca

Please sign in to comment.