Skip to content

Commit

Permalink
[lib] Introduce MinimallyEncodedMemberInfo and validator
Browse files Browse the repository at this point in the history
Summary:
`MemberInfo` type w/ `permissions` as string instread of `ThreadPermissions`.

Next diffs:
- Introduce `MinimallyEncodedRawThreadInfo`
- Higher level utilities for translating back/forth from `RawThreadInfo` <=> `MinimallyEncodedRawThreadInfo`. Found this to be cleaner API than encoding/decoding `RawThreadInfo.currentUser.permissions` and `RawThreadInfo.members[memberID].permissions` "manually" a bunch of different places.
- Native refactoring + migrations
- Web refactoring
- Flipping the switch

---

Depends on D9734

Test Plan: Unit tests, will be tested implicitly in subsequent diffs as well.

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: ashoat

Subscribers: wyilio

Differential Revision: https://phab.comm.dev/D9735
  • Loading branch information
atulsmadhugiri committed Nov 6, 2023
1 parent dc24c82 commit 8485f33
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/permissions/minimally-encoded-thread-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import type {
ThreadPermissionsInfo,
ThreadRolePermissionsBlob,
} from '../types/thread-permission-types.js';
import type { RoleInfo, ThreadCurrentUserInfo } from '../types/thread-types.js';
import type {
MemberInfo,
RoleInfo,
ThreadCurrentUserInfo,
} from '../types/thread-types.js';
import {
memberInfoValidator,
roleInfoValidator,
threadCurrentUserInfoValidator,
} from '../types/thread-types.js';
Expand Down Expand Up @@ -211,6 +216,17 @@ const minimallyEncodedThreadCurrentUserInfoValidator: TInterface<MinimallyEncode
permissions: tHexEncodedPermissionsBitmask,
});

export type MinimallyEncodedMemberInfo = {
...MemberInfo,
+permissions: string,
};

const minimallyEncodedMemberInfoValidator: TInterface<MinimallyEncodedMemberInfo> =
tShape<MinimallyEncodedMemberInfo>({
...memberInfoValidator.meta.props,
permissions: tHexEncodedPermissionsBitmask,
});

export {
permissionsToBitmaskHex,
hasPermission,
Expand All @@ -220,4 +236,5 @@ export {
decodeThreadRolePermissionsBitmaskArray,
minimallyEncodedRoleInfoValidator,
minimallyEncodedThreadCurrentUserInfoValidator,
minimallyEncodedMemberInfoValidator,
};
39 changes: 39 additions & 0 deletions lib/permissions/minimally-encoded-thread-permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
decodeRolePermissionBitmask,
decodeThreadRolePermissionsBitmaskArray,
hasPermission,
minimallyEncodedMemberInfoValidator,
minimallyEncodedRoleInfoValidator,
minimallyEncodedThreadCurrentUserInfoValidator,
permissionsToBitmaskHex,
Expand Down Expand Up @@ -323,3 +324,41 @@ describe('minimallyEncodedThreadCurrentUserInfoValidator', () => {
).toBe(false);
});
});

describe('minimallyEncodedMemberInfoValidator', () => {
it('should validate correctly formed MinimallyEncodedMemberInfo', () => {
expect(
minimallyEncodedMemberInfoValidator.is({
id: 'memberID',
permissions: 'ABCDEF',
isSender: true,
}),
).toBe(true);

expect(
minimallyEncodedMemberInfoValidator.is({
id: 'memberID',
permissions: '01b',
isSender: false,
}),
).toBe(true);
});

it('should NOT validate malformed MinimallyEncodedMemberInfo', () => {
expect(
minimallyEncodedMemberInfoValidator.is({
id: 'memberID',
permissions: 'INVALID',
isSender: false,
}),
).toBe(false);

expect(
minimallyEncodedMemberInfoValidator.is({
id: 'memberID',
permissions: 100,
isSender: false,
}),
).toBe(false);
});
});
2 changes: 1 addition & 1 deletion lib/types/thread-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type MemberInfo = {
+permissions: ThreadPermissionsInfo,
+isSender: boolean,
};
const memberInfoValidator = tShape<MemberInfo>({
export const memberInfoValidator: TInterface<MemberInfo> = tShape<MemberInfo>({
id: t.String,
role: t.maybe(tID),
permissions: threadPermissionsInfoValidator,
Expand Down

0 comments on commit 8485f33

Please sign in to comment.