diff --git a/lib/permissions/minimally-encoded-thread-permissions-test-data.js b/lib/permissions/minimally-encoded-thread-permissions-test-data.js new file mode 100644 --- /dev/null +++ b/lib/permissions/minimally-encoded-thread-permissions-test-data.js @@ -0,0 +1,356 @@ +// @flow + +import type { MinimallyEncodedRawThreadInfo } from './minimally-encoded-thread-permissions.js'; +import { threadTypes } from '../types/thread-types-enum.js'; +import type { RawThreadInfo } from '../types/thread-types.js'; + +const exampleRawThreadInfoA: RawThreadInfo = { + id: '85171', + type: threadTypes.PERSONAL, + name: '', + description: '', + color: '6d49ab', + creationTime: 1675887298557, + parentThreadID: '1', + members: [ + { + id: '256', + role: null, + permissions: { + know_of: { + value: true, + source: '1', + }, + visible: { + value: true, + source: '1', + }, + voiced: { + value: true, + source: '1', + }, + edit_entries: { + value: true, + source: '1', + }, + edit_thread: { + value: true, + source: '1', + }, + edit_thread_description: { + value: true, + source: '1', + }, + edit_thread_color: { + value: true, + source: '1', + }, + delete_thread: { + value: true, + source: '1', + }, + create_subthreads: { + value: true, + source: '1', + }, + create_sidebars: { + value: true, + source: '1', + }, + join_thread: { + value: true, + source: '1', + }, + edit_permissions: { + value: true, + source: '1', + }, + add_members: { + value: true, + source: '1', + }, + remove_members: { + value: true, + source: '1', + }, + change_role: { + value: true, + source: '1', + }, + leave_thread: { + value: false, + source: null, + }, + react_to_message: { + value: false, + source: null, + }, + edit_message: { + value: false, + source: null, + }, + manage_pins: { + value: true, + source: '1', + }, + }, + isSender: false, + }, + { + id: '83853', + role: '85172', + permissions: { + know_of: { + value: true, + source: '85171', + }, + visible: { + value: true, + source: '85171', + }, + voiced: { + value: true, + source: '85171', + }, + edit_entries: { + value: true, + source: '85171', + }, + edit_thread: { + value: true, + source: '85171', + }, + edit_thread_description: { + value: true, + source: '85171', + }, + edit_thread_color: { + value: true, + source: '85171', + }, + delete_thread: { + value: false, + source: null, + }, + create_subthreads: { + value: false, + source: null, + }, + create_sidebars: { + value: true, + source: '85171', + }, + 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: true, + source: '85171', + }, + edit_message: { + value: true, + source: '85171', + }, + manage_pins: { + value: false, + source: null, + }, + }, + isSender: true, + }, + ], + roles: { + '85172': { + id: '85172', + name: 'Members', + permissions: { + know_of: true, + visible: true, + voiced: true, + react_to_message: true, + edit_message: true, + edit_entries: true, + edit_thread: true, + edit_thread_color: true, + edit_thread_description: true, + create_sidebars: true, + descendant_open_know_of: true, + descendant_open_visible: true, + child_open_join_thread: true, + }, + isDefault: true, + }, + }, + currentUser: { + role: '85172', + permissions: { + know_of: { + value: true, + source: '85171', + }, + visible: { + value: true, + source: '85171', + }, + voiced: { + value: true, + source: '85171', + }, + edit_entries: { + value: true, + source: '85171', + }, + edit_thread: { + value: true, + source: '85171', + }, + edit_thread_description: { + value: true, + source: '85171', + }, + edit_thread_color: { + value: true, + source: '85171', + }, + delete_thread: { + value: false, + source: null, + }, + create_subthreads: { + value: false, + source: null, + }, + create_sidebars: { + value: true, + source: '85171', + }, + 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: true, + source: '85171', + }, + edit_message: { + value: true, + source: '85171', + }, + manage_pins: { + value: false, + source: null, + }, + }, + subscription: { + home: true, + pushNotifs: true, + }, + unread: false, + }, + repliesCount: 0, + containingThreadID: '1', + community: '1', + pinnedCount: 0, +}; + +const exampleMinimallyEncodedRawThreadInfoA: MinimallyEncodedRawThreadInfo = { + 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, +}; + +export { exampleRawThreadInfoA, exampleMinimallyEncodedRawThreadInfoA }; 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 @@ -1,6 +1,7 @@ // @flow import invariant from 'invariant'; +import _mapValues from 'lodash/fp/mapValues.js'; import t, { type TInterface } from 'tcomb'; import { parseThreadPermissionString } from './prefixes.js'; @@ -206,6 +207,13 @@ permissions: t.list(tHexEncodedRolePermission), }); +const minimallyEncodeRoleInfo = ( + roleInfo: RoleInfo, +): MinimallyEncodedRoleInfo => ({ + ...roleInfo, + permissions: threadRolePermissionsBlobToBitmaskArray(roleInfo.permissions), +}); + export type MinimallyEncodedThreadCurrentUserInfo = { ...ThreadCurrentUserInfo, +permissions: string, @@ -218,6 +226,13 @@ permissions: tHexEncodedPermissionsBitmask, }); +const minimallyEncodeThreadCurrentUserInfo = ( + threadCurrentUserInfo: ThreadCurrentUserInfo, +): MinimallyEncodedThreadCurrentUserInfo => ({ + ...threadCurrentUserInfo, + permissions: permissionsToBitmaskHex(threadCurrentUserInfo.permissions), +}); + export type MinimallyEncodedMemberInfo = { ...MemberInfo, +permissions: string, @@ -229,6 +244,13 @@ permissions: tHexEncodedPermissionsBitmask, }); +const minimallyEncodeMemberInfo = ( + memberInfo: MemberInfo, +): MinimallyEncodedMemberInfo => ({ + ...memberInfo, + permissions: permissionsToBitmaskHex(memberInfo.permissions), +}); + export type MinimallyEncodedRawThreadInfo = { ...RawThreadInfo, +members: $ReadOnlyArray, @@ -244,6 +266,18 @@ currentUser: minimallyEncodedThreadCurrentUserInfoValidator, }); +const minimallyEncodeRawThreadInfo = ( + rawThreadInfo: RawThreadInfo, +): MinimallyEncodedRawThreadInfo => { + const { members, roles, currentUser, ...rest } = rawThreadInfo; + return { + ...rest, + members: members.map(minimallyEncodeMemberInfo), + roles: _mapValues(minimallyEncodeRoleInfo)(roles), + currentUser: minimallyEncodeThreadCurrentUserInfo(currentUser), + }; +}; + export { permissionsToBitmaskHex, hasPermission, @@ -255,4 +289,5 @@ minimallyEncodedThreadCurrentUserInfoValidator, minimallyEncodedMemberInfoValidator, minimallyEncodedRawThreadInfoValidator, + minimallyEncodeRawThreadInfo, }; 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 @@ -1,5 +1,9 @@ // @flow +import { + exampleMinimallyEncodedRawThreadInfoA, + exampleRawThreadInfoA, +} from './minimally-encoded-thread-permissions-test-data.js'; import { decodeRolePermissionBitmask, decodeThreadRolePermissionsBitmaskArray, @@ -8,12 +12,12 @@ minimallyEncodedRawThreadInfoValidator, minimallyEncodedRoleInfoValidator, minimallyEncodedThreadCurrentUserInfoValidator, + minimallyEncodeRawThreadInfo, 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 = { @@ -366,68 +370,21 @@ }); 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), + minimallyEncodedRawThreadInfoValidator.is( + exampleMinimallyEncodedRawThreadInfoA, + ), + ).toBe(true); + }); +}); + +describe('minimallyEncodeRawThreadInfo', () => { + it('should correctly encode RawThreadInfo', () => { + expect( + minimallyEncodedRawThreadInfoValidator.is( + minimallyEncodeRawThreadInfo(exampleRawThreadInfoA), + ), ).toBe(true); }); });