Skip to content

Commit

Permalink
[lib] Introduce decodeRolePermissionBitmask
Browse files Browse the repository at this point in the history
Summary:
To reverse `rolePermissionToBitmaskHex` introduced in D9662.

---

Depends on D9662

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

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: ashoat

Subscribers: wyilio

Differential Revision: https://phab.comm.dev/D9663
  • Loading branch information
atulsmadhugiri committed Nov 2, 2023
1 parent 4846f86 commit 445632b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
47 changes: 46 additions & 1 deletion lib/permissions/minimally-encoded-thread-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,49 @@ const rolePermissionToBitmaskHex = (threadRolePermission: string): string => {
return bitmask.toString(16).padStart(3, '0');
};

export { permissionsToBitmaskHex, hasPermission, rolePermissionToBitmaskHex };
const inverseBaseRolePermissionEncoding = new Map(
Object.entries(baseRolePermissionEncoding).map(([key, value]) => [
value,
key,
]),
);

// $FlowIssue bigint-unsupported
const inversePropagationPrefixes: Map<bigint, string> = new Map(
Object.entries(propagationPrefixes).map(([key, value]) => [value, key]),
);
// $FlowIssue bigint-unsupported
const inverseFilterPrefixes: Map<bigint, string> = new Map(
Object.entries(filterPrefixes).map(([key, value]) => [value, key]),
);
const decodeRolePermissionBitmask = (bitmask: string): string => {
const bitmaskInt = BigInt(`0x${bitmask}`);
const basePermission = (bitmaskInt >> BigInt(4)) & BigInt(63);
const propagationPrefix = (bitmaskInt >> BigInt(2)) & BigInt(3);
const filterPrefix = bitmaskInt & BigInt(3);

const basePermissionString =
inverseBaseRolePermissionEncoding.get(basePermission);
const propagationPrefixString =
inversePropagationPrefixes.get(propagationPrefix) ?? '';
const filterPrefixString = inverseFilterPrefixes.get(filterPrefix) ?? '';

invariant(
basePermissionString !== null &&
basePermissionString !== undefined &&
propagationPrefixString !== null &&
propagationPrefixString !== undefined &&
filterPrefixString !== null &&
filterPrefixString !== undefined,
'invalid bitmask',
);

return `${propagationPrefixString}${filterPrefixString}${basePermissionString}`;
};

export {
permissionsToBitmaskHex,
hasPermission,
rolePermissionToBitmaskHex,
decodeRolePermissionBitmask,
};
37 changes: 37 additions & 0 deletions lib/permissions/minimally-encoded-thread-permissions.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import {
decodeRolePermissionBitmask,
hasPermission,
permissionsToBitmaskHex,
rolePermissionToBitmaskHex,
Expand Down Expand Up @@ -88,3 +89,39 @@ describe('rolePermissionToBitmaskHex', () => {
expect(rolePermissionToBitmaskHex('child_know_of')).toBe('008');
});
});

describe('decodeRolePermissionBitmask', () => {
it('should decode `01b` to `child_opentoplevel_visible` successfully', () => {
expect(decodeRolePermissionBitmask('01b')).toBe(
'child_opentoplevel_visible',
);
});

it('should decode `00b` to `child_opentoplevel_know_of` successfully', () => {
expect(decodeRolePermissionBitmask('00b')).toBe(
'child_opentoplevel_know_of',
);
});

it('should decode `01a` to `child_toplevel_visible` successfully', () => {
expect(decodeRolePermissionBitmask('01a')).toBe('child_toplevel_visible');
});

it('should decode `00a` to `child_toplevel_know_of` successfully', () => {
expect(decodeRolePermissionBitmask('00a')).toBe('child_toplevel_know_of');
});

it('should decode `0ab` to `child_opentoplevel_join_thread` successfully', () => {
expect(decodeRolePermissionBitmask('0ab')).toBe(
'child_opentoplevel_join_thread',
);
});

it('should decode `018` to `child_visible` successfully', () => {
expect(decodeRolePermissionBitmask('018')).toBe('child_visible');
});

it('should decode `008` to `child_know_of` successfully', () => {
expect(decodeRolePermissionBitmask('008')).toBe('child_know_of');
});
});

0 comments on commit 445632b

Please sign in to comment.