diff --git a/lib/utils/thread-ops-utils.js b/lib/utils/thread-ops-utils.js --- a/lib/utils/thread-ops-utils.js +++ b/lib/utils/thread-ops-utils.js @@ -1,10 +1,20 @@ // @flow -import type { MinimallyEncodedRawThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; -import { decodeMinimallyEncodedRawThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; +import invariant from 'invariant'; + +import { minimallyEncodedMemberInfoValidator } from '../permissions/minimally-encoded-thread-permissions-validators.js'; +import type { + MinimallyEncodedMemberInfo, + MinimallyEncodedRawThreadInfo, +} from '../types/minimally-encoded-thread-permissions-types.js'; +import { + decodeMinimallyEncodedRawThreadInfo, + minimallyEncodeMemberInfo, +} from '../types/minimally-encoded-thread-permissions-types.js'; import { assertThreadType } from '../types/thread-types-enum.js'; import { type ClientDBThreadInfo, + legacyMemberInfoValidator, type LegacyRawThreadInfo, type RawThreadInfo, } from '../types/thread-types.js'; @@ -26,6 +36,22 @@ function convertClientDBThreadInfoToRawThreadInfo( clientDBThreadInfo: ClientDBThreadInfo, ): MinimallyEncodedRawThreadInfo { + // 1. Validate and potentially minimally encode `rawMembers`. + const rawMembers = JSON.parse(clientDBThreadInfo.members); + rawMembers.forEach(rawMember => + invariant( + minimallyEncodedMemberInfoValidator.is(rawMember) || + legacyMemberInfoValidator.is(rawMember), + 'rawMember must be correctly formed [MinimallyEncoded/Legacy]MemberInfo', + ), + ); + const minimallyEncodedMembers: $ReadOnlyArray = + rawMembers.map(rawMember => + rawMember.minimallyEncoded + ? rawMember + : minimallyEncodeMemberInfo(rawMember), + ); + let rawThreadInfo: MinimallyEncodedRawThreadInfo = { minimallyEncoded: true, id: clientDBThreadInfo.id, @@ -37,7 +63,7 @@ parentThreadID: clientDBThreadInfo.parentThreadID, containingThreadID: clientDBThreadInfo.containingThreadID, community: clientDBThreadInfo.community, - members: JSON.parse(clientDBThreadInfo.members), + members: minimallyEncodedMembers, roles: JSON.parse(clientDBThreadInfo.roles), currentUser: JSON.parse(clientDBThreadInfo.currentUser), repliesCount: clientDBThreadInfo.repliesCount,