diff --git a/lib/types/identity-search/auth-message-types.js b/lib/types/identity-search/auth-message-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/auth-message-types.js @@ -0,0 +1,21 @@ +// @flow + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type AuthMessage = { + +type: 'AuthMessage', + +userID: string, + +deviceID: string, + +accessToken: string, +}; + +export const authMessageValidator: TInterface = + tShape({ + type: tString('AuthMessage'), + userID: t.String, + deviceID: t.String, + accessToken: t.String, + }); diff --git a/lib/types/identity-search/messages.js b/lib/types/identity-search/messages.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/messages.js @@ -0,0 +1,61 @@ +// @flow + +import type { TUnion } from 'tcomb'; +import t from 'tcomb'; + +import { + type AuthMessage, + authMessageValidator, +} from './auth-message-types.js'; +import { + type SearchQuery, + searchQueryValidator, +} from './search-query-types.js'; +import { + type SearchResult, + searchResultValidator, +} from './search-result-types.js'; +import { + type ConnectionInitializationResponse, + connectionInitializationResponseValidator, +} from '../websocket/connection-initialization-response-types.js'; +import { + type Heartbeat, + heartbeatValidator, +} from '../websocket/heartbeat-types.js'; + +export const identitySearchMessageTypes = Object.freeze({ + AUTH_MESSAGE: 'AuthMessage', + SEARCH_QUERY: 'SearchQuery', + HEARTBEAT: 'Heartbeat', + CONNECTION_INITIALIZATION_RESPONSE: 'ConnectionInitializationResponse', + SEARCH_RESULT: 'SearchResult', +}); + +/* + * This file defines types and validation for messages exchanged + * with the Identity Search WebSocket server. + * The definitions in this file should remain in sync + * with the structures defined in the corresponding + * Rust file at `shared/identity_search_messages/src/messages/mod.rs`. + * + * If you edit the definitions in one file, + * please make sure to update the corresponding definitions in the other. + * + */ + +export const identitySearchMessageValidator: TUnion = + t.union([ + authMessageValidator, + searchQueryValidator, + heartbeatValidator, + connectionInitializationResponseValidator, + searchResultValidator, + ]); + +export type IdentitySearchMessage = + | AuthMessage + | SearchQuery + | Heartbeat + | ConnectionInitializationResponse + | SearchResult; diff --git a/lib/types/identity-search/search-query-types.js b/lib/types/identity-search/search-query-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/search-query-types.js @@ -0,0 +1,20 @@ +// @flow + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type SearchQuery = Prefix; + +export type Prefix = { + +type: 'Prefix', + +prefix: string, +}; + +export const prefixValidator: TInterface = tShape({ + type: tString('Prefix'), + prefix: t.String, +}); + +export const searchQueryValidator: TInterface = prefixValidator; diff --git a/lib/types/identity-search/search-result-types.js b/lib/types/identity-search/search-result-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/search-result-types.js @@ -0,0 +1,27 @@ +// @flow + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type User = { + +userID: string, + +username: string, +}; + +export type SearchResult = { + +type: 'SearchResult', + +payload: $ReadOnlyArray, +}; + +export const userValidator: TInterface = tShape({ + userID: t.String, + username: t.String, +}); + +export const searchResultValidator: TInterface = + tShape({ + type: tString('SearchResult'), + payload: t.list(userValidator), + });