Skip to content

Commit

Permalink
[lib] Introduce MinimallyEncodedRawThreadInfo and validator
Browse files Browse the repository at this point in the history
Summary:
`RawThreadInfo` type w/ minimally encoded fields.

Next diffs:
- Higher level utilities for translating back/forth from `RawThreadInfo` <=> `MinimallyEncodedRawThreadInfo`
- Native refactoring + migrations
- Web refactoring
- Flipping the switch

---

Depends on D9735

Test Plan: Single unit test, will be tested implicitly in subsequent diffs as well.

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: tomek

Subscribers: wyilio

Differential Revision: https://phab.comm.dev/D9736
  • Loading branch information
atulsmadhugiri committed Nov 7, 2023
1 parent b67b7d2 commit 611517f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/permissions/minimally-encoded-thread-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import type {
} from '../types/thread-permission-types.js';
import type {
MemberInfo,
RawThreadInfo,
RoleInfo,
ThreadCurrentUserInfo,
} from '../types/thread-types.js';
import {
memberInfoValidator,
rawThreadInfoValidator,
roleInfoValidator,
threadCurrentUserInfoValidator,
} from '../types/thread-types.js';
import { entries, invertObjectToMap } from '../utils/objects.js';
import { tRegex, tShape } from '../utils/validation-utils.js';
import { tID, tRegex, tShape } from '../utils/validation-utils.js';
import type { TRegex } from '../utils/validation-utils.js';

// `baseRolePermissionEncoding` maps permission names to indices.
Expand Down Expand Up @@ -227,6 +229,21 @@ const minimallyEncodedMemberInfoValidator: TInterface<MinimallyEncodedMemberInfo
permissions: tHexEncodedPermissionsBitmask,
});

export type MinimallyEncodedRawThreadInfo = {
...RawThreadInfo,
+members: $ReadOnlyArray<MinimallyEncodedMemberInfo>,
+roles: { +[id: string]: MinimallyEncodedRoleInfo },
+currentUser: MinimallyEncodedThreadCurrentUserInfo,
};

const minimallyEncodedRawThreadInfoValidator: TInterface<MinimallyEncodedRawThreadInfo> =
tShape<MinimallyEncodedRawThreadInfo>({
...rawThreadInfoValidator.meta.props,
members: t.list(minimallyEncodedMemberInfoValidator),
roles: t.dict(tID, minimallyEncodedRoleInfoValidator),
currentUser: minimallyEncodedThreadCurrentUserInfoValidator,
});

export {
permissionsToBitmaskHex,
hasPermission,
Expand All @@ -237,4 +254,5 @@ export {
minimallyEncodedRoleInfoValidator,
minimallyEncodedThreadCurrentUserInfoValidator,
minimallyEncodedMemberInfoValidator,
minimallyEncodedRawThreadInfoValidator,
};
69 changes: 69 additions & 0 deletions lib/permissions/minimally-encoded-thread-permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
decodeThreadRolePermissionsBitmaskArray,
hasPermission,
minimallyEncodedMemberInfoValidator,
minimallyEncodedRawThreadInfoValidator,
minimallyEncodedRoleInfoValidator,
minimallyEncodedThreadCurrentUserInfoValidator,
permissionsToBitmaskHex,
rolePermissionToBitmaskHex,
threadRolePermissionsBlobToBitmaskArray,
} from './minimally-encoded-thread-permissions.js';
import type { ThreadRolePermissionsBlob } from '../types/thread-permission-types.js';
import { threadTypes } from '../types/thread-types-enum.js';

describe('minimallyEncodedThreadPermissions', () => {
const permissions = {
Expand Down Expand Up @@ -362,3 +364,70 @@ describe('minimallyEncodedMemberInfoValidator', () => {
).toBe(false);
});
});

describe('minimallyEncodedRawThreadInfoValidator', () => {
const minimalRawThreadInfo = {
id: '85171',
type: threadTypes.PERSONAL,
name: '',
description: '',
color: '6d49ab',
creationTime: 1675887298557,
parentThreadID: '1',
members: [
{
id: '256',
role: null,
permissions: '87fff',
isSender: false,
},
{
id: '83853',
role: '85172',
permissions: '3027f',
isSender: true,
},
],
roles: {
'85172': {
id: '85172',
name: 'Members',
permissions: [
'000',
'010',
'020',
'100',
'110',
'030',
'040',
'060',
'050',
'090',
'005',
'015',
'0a9',
],
isDefault: true,
},
},
currentUser: {
role: '85172',
permissions: '3027f',
subscription: {
home: true,
pushNotifs: true,
},
unread: false,
},
repliesCount: 0,
containingThreadID: '1',
community: '1',
pinnedCount: 0,
};

it('should validate correctly formed MinimallyEncodedRawThreadInfo', () => {
expect(
minimallyEncodedRawThreadInfoValidator.is(minimalRawThreadInfo),
).toBe(true);
});
});

0 comments on commit 611517f

Please sign in to comment.