diff --git a/lib/permissions/minimally-encoded-thread-permissions-validators.js b/lib/permissions/minimally-encoded-thread-permissions-validators.js --- a/lib/permissions/minimally-encoded-thread-permissions-validators.js +++ b/lib/permissions/minimally-encoded-thread-permissions-validators.js @@ -11,8 +11,8 @@ MinimallyEncodedMemberInfo, RawThreadInfo, RelativeMemberInfo, - MinimallyEncodedRoleInfo, MinimallyEncodedThreadCurrentUserInfo, + RoleInfo, ThreadInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { threadTypeValidator } from '../types/thread-types-enum.js'; @@ -26,8 +26,8 @@ import { threadEntityValidator } from '../utils/entity-text.js'; import { tBool, tID, tShape } from '../utils/validation-utils.js'; -const minimallyEncodedRoleInfoValidator: TInterface = - tShape({ +const minimallyEncodedRoleInfoValidator: TInterface = + tShape({ ...legacyRoleInfoValidator.meta.props, minimallyEncoded: tBool(true), permissions: t.list(tHexEncodedRolePermission), diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -59,9 +59,9 @@ } from '../types/message-types.js'; import type { RelativeMemberInfo, - MinimallyEncodedRoleInfo, RawThreadInfo, MinimallyEncodedThreadCurrentUserInfo, + RoleInfo, ThreadInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { @@ -362,7 +362,7 @@ makePermissionsBlob(permissions, null, threadID, threadType), threadID, ); - const role: MinimallyEncodedRoleInfo = minimallyEncodeRoleInfo({ + const role: RoleInfo = minimallyEncodeRoleInfo({ id: `${threadID}/role`, name: 'Members', permissions, @@ -1038,9 +1038,7 @@ return !!memberInfo.permissions[threadPermissions.CHANGE_ROLE]?.value; } -function roleIsAdminRole( - roleInfo: ?LegacyRoleInfo | ?MinimallyEncodedRoleInfo, -): boolean { +function roleIsAdminRole(roleInfo: ?LegacyRoleInfo | ?RoleInfo): boolean { return !!(roleInfo && !roleInfo.isDefault && roleInfo.name === 'Admins'); } 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 @@ -19,15 +19,13 @@ } from '../permissions/minimally-encoded-thread-permissions.js'; import type { ThreadEntity } from '../utils/entity-text.js'; -export type MinimallyEncodedRoleInfo = $ReadOnly<{ +export type RoleInfo = $ReadOnly<{ ...LegacyRoleInfo, +minimallyEncoded: true, +permissions: $ReadOnlyArray, }>; -const minimallyEncodeRoleInfo = ( - roleInfo: LegacyRoleInfo, -): MinimallyEncodedRoleInfo => { +const minimallyEncodeRoleInfo = (roleInfo: LegacyRoleInfo): RoleInfo => { invariant( !('minimallyEncoded' in roleInfo), 'roleInfo is already minimally encoded.', @@ -40,7 +38,7 @@ }; const decodeMinimallyEncodedRoleInfo = ( - minimallyEncodedRoleInfo: MinimallyEncodedRoleInfo, + minimallyEncodedRoleInfo: RoleInfo, ): LegacyRoleInfo => { const { minimallyEncoded, ...rest } = minimallyEncodedRoleInfo; return { @@ -125,7 +123,7 @@ ...LegacyRawThreadInfo, +minimallyEncoded: true, +members: $ReadOnlyArray, - +roles: { +[id: string]: MinimallyEncodedRoleInfo }, + +roles: { +[id: string]: RoleInfo }, +currentUser: MinimallyEncodedThreadCurrentUserInfo, }>; @@ -173,7 +171,7 @@ +containingThreadID: ?string, +community: ?string, +members: $ReadOnlyArray, - +roles: { +[id: string]: MinimallyEncodedRoleInfo }, + +roles: { +[id: string]: RoleInfo }, +currentUser: MinimallyEncodedThreadCurrentUserInfo, +sourceMessageID?: string, +repliesCount: number, diff --git a/lib/utils/role-utils.js b/lib/utils/role-utils.js --- a/lib/utils/role-utils.js +++ b/lib/utils/role-utils.js @@ -5,8 +5,8 @@ import { useSelector } from './redux-utils.js'; import { threadInfoSelector } from '../selectors/thread-selectors.js'; import type { - MinimallyEncodedRoleInfo, RelativeMemberInfo, + RoleInfo, ThreadInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { @@ -58,7 +58,7 @@ function useRolesFromCommunityThreadInfo( threadInfo: ThreadInfo, memberInfos: $ReadOnlyArray, -): $ReadOnlyMap { +): $ReadOnlyMap { // Our in-code system has chat-specific roles, while the // user-surfaced system has roles only for communities. We retrieve roles // from the top-level community thread for accuracy, with a rare fallback @@ -73,7 +73,7 @@ community ? threadInfoSelector(state)[community] : null, ); const topMostThreadInfo = communityThreadInfo || threadInfo; - const roleMap = new Map(); + const roleMap = new Map(); if (topMostThreadInfo.type === threadTypes.GENESIS) { memberInfos.forEach(memberInfo => 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 @@ -10,7 +10,7 @@ import type { MinimallyEncodedMemberInfo, RawThreadInfo, - MinimallyEncodedRoleInfo, + RoleInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import { decodeMinimallyEncodedRawThreadInfo, @@ -60,22 +60,20 @@ // 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; - }, - {}, + const minimallyEncodedRoles: { +[id: string]: RoleInfo } = Object.keys( + rawRoles, + ).reduce((acc: { [string]: RoleInfo }, 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; + }, {}); // 3. Validate and potentially minimally encode `rawCurrentUser`. const rawCurrentUser = JSON.parse(clientDBThreadInfo.currentUser);