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,33 @@ +// @flow + +/* + * This file defines types and validation for the auth message sent + * from the client to 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/auth_messages.rs`. + * + * If you edit the definitions in one file, + * please make sure to update the corresponding definitions in the other. + * + */ + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type IdentitySearchAuthMessage = { + +type: 'IdentitySearchAuthMessage', + +userID: string, + +deviceID: string, + +accessToken: string, +}; + +export const identityAuthMessageValidator: TInterface = + tShape({ + type: tString('IdentitySearchAuthMessage'), + 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,76 @@ +// @flow + +/* + * This file defines types and validation for messages sent + * to Identity Search WebSocket server and messages sent to client. + * 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. + * + */ + +import type { TUnion } from 'tcomb'; +import t from 'tcomb'; + +import { + type IdentitySearchAuthMessage, + identityAuthMessageValidator, +} from './auth-message-types.js'; +import { + type IdentitySearchQuery, + identitySearchQueryValidator, +} from './search-query-types.js'; +import { + type IdentitySearchResponse, + identitySearchResponseValidator, +} from './search-response-types.js'; +import { + type ConnectionInitializationResponse, + connectionInitializationResponseValidator, +} from '../websocket/connection-initialization-response-types.js'; +import { + type Heartbeat, + heartbeatValidator, +} from '../websocket/heartbeat-types.js'; + +export const identitySearchMessageToClientTypes = Object.freeze({ + CONNECTION_INITIALIZATION_RESPONSE: 'ConnectionInitializationResponse', + SUCCESS: 'Success', + ERROR: 'Error', + SEARCH_RESPONSE: 'IdentitySearchResponse', + HEARTBEAT: 'Heartbeat', +}); + +export const identitySearchMessageToClientValidator: TUnion = + t.union([ + connectionInitializationResponseValidator, + identitySearchResponseValidator, + heartbeatValidator, + ]); + +export type IdentitySearchMessageToClient = + | ConnectionInitializationResponse + | IdentitySearchResponse + | Heartbeat; + +export const identitySearchMessageToServerTypes = Object.freeze({ + IDENTITY_SEARCH_AUTH_MESSAGE: 'IdentitySearchAuthMessage', + IDENTITY_SEARCH_QUERY: 'IdentitySearchQuery', + IDENTITY_SEARCH_PREFIX: 'IdentitySearchPrefix', + HEARTBEAT: 'Heartbeat', +}); + +export const identitySearchMessageToServerValidator: TUnion = + t.union([ + identityAuthMessageValidator, + identitySearchQueryValidator, + heartbeatValidator, + ]); + +export type IdentitySearchMessageToServer = + | IdentitySearchAuthMessage + | IdentitySearchQuery + | Heartbeat; 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,47 @@ +// @flow + +/* + * This file defines types and validation for the search query message sent + * from the client to 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/search_query.rs`. + * + * If you edit the definitions in one file, + * please make sure to update the corresponding definitions in the other. + * + */ + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type IdentitySearchMethod = IdentitySearchPrefix; + +export type IdentitySearchPrefix = { + +type: 'IdentitySearchPrefix', + +prefix: string, +}; + +export const identityPrefixValidator: TInterface = + tShape({ + type: tString('IdentitySearchPrefix'), + prefix: t.String, + }); + +export const identitySearchMethodValidator: TInterface = + identityPrefixValidator; + +export type IdentitySearchQuery = { + +type: 'IdentitySearchQuery', + +id: string, + +searchMethod: IdentitySearchMethod, +}; + +export const identitySearchQueryValidator: TInterface = + tShape({ + type: tString('IdentitySearchQuery'), + id: t.String, + searchMethod: identitySearchMethodValidator, + }); diff --git a/lib/types/identity-search/search-response-types.js b/lib/types/identity-search/search-response-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/search-response-types.js @@ -0,0 +1,76 @@ +// @flow + +/* + * This file defines types and validation for the search response message + * sent from the Identity Search WebSocket server to client. + * 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/search_response.rs`. + * + * If you edit the definitions in one file, + * please make sure to update the corresponding definitions in the other. + * + */ + +import type { TInterface, TUnion } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type IdentitySearchFailure = { + +id: string, + +error: string, +}; + +export const identityFailureValidator: TInterface = + tShape({ + id: t.String, + error: t.String, + }); + +export type IdentitySearchUser = { + +userID: string, + +username: string, +}; + +export const identitySearchUserValidator: TInterface = + tShape({ + userID: t.String, + username: t.String, + }); + +export type IdentitySearchResult = { + +id: string, + +hits: $ReadOnlyArray, +}; + +export const identitySearchResultValidator: TInterface = + tShape({ + id: t.String, + hits: t.list(identitySearchUserValidator), + }); + +type IdentitySearchResponseSuccess = { + +type: 'Success', + +data: IdentitySearchResult, +}; +type IdentitySearchResponseError = { + +type: 'Error', + +data: IdentitySearchFailure, +}; + +export type IdentitySearchResponse = + | IdentitySearchResponseSuccess + | IdentitySearchResponseError; + +export const identitySearchResponseValidator: TUnion = + t.union([ + tShape({ + type: tString('Success'), + data: identitySearchResultValidator, + }), + tShape({ + type: tString('Error'), + data: identityFailureValidator, + }), + ]); diff --git a/shared/identity_search_messages/src/messages/auth_messages.rs b/shared/identity_search_messages/src/messages/auth_messages.rs --- a/shared/identity_search_messages/src/messages/auth_messages.rs +++ b/shared/identity_search_messages/src/messages/auth_messages.rs @@ -1,4 +1,16 @@ -//! Message sent by client to authenticate with Identity Search Server +//! Auth Message sent by Client to authenticate with Identity Search Server via WebSocket. + +// +// This file defines structs and enums for the auth message sent +// from the Client to the Identity Search WebSocket server. +// The definitions in this file should remain in sync +// with the types and validators defined in the corresponding +// JavaScript file at `lib/types/identity-search/auth-message-types.js`. +// +// If you edit the definitions in one file, +// please make sure to update the corresponding definitions in the other. +// +// use serde::{Deserialize, Serialize}; diff --git a/shared/identity_search_messages/src/messages/mod.rs b/shared/identity_search_messages/src/messages/mod.rs --- a/shared/identity_search_messages/src/messages/mod.rs +++ b/shared/identity_search_messages/src/messages/mod.rs @@ -1,4 +1,16 @@ -//! Messages sent between Client and Identity Search Server +//! Messages sent between Client and Identity Search Server via WebSocket. + +// +// This file defines structs and enums for messages sent +// to Identity Search WebSocket server and messages sent to Client. +// The definitions in this file should remain in sync +// with the types and validators defined in the corresponding +// JavaScript file at `lib/types/identity-search/messages.js`. +// +// If you edit the definitions in one file, +// please make sure to update the corresponding definitions in the other. +// +// pub mod auth_messages; pub mod search_query; diff --git a/shared/identity_search_messages/src/messages/search_query.rs b/shared/identity_search_messages/src/messages/search_query.rs --- a/shared/identity_search_messages/src/messages/search_query.rs +++ b/shared/identity_search_messages/src/messages/search_query.rs @@ -1,4 +1,15 @@ -//! Search Request Messages sent by Client to Identity Search via WebSocket. +//! Search Query Message sent by Client to Identity Search via WebSocket. + +// +// This file defines structs and enums for the search query message sent +// from the Client to the Identity Search WebSocket server. +// The definitions in this file should remain in sync +// with the types and validators defined in the corresponding +// JavaScript file at `lib/types/identity-search/search-query-types.js`. +// +// If you edit the definitions in one file, +// please make sure to update the corresponding definitions in the other. +// use serde::{Deserialize, Serialize}; diff --git a/shared/identity_search_messages/src/messages/search_response.rs b/shared/identity_search_messages/src/messages/search_response.rs --- a/shared/identity_search_messages/src/messages/search_response.rs +++ b/shared/identity_search_messages/src/messages/search_response.rs @@ -1,4 +1,16 @@ -//! Search Result Messages sent by Identity Search via WebSocket. +//! Search Response Message sent by Identity Search via WebSocket to Client. + +// +// This file defines structs and enums for the search response message +// sent from the Identity Search WebSocket server to Client. +// The definitions in this file should remain in sync +// with the types and validators defined in the corresponding JavaScript file at +// `lib/types/identity-search/search-response-types.js`. +// +// If you edit the definitions in one file, +// please make sure to update the corresponding definitions in the other. +// +// use serde::{Deserialize, Serialize};