diff --git a/lib/types/minimally-encoded-thread-permissions-types.js b/lib/types/minimally-encoded-thread-permissions-types.js --- a/lib/types/minimally-encoded-thread-permissions-types.js +++ b/lib/types/minimally-encoded-thread-permissions-types.js @@ -9,6 +9,7 @@ rawThreadInfoValidator, roleInfoValidator, threadCurrentUserInfoValidator, + threadInfoValidator, } from './thread-types.js'; import type { MemberInfo, @@ -16,6 +17,7 @@ RelativeMemberInfo, RoleInfo, ThreadCurrentUserInfo, + ThreadInfo, } from './thread-types.js'; import { decodeThreadRolePermissionsBitmaskArray, @@ -202,6 +204,49 @@ }; }; +export type MinimallyEncodedThreadInfo = $ReadOnly<{ + ...ThreadInfo, + +minimallyEncoded: true, + +members: $ReadOnlyArray, + +roles: { +[id: string]: MinimallyEncodedRoleInfo }, + +currentUser: MinimallyEncodedThreadCurrentUserInfo, +}>; + +const minimallyEncodedThreadInfoValidator: TInterface = + tShape({ + ...threadInfoValidator.meta.props, + minimallyEncoded: tBool(true), + members: t.list(minimallyEncodedRelativeMemberInfoValidator), + roles: t.dict(tID, minimallyEncodedRoleInfoValidator), + currentUser: minimallyEncodedThreadCurrentUserInfoValidator, + }); + +const minimallyEncodeThreadInfo = ( + threadInfo: ThreadInfo, +): MinimallyEncodedThreadInfo => { + const { members, roles, currentUser, ...rest } = threadInfo; + return { + ...rest, + minimallyEncoded: true, + members: members.map(minimallyEncodeRelativeMemberInfo), + roles: _mapValues(minimallyEncodeRoleInfo)(roles), + currentUser: minimallyEncodeThreadCurrentUserInfo(currentUser), + }; +}; + +const decodeMinimallyEncodedThreadInfo = ( + minimallyEncodedThreadInfo: MinimallyEncodedThreadInfo, +): ThreadInfo => { + const { minimallyEncoded, members, roles, currentUser, ...rest } = + minimallyEncodedThreadInfo; + return { + ...rest, + members: members.map(decodeMinimallyEncodedRelativeMemberInfo), + roles: _mapValues(decodeMinimallyEncodedRoleInfo)(roles), + currentUser: decodeMinimallyEncodedThreadCurrentUserInfo(currentUser), + }; +}; + export { minimallyEncodedRoleInfoValidator, minimallyEncodeRoleInfo, @@ -218,4 +263,7 @@ minimallyEncodedRelativeMemberInfoValidator, minimallyEncodeRelativeMemberInfo, decodeMinimallyEncodedRelativeMemberInfo, + minimallyEncodedThreadInfoValidator, + minimallyEncodeThreadInfo, + decodeMinimallyEncodedThreadInfo, };