diff --git a/lib/types/identity-search/auth-message-types.js b/lib/types/identity-search/auth-message-types.js new file mode 100644 index 000000000..79e69906b --- /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 index 000000000..ceb91f707 --- /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 index 000000000..eb4b1fe4a --- /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 index 000000000..bb3b68d12 --- /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 index baf55df8a..33f26a505 100644 --- a/shared/identity_search_messages/src/messages/auth_messages.rs +++ b/shared/identity_search_messages/src/messages/auth_messages.rs @@ -1,13 +1,25 @@ -//! 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}; #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type", rename_all = "camelCase")] pub struct IdentitySearchAuthMessage { #[serde(rename = "userID")] pub user_id: String, #[serde(rename = "deviceID")] pub device_id: String, pub access_token: String, } diff --git a/shared/identity_search_messages/src/messages/mod.rs b/shared/identity_search_messages/src/messages/mod.rs index c035936ef..2784969eb 100644 --- a/shared/identity_search_messages/src/messages/mod.rs +++ b/shared/identity_search_messages/src/messages/mod.rs @@ -1,30 +1,42 @@ -//! 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; pub mod search_response; pub use auth_messages::*; pub use search_query::*; pub use search_response::*; use serde::{Deserialize, Serialize}; pub use websocket_messages::{ ConnectionInitializationResponse, ConnectionInitializationStatus, Heartbeat, }; #[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] pub enum MessagesToClient { ConnectionInitializationResponse(ConnectionInitializationResponse), IdentitySearchResponse(IdentitySearchResponse), Heartbeat(Heartbeat), } #[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] pub enum MessagesToServer { IdentitySearchAuthMessage(IdentitySearchAuthMessage), IdentitySearchQuery(IdentitySearchQuery), Heartbeat(Heartbeat), } diff --git a/shared/identity_search_messages/src/messages/search_query.rs b/shared/identity_search_messages/src/messages/search_query.rs index 26f7a3b69..56e6a0dc1 100644 --- a/shared/identity_search_messages/src/messages/search_query.rs +++ b/shared/identity_search_messages/src/messages/search_query.rs @@ -1,21 +1,32 @@ -//! 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}; #[derive(Debug, Serialize, Deserialize)] pub struct IdentitySearchPrefix { pub prefix: String, } #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type")] pub enum IdentitySearchMethod { IdentitySearchPrefix(IdentitySearchPrefix), } #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "camelCase")] pub struct IdentitySearchQuery { pub id: String, pub search_method: IdentitySearchMethod, } diff --git a/shared/identity_search_messages/src/messages/search_response.rs b/shared/identity_search_messages/src/messages/search_response.rs index 66e57bcfc..f2ec51cbe 100644 --- a/shared/identity_search_messages/src/messages/search_response.rs +++ b/shared/identity_search_messages/src/messages/search_response.rs @@ -1,29 +1,41 @@ -//! 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}; #[derive(Debug, Serialize, Deserialize)] pub struct IdentitySearchFailure { pub id: String, pub error: String, } #[derive(Debug, Serialize, Deserialize)] pub struct IdentitySearchUser { #[serde(rename = "userID")] pub user_id: String, pub username: String, } #[derive(Debug, Serialize, Deserialize)] pub struct IdentitySearchResult { pub id: String, pub hits: Vec, } #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type", content = "data")] pub enum IdentitySearchResponse { Success(IdentitySearchResult), Error(IdentitySearchFailure), }