diff --git a/lib/types/relationship-types.js b/lib/types/relationship-types.js index 35e3b670f..308639c33 100644 --- a/lib/types/relationship-types.js +++ b/lib/types/relationship-types.js @@ -1,96 +1,113 @@ // @flow -import type { TRefinement } from 'tcomb'; +import type { TInterface, TRefinement } from 'tcomb'; +import t from 'tcomb'; import type { AccountUserInfo } from './user-types.js'; import { values } from '../utils/objects.js'; -import { tNumEnum } from '../utils/validation-utils.js'; +import { tNumEnum, tShape, tString } from '../utils/validation-utils.js'; export const undirectedStatus = Object.freeze({ KNOW_OF: 0, FRIEND: 2, }); export type UndirectedStatus = $Values; export const directedStatus = Object.freeze({ PENDING_FRIEND: 1, BLOCKED: 3, }); export type DirectedStatus = $Values; export const userRelationshipStatus = Object.freeze({ REQUEST_SENT: 1, REQUEST_RECEIVED: 2, FRIEND: 3, BLOCKED_BY_VIEWER: 4, BLOCKED_VIEWER: 5, BOTH_BLOCKED: 6, }); export type UserRelationshipStatus = $Values; export const userRelationshipStatusValidator: TRefinement = tNumEnum( values(userRelationshipStatus), ); const relationshipActionsSansFarcaster = Object.freeze({ FRIEND: 'friend', UNFRIEND: 'unfriend', BLOCK: 'block', UNBLOCK: 'unblock', }); const farcasterRelationshipActions = Object.freeze({ FARCASTER_MUTUAL: 'farcaster', }); export const relationshipActions = Object.freeze({ ...relationshipActionsSansFarcaster, ...farcasterRelationshipActions, }); export type RelationshipAction = $Values; export const relationshipActionsList: $ReadOnlyArray = values(relationshipActions); export type RelationshipActionSansFarcaster = $Values< typeof relationshipActionsSansFarcaster, >; export const relationshipButtons = Object.freeze({ FRIEND: 'friend', UNFRIEND: 'unfriend', BLOCK: 'block', UNBLOCK: 'unblock', ACCEPT: 'accept', WITHDRAW: 'withdraw', REJECT: 'reject', }); export type RelationshipButton = $Values; export type RelationshipRequest = { - action: RelationshipAction, - userIDs: $ReadOnlyArray, + +action: RelationshipAction, + +userIDs: $ReadOnlyArray, }; +export type FarcasterRelationshipRequest = { + +action: 'farcaster', + +userIDsToFID: { +[userID: string]: string }, +}; + +const exactlyTwoDictEntriesValidator = t.refinement( + t.dict(t.String, t.String), + dict => Object.keys(dict).length === 2, +); + +export const updateFarcasterRelationshipInputValidator: TInterface = + tShape({ + action: tString('farcaster'), + userIDsToFID: exactlyTwoDictEntriesValidator, + }); + type SharedRelationshipRow = { user1: string, user2: string, }; export type DirectedRelationshipRow = { ...SharedRelationshipRow, status: DirectedStatus, }; export type UndirectedRelationshipRow = { ...SharedRelationshipRow, status: UndirectedStatus, }; export type RelationshipErrors = Partial<{ invalid_user: string[], already_friends: string[], user_blocked: string[], }>; export type UserRelationships = { +friends: $ReadOnlyArray, +blocked: $ReadOnlyArray, }; diff --git a/lib/types/relationship-types.test.js b/lib/types/relationship-types.test.js new file mode 100644 index 000000000..bd22a6915 --- /dev/null +++ b/lib/types/relationship-types.test.js @@ -0,0 +1,49 @@ +// @flow + +import { updateFarcasterRelationshipInputValidator } from './relationship-types.js'; + +describe('updateFarcasterRelationshipInputValidator', () => { + test('SHOULD validate input with exactly 2 userIDsToFID entries', () => { + const input = { + action: 'farcaster', + userIDsToFID: { + '256': 'f256', + '512': 'f512', + }, + }; + expect(updateFarcasterRelationshipInputValidator.is(input)).toBe(true); + }); + + test('SHOULD NOT validate input with > 2 userIDsToFID entries', () => { + const input = { + action: 'farcaster', + userIDsToFID: { + '256': 'f256', + '512': 'f512', + '1024': 'f1024', + }, + }; + expect(updateFarcasterRelationshipInputValidator.is(input)).toBe(false); + }); + + test('SHOULD NOT validate input with < 2 userIDsToFID entries', () => { + const input = { + action: 'farcaster', + userIDsToFID: { + '256': 'f256', + }, + }; + expect(updateFarcasterRelationshipInputValidator.is(input)).toBe(false); + }); + + test('Should not validate if action is not farcaster', () => { + const input = { + action: 'NOT_FARCASTER', + userIDsToFID: { + '256': 'f256', + '512': 'f512', + }, + }; + expect(updateFarcasterRelationshipInputValidator.is(input)).toBe(false); + }); +});