diff --git a/lib/types/minimally-encoded-thread-permissions-types.js b/lib/types/minimally-encoded-thread-permissions-types.js index 929540839..1f8f17b87 100644 --- a/lib/types/minimally-encoded-thread-permissions-types.js +++ b/lib/types/minimally-encoded-thread-permissions-types.js @@ -1,217 +1,243 @@ // @flow import invariant from 'invariant'; import _mapValues from 'lodash/fp/mapValues.js'; import type { LegacyMemberInfo, LegacyRawThreadInfo, LegacyRelativeMemberInfo, LegacyRoleInfo, ThreadCurrentUserInfo, LegacyThreadInfo, } from './thread-types.js'; import { decodeThreadRolePermissionsBitmaskArray, permissionsToBitmaskHex, threadPermissionsFromBitmaskHex, threadRolePermissionsBlobToBitmaskArray, } from '../permissions/minimally-encoded-thread-permissions.js'; export type MinimallyEncodedRoleInfo = $ReadOnly<{ ...LegacyRoleInfo, +minimallyEncoded: true, +permissions: $ReadOnlyArray, }>; const minimallyEncodeRoleInfo = ( roleInfo: LegacyRoleInfo, -): MinimallyEncodedRoleInfo => ({ - ...roleInfo, - minimallyEncoded: true, - permissions: threadRolePermissionsBlobToBitmaskArray(roleInfo.permissions), -}); +): MinimallyEncodedRoleInfo => { + invariant( + !('minimallyEncoded' in roleInfo), + 'roleInfo is already minimally encoded.', + ); + return { + ...roleInfo, + minimallyEncoded: true, + permissions: threadRolePermissionsBlobToBitmaskArray(roleInfo.permissions), + }; +}; const decodeMinimallyEncodedRoleInfo = ( minimallyEncodedRoleInfo: MinimallyEncodedRoleInfo, ): LegacyRoleInfo => { const { minimallyEncoded, ...rest } = minimallyEncodedRoleInfo; return { ...rest, permissions: decodeThreadRolePermissionsBitmaskArray( minimallyEncodedRoleInfo.permissions, ), }; }; export type MinimallyEncodedThreadCurrentUserInfo = $ReadOnly<{ ...ThreadCurrentUserInfo, +minimallyEncoded: true, +permissions: string, }>; const minimallyEncodeThreadCurrentUserInfo = ( threadCurrentUserInfo: ThreadCurrentUserInfo, ): MinimallyEncodedThreadCurrentUserInfo => { invariant( !('minimallyEncoded' in threadCurrentUserInfo), 'threadCurrentUserInfo is already minimally encoded.', ); return { ...threadCurrentUserInfo, minimallyEncoded: true, permissions: permissionsToBitmaskHex(threadCurrentUserInfo.permissions), }; }; const decodeMinimallyEncodedThreadCurrentUserInfo = ( minimallyEncodedThreadCurrentUserInfo: MinimallyEncodedThreadCurrentUserInfo, ): ThreadCurrentUserInfo => { const { minimallyEncoded, ...rest } = minimallyEncodedThreadCurrentUserInfo; return { ...rest, permissions: threadPermissionsFromBitmaskHex( minimallyEncodedThreadCurrentUserInfo.permissions, ), }; }; export type MinimallyEncodedMemberInfo = $ReadOnly<{ ...LegacyMemberInfo, +minimallyEncoded: true, +permissions: string, }>; const minimallyEncodeMemberInfo = ( memberInfo: LegacyMemberInfo, -): MinimallyEncodedMemberInfo => ({ - ...memberInfo, - minimallyEncoded: true, - permissions: permissionsToBitmaskHex(memberInfo.permissions), -}); +): MinimallyEncodedMemberInfo => { + invariant( + !('minimallyEncoded' in memberInfo), + 'memberInfo is already minimally encoded.', + ); + return { + ...memberInfo, + minimallyEncoded: true, + permissions: permissionsToBitmaskHex(memberInfo.permissions), + }; +}; const decodeMinimallyEncodedMemberInfo = ( minimallyEncodedMemberInfo: MinimallyEncodedMemberInfo, ): LegacyMemberInfo => { const { minimallyEncoded, ...rest } = minimallyEncodedMemberInfo; return { ...rest, permissions: threadPermissionsFromBitmaskHex( minimallyEncodedMemberInfo.permissions, ), }; }; export type MinimallyEncodedRelativeMemberInfo = $ReadOnly<{ ...MinimallyEncodedMemberInfo, +username: ?string, +isViewer: boolean, }>; const minimallyEncodeRelativeMemberInfo = ( relativeMemberInfo: LegacyRelativeMemberInfo, -): MinimallyEncodedRelativeMemberInfo => ({ - ...relativeMemberInfo, - minimallyEncoded: true, - permissions: permissionsToBitmaskHex(relativeMemberInfo.permissions), -}); +): MinimallyEncodedRelativeMemberInfo => { + invariant( + !('minimallyEncoded' in relativeMemberInfo), + 'relativeMemberInfo is already minimally encoded.', + ); + return { + ...relativeMemberInfo, + minimallyEncoded: true, + permissions: permissionsToBitmaskHex(relativeMemberInfo.permissions), + }; +}; const decodeMinimallyEncodedRelativeMemberInfo = ( minimallyEncodedRelativeMemberInfo: MinimallyEncodedRelativeMemberInfo, ): LegacyRelativeMemberInfo => { const { minimallyEncoded, ...rest } = minimallyEncodedRelativeMemberInfo; return { ...rest, permissions: threadPermissionsFromBitmaskHex( minimallyEncodedRelativeMemberInfo.permissions, ), }; }; export type MinimallyEncodedRawThreadInfo = $ReadOnly<{ ...LegacyRawThreadInfo, +minimallyEncoded: true, +members: $ReadOnlyArray, +roles: { +[id: string]: MinimallyEncodedRoleInfo }, +currentUser: MinimallyEncodedThreadCurrentUserInfo, }>; const minimallyEncodeRawThreadInfo = ( rawThreadInfo: LegacyRawThreadInfo, ): MinimallyEncodedRawThreadInfo => { + invariant( + !('minimallyEncoded' in rawThreadInfo), + 'rawThreadInfo is already minimally encoded.', + ); const { members, roles, currentUser, ...rest } = rawThreadInfo; return { ...rest, minimallyEncoded: true, members: members.map(minimallyEncodeMemberInfo), roles: _mapValues(minimallyEncodeRoleInfo)(roles), currentUser: minimallyEncodeThreadCurrentUserInfo(currentUser), }; }; const decodeMinimallyEncodedRawThreadInfo = ( minimallyEncodedRawThreadInfo: MinimallyEncodedRawThreadInfo, ): LegacyRawThreadInfo => { const { minimallyEncoded, members, roles, currentUser, ...rest } = minimallyEncodedRawThreadInfo; return { ...rest, members: members.map(decodeMinimallyEncodedMemberInfo), roles: _mapValues(decodeMinimallyEncodedRoleInfo)(roles), currentUser: decodeMinimallyEncodedThreadCurrentUserInfo(currentUser), }; }; export type MinimallyEncodedThreadInfo = $ReadOnly<{ ...LegacyThreadInfo, +minimallyEncoded: true, +members: $ReadOnlyArray, +roles: { +[id: string]: MinimallyEncodedRoleInfo }, +currentUser: MinimallyEncodedThreadCurrentUserInfo, }>; const minimallyEncodeThreadInfo = ( threadInfo: LegacyThreadInfo, ): MinimallyEncodedThreadInfo => { + invariant( + !('minimallyEncoded' in threadInfo), + 'threadInfo is already minimally encoded.', + ); 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, ): LegacyThreadInfo => { const { minimallyEncoded, members, roles, currentUser, ...rest } = minimallyEncodedThreadInfo; return { ...rest, members: members.map(decodeMinimallyEncodedRelativeMemberInfo), roles: _mapValues(decodeMinimallyEncodedRoleInfo)(roles), currentUser: decodeMinimallyEncodedThreadCurrentUserInfo(currentUser), }; }; export type MinimallyEncodedResolvedThreadInfo = $ReadOnly<{ ...MinimallyEncodedThreadInfo, +uiName: string, }>; export { minimallyEncodeRoleInfo, decodeMinimallyEncodedRoleInfo, minimallyEncodeThreadCurrentUserInfo, decodeMinimallyEncodedThreadCurrentUserInfo, minimallyEncodeMemberInfo, decodeMinimallyEncodedMemberInfo, minimallyEncodeRawThreadInfo, decodeMinimallyEncodedRawThreadInfo, minimallyEncodeRelativeMemberInfo, decodeMinimallyEncodedRelativeMemberInfo, minimallyEncodeThreadInfo, decodeMinimallyEncodedThreadInfo, };