diff --git a/lib/permissions/minimally-encoded-thread-permissions.js b/lib/permissions/minimally-encoded-thread-permissions.js --- a/lib/permissions/minimally-encoded-thread-permissions.js +++ b/lib/permissions/minimally-encoded-thread-permissions.js @@ -75,12 +75,12 @@ return bitmask.toString(16); }; -const tHexEncodedRolePermission: TRegex = tRegex(/^[0-9a-fA-F]{3,}$/); +const tHexEncodedPermissionsBitmask: TRegex = tRegex(/^[0-9a-fA-F]+$/); const threadPermissionsFromBitmaskHex = ( permissionsBitmaskHex: string, ): ThreadPermissionsInfo => { invariant( - tHexEncodedRolePermission.is(permissionsBitmaskHex), + tHexEncodedPermissionsBitmask.is(permissionsBitmaskHex), 'permissionsBitmaskHex must be valid hex string.', ); @@ -166,7 +166,7 @@ const inverseFilterPrefixes: Map = invertObjectToMap(filterPrefixes); -const tHexEncodedPermissionsBitmask: TRegex = tRegex(/^[0-9a-fA-F]+$/); +const tHexEncodedRolePermission: TRegex = tRegex(/^[0-9a-fA-F]{3,}$/); const decodeRolePermissionBitmask = (bitmask: string): string => { const bitmaskInt = BigInt(`0x${bitmask}`); const basePermission = (bitmaskInt >> BigInt(4)) & BigInt(63); diff --git a/lib/permissions/minimally-encoded-thread-permissions.test.js b/lib/permissions/minimally-encoded-thread-permissions.test.js --- a/lib/permissions/minimally-encoded-thread-permissions.test.js +++ b/lib/permissions/minimally-encoded-thread-permissions.test.js @@ -138,6 +138,38 @@ expectedDecodedThreadPermissions, ); }); + + it('should decode bitmask strings under 3 characters', () => { + // We know that '3' in hex is 0b0011. Given that permissions are encoded + // from least significant bit (LSB) to most significant bit (MSB), we would + // except this to mean that only the first two permissions listed in + // `baseRolePermissionEncoding` are `true`. Which is the case. + const decodedThreadPermissions = threadPermissionsFromBitmaskHex('3'); + expect(decodedThreadPermissions).toStrictEqual({ + know_of: { value: true, source: 'null' }, + visible: { value: true, source: 'null' }, + voiced: { value: false, source: null }, + edit_entries: { value: false, source: null }, + edit_thread: { value: false, source: null }, + edit_thread_description: { value: false, source: null }, + edit_thread_color: { value: false, source: null }, + delete_thread: { value: false, source: null }, + create_subthreads: { value: false, source: null }, + create_sidebars: { value: false, source: null }, + join_thread: { value: false, source: null }, + edit_permissions: { value: false, source: null }, + add_members: { value: false, source: null }, + remove_members: { value: false, source: null }, + change_role: { value: false, source: null }, + leave_thread: { value: false, source: null }, + react_to_message: { value: false, source: null }, + edit_message: { value: false, source: null }, + edit_thread_avatar: { value: false, source: null }, + manage_pins: { value: false, source: null }, + manage_invite_links: { value: false, source: null }, + voiced_in_announcement_channels: { value: false, source: null }, + }); + }); }); describe('rolePermissionToBitmaskHex', () => {