diff --git a/lib/permissions/special-roles.js b/lib/permissions/special-roles.js index 383d3785d..ffdeb2c47 100644 --- a/lib/permissions/special-roles.js +++ b/lib/permissions/special-roles.js @@ -1,60 +1,68 @@ // @flow import _mapValues from 'lodash/fp/mapValues.js'; import type { TRefinement } from 'tcomb'; import { roleIsAdminRole, roleIsDefaultRole } from '../shared/thread-utils.js'; import type { RawThreadInfo, RoleInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; import type { RawThreadInfos } from '../types/thread-types.js'; import { values } from '../utils/objects.js'; import { tNumEnum } from '../utils/validation-utils.js'; export const specialRoles = Object.freeze({ DEFAULT_ROLE: 1, ADMIN_ROLE: 2, }); export type SpecialRole = $Values; export const specialRoleValidator: TRefinement = tNumEnum( values(specialRoles), ); export const defaultSpecialRoles = Object.freeze({ Members: specialRoles.DEFAULT_ROLE, Admins: specialRoles.ADMIN_ROLE, }); function patchRoleInfoWithSpecialRole(role: RoleInfo): RoleInfo { + // We removed the `.isDefault` field from `RoleInfo`, but persisted + // `RoleInfo`s will still have the field until this migration. + // $FlowIgnore[prop-missing] + const { isDefault, ...roleSansIsDefault } = role; if (roleIsDefaultRole(role)) { return { - ...role, + ...roleSansIsDefault, specialRole: specialRoles.DEFAULT_ROLE, }; } else if (roleIsAdminRole(role)) { return { - ...role, + ...roleSansIsDefault, specialRole: specialRoles.ADMIN_ROLE, }; + } else { + return { + ...roleSansIsDefault, + specialRole: null, + }; } - return role; } function patchRawThreadInfoWithSpecialRole( rawThreadInfo: RawThreadInfo, ): RawThreadInfo { return { ...rawThreadInfo, roles: _mapValues(patchRoleInfoWithSpecialRole)(rawThreadInfo.roles), }; } function patchRawThreadInfosWithSpecialRole( rawThreadInfos: RawThreadInfos, ): RawThreadInfos { return _mapValues(patchRawThreadInfoWithSpecialRole)(rawThreadInfos); } export { patchRoleInfoWithSpecialRole, patchRawThreadInfosWithSpecialRole }; diff --git a/lib/permissions/special-roles.test.js b/lib/permissions/special-roles.test.js index 7942953f2..e2b6106a3 100644 --- a/lib/permissions/special-roles.test.js +++ b/lib/permissions/special-roles.test.js @@ -1,223 +1,223 @@ // @flow import { patchRawThreadInfosWithSpecialRole, patchRoleInfoWithSpecialRole, specialRoles, } from './special-roles.js'; import type { RoleInfo } from '../types/minimally-encoded-thread-permissions-types.js'; import { threadTypes } from '../types/thread-types-enum.js'; import type { RawThreadInfos } from '../types/thread-types.js'; describe('patchRoleInfoWithSpecialRole', () => { it('should correctly set DEFAULT_ROLE', () => { const role: RoleInfo = { minimallyEncoded: true, id: 'roleID', name: 'roleName', permissions: ['abc', 'def'], specialRole: specialRoles.DEFAULT_ROLE, }; const patchedRole = patchRoleInfoWithSpecialRole(role); expect(patchedRole.specialRole).toBe(specialRoles.DEFAULT_ROLE); }); it('should correctly set ADMIN_ROLE', () => { const role: RoleInfo = { minimallyEncoded: true, id: 'roleID', name: 'Admins', permissions: ['abc', 'def'], }; const patchedRole = patchRoleInfoWithSpecialRole(role); expect(patchedRole.specialRole).toBe(specialRoles.ADMIN_ROLE); }); it('should correctly set undefined', () => { const role: RoleInfo = { minimallyEncoded: true, id: 'roleID', name: 'BLAH', permissions: ['abc', 'def'], }; const patchedRole = patchRoleInfoWithSpecialRole(role); - expect(patchedRole.specialRole).toBe(undefined); + expect(patchedRole.specialRole).toBe(null); }); }); const rawThreadInfos: RawThreadInfos = { '256|1': { minimallyEncoded: true, id: '256|1', type: threadTypes.GENESIS, name: 'GENESIS', description: 'This is the first community on Comm. In the future it will be possible to create chats outside of a community, but for now all of these chats get set with GENESIS as their parent. GENESIS is hosted on Ashoat’s keyserver.', color: 'c85000', creationTime: 1702415956354, parentThreadID: null, containingThreadID: null, community: null, members: [ { id: '256', role: '256|83796', permissions: '3f73ff', isSender: false, minimallyEncoded: true, }, { id: '83809', role: '256|83795', permissions: '3', isSender: false, minimallyEncoded: true, }, ], roles: { '256|83795': { id: '256|83795', name: 'Members', permissions: ['000', '010', '005', '015', '0a7'], minimallyEncoded: true, specialRole: specialRoles.DEFAULT_ROLE, }, '256|83796': { id: '256|83796', name: 'Admins', permissions: [ '000', '010', '020', '100', '110', '030', '040', '060', '050', '120', '080', '090', '0c0', '070', '0d0', '0e0', '130', '140', '150', '004', '014', '0a6', '0a8', '024', '034', '044', '064', '054', '124', '086', '096', '0c4', '074', '0b4', '0d4', '0e4', '134', '156', ], minimallyEncoded: true, }, }, currentUser: { role: '256|83795', permissions: '3', subscription: { home: true, pushNotifs: true, }, unread: false, minimallyEncoded: true, }, repliesCount: 0, pinnedCount: 0, }, '256|83814': { id: '256|83814', type: threadTypes.PRIVATE, name: '', description: 'This is your private chat, where you can set reminders and jot notes in private!', color: 'aa4b4b', creationTime: 1702415964471, parentThreadID: '256|1', repliesCount: 0, containingThreadID: '256|1', community: '256|1', pinnedCount: 0, minimallyEncoded: true, members: [ { id: '256', role: null, permissions: '2c7fff', isSender: false, minimallyEncoded: true, }, { id: '83809', role: '256|83815', permissions: '3026f', isSender: true, minimallyEncoded: true, }, ], roles: { '256|83815': { id: '256|83815', name: 'NotMembers', permissions: [ '000', '010', '020', '100', '110', '060', '050', '090', '030', '005', '015', '0a9', ], minimallyEncoded: true, }, }, currentUser: { role: '256|83815', permissions: '3026f', subscription: { home: true, pushNotifs: true, }, unread: false, minimallyEncoded: true, }, }, }; describe('patchRawThreadInfosWithSpecialRole', () => { it('should correctly set special roles', () => { const patchedRawThreadInfos = patchRawThreadInfosWithSpecialRole(rawThreadInfos); expect(patchedRawThreadInfos['256|1'].roles['256|83795'].specialRole).toBe( specialRoles.DEFAULT_ROLE, ); expect(patchedRawThreadInfos['256|1'].roles['256|83796'].specialRole).toBe( specialRoles.ADMIN_ROLE, ); expect( patchedRawThreadInfos['256|83814'].roles['256|83815'].specialRole, - ).toBe(undefined); + ).toBe(null); }); });