Skip to content

Commit

Permalink
[lib] Update permissionsToBitmask to permissionsToBitmaskHex
Browse files Browse the repository at this point in the history
Summary:
Summmary:
Was initially planning (and drafting) to encode the `BigInt` as `base64` to be included in stringified JSON. However, as I jotted down ([ENG-5373 : Use "bitmask" approach to encoding ThreadPermissions and Role permissions blobs](https://linear.app/comm/issue/ENG-5373/use-bitmask-approach-to-encoding-threadpermissions-and-role)), decided to encode as hex instead.

Update `permissionsToBitmask` and corresponding `hasPermission` to return and accept `string` instead of `bigint`.

Test Plan: Unit test, will add a few more.

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: tomek

Subscribers: wyilio

Differential Revision: https://phab.comm.dev/D9589
  • Loading branch information
atulsmadhugiri committed Oct 25, 2023
1 parent 433111d commit 5ea56c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions lib/permissions/minimally-encoded-thread-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const minimallyEncodedThreadPermissions = Object.freeze({
manage_invite_links: BigInt(1) << BigInt(20),
});

// TODO (atul): Update flow to `194.0.0` for bigint support
// $FlowIssue bigint-unsupported
const permissionsToBitmask = (permissions: ThreadPermissionsInfo): bigint => {
const permissionsToBitmaskHex = (
permissions: ThreadPermissionsInfo,
): string => {
let bitmask = BigInt(0);
for (const [key, permission] of entries(permissions)) {
if (permission.value && key in minimallyEncodedThreadPermissions) {
Expand All @@ -49,15 +49,14 @@ const permissionsToBitmask = (permissions: ThreadPermissionsInfo): bigint => {
bitmask |= minimallyEncodedThreadPermissions[key];
}
}
return bitmask;
return bitmask.toString(16);
};

const hasPermission = (
// TODO (atul): Update flow to `194.0.0` for bigint support
// $FlowIssue bigint-unsupported
permissionsBitmask: bigint,
permissionsBitmaskHex: string,
permission: ThreadPermission,
): boolean => {
const permissionsBitmask = BigInt(`0x${permissionsBitmaskHex}`);
if (!(permission in minimallyEncodedThreadPermissions)) {
return false;
}
Expand All @@ -73,6 +72,6 @@ const hasPermission = (

export {
minimallyEncodedThreadPermissions,
permissionsToBitmask,
permissionsToBitmaskHex,
hasPermission,
};
6 changes: 3 additions & 3 deletions lib/permissions/minimally-encoded-thread-permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {
hasPermission,
permissionsToBitmask,
permissionsToBitmaskHex,
} from './minimally-encoded-thread-permissions.js';

describe('minimallyEncodedThreadPermissions', () => {
Expand All @@ -28,8 +28,8 @@ describe('minimallyEncodedThreadPermissions', () => {
};

it('should encode ThreadPermissionsInfo as bitmask', () => {
const permissionsBitmask = permissionsToBitmask(permissions);
expect(permissionsBitmask.toString(2)).toBe('110111001111111111');
const permissionsBitmask = permissionsToBitmaskHex(permissions);
expect(permissionsBitmask).toBe('373ff');
expect(hasPermission(permissionsBitmask, 'know_of')).toBe(true);
expect(hasPermission(permissionsBitmask, 'visible')).toBe(true);
expect(hasPermission(permissionsBitmask, 'voiced')).toBe(true);
Expand Down

0 comments on commit 5ea56c5

Please sign in to comment.