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 @@ -2,20 +2,26 @@ import invariant from 'invariant'; -import { minimallyEncodedMemberInfoValidator } from '../permissions/minimally-encoded-thread-permissions-validators.js'; +import { + minimallyEncodedMemberInfoValidator, + minimallyEncodedRoleInfoValidator, +} from '../permissions/minimally-encoded-thread-permissions-validators.js'; import type { MinimallyEncodedMemberInfo, MinimallyEncodedRawThreadInfo, + MinimallyEncodedRoleInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { decodeMinimallyEncodedRawThreadInfo, minimallyEncodeMemberInfo, + minimallyEncodeRoleInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { assertThreadType } from '../types/thread-types-enum.js'; import { type ClientDBThreadInfo, legacyMemberInfoValidator, type LegacyRawThreadInfo, + legacyRoleInfoValidator, type RawThreadInfo, } from '../types/thread-types.js'; @@ -50,6 +56,25 @@ : minimallyEncodeMemberInfo(rawMember); }); + // 2. Validate and potentially minimally encode `rawRoles`. + const rawRoles = JSON.parse(clientDBThreadInfo.roles); + const minimallyEncodedRoles: { +[id: string]: MinimallyEncodedRoleInfo } = + Object.keys(rawRoles).reduce( + (acc: { [string]: MinimallyEncodedRoleInfo }, roleID: string) => { + const roleInfo = rawRoles[roleID]; + invariant( + minimallyEncodedRoleInfoValidator.is(roleInfo) || + legacyRoleInfoValidator.is(roleInfo), + 'rawRole must be valid [MinimallyEncoded/Legacy]RoleInfo', + ); + acc[roleID] = roleInfo.minimallyEncoded + ? roleInfo + : minimallyEncodeRoleInfo(roleInfo); + return acc; + }, + {}, + ); + let rawThreadInfo: MinimallyEncodedRawThreadInfo = { minimallyEncoded: true, id: clientDBThreadInfo.id, @@ -62,7 +87,7 @@ containingThreadID: clientDBThreadInfo.containingThreadID, community: clientDBThreadInfo.community, members: minimallyEncodedMembers, - roles: JSON.parse(clientDBThreadInfo.roles), + roles: minimallyEncodedRoles, currentUser: JSON.parse(clientDBThreadInfo.currentUser), repliesCount: clientDBThreadInfo.repliesCount, pinnedCount: clientDBThreadInfo.pinnedCount,