Changeset View
Changeset View
Standalone View
Standalone View
lib/types/socket-types.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import t, { type TInterface, type TUnion } from 'tcomb'; | |||||
import { | import { | ||||
type ActivityUpdate, | type ActivityUpdate, | ||||
type UpdateActivityResult, | type UpdateActivityResult, | ||||
updateActivityResultValidator, | |||||
} from './activity-types.js'; | } from './activity-types.js'; | ||||
import type { Platform } from './device-types.js'; | import type { Platform } from './device-types.js'; | ||||
import type { APIRequest } from './endpoints.js'; | import type { APIRequest } from './endpoints.js'; | ||||
import { | import { | ||||
type RawEntryInfo, | type RawEntryInfo, | ||||
rawEntryInfoValidator, | |||||
type CalendarQuery, | type CalendarQuery, | ||||
defaultCalendarQuery, | defaultCalendarQuery, | ||||
} from './entry-types.js'; | } from './entry-types.js'; | ||||
import type { MessagesResponse, NewMessagesPayload } from './message-types.js'; | import { | ||||
import type { | type MessagesResponse, | ||||
ServerServerRequest, | messagesResponseValidator, | ||||
ClientServerRequest, | type NewMessagesPayload, | ||||
ClientResponse, | newMessagesPayloadValidator, | ||||
ClientClientResponse, | } from './message-types.js'; | ||||
import { | |||||
type ServerServerRequest, | |||||
serverServerRequestValidator, | |||||
type ClientServerRequest, | |||||
type ClientResponse, | |||||
type ClientClientResponse, | |||||
} from './request-types.js'; | } from './request-types.js'; | ||||
import type { SessionState, SessionIdentification } from './session-types.js'; | import type { SessionState, SessionIdentification } from './session-types.js'; | ||||
import type { RawThreadInfo } from './thread-types.js'; | import { type RawThreadInfo, rawThreadInfoValidator } from './thread-types.js'; | ||||
import type { | import { | ||||
ClientUpdatesResult, | type ClientUpdatesResult, | ||||
ClientUpdatesResultWithUserInfos, | type ClientUpdatesResultWithUserInfos, | ||||
ServerUpdatesResult, | type ServerUpdatesResult, | ||||
ServerUpdatesResultWithUserInfos, | serverUpdatesResultValidator, | ||||
type ServerUpdatesResultWithUserInfos, | |||||
serverUpdatesResultWithUserInfosValidator, | |||||
} from './update-types.js'; | } from './update-types.js'; | ||||
import type { | import { | ||||
UserInfo, | type UserInfo, | ||||
CurrentUserInfo, | userInfoValidator, | ||||
OldCurrentUserInfo, | type CurrentUserInfo, | ||||
LoggedOutUserInfo, | currentUserInfoValidator, | ||||
type OldCurrentUserInfo, | |||||
oldCurrentUserInfoValidator, | |||||
type LoggedOutUserInfo, | |||||
loggedOutUserInfoValidator, | |||||
} from './user-types.js'; | } from './user-types.js'; | ||||
import { tShape, tNumber, tID } from '../utils/validation-utils.js'; | |||||
// The types of messages that the client sends across the socket | // The types of messages that the client sends across the socket | ||||
export const clientSocketMessageTypes = Object.freeze({ | export const clientSocketMessageTypes = Object.freeze({ | ||||
INITIAL: 0, | INITIAL: 0, | ||||
RESPONSES: 1, | RESPONSES: 1, | ||||
//ACTIVITY_UPDATES: 2, (DEPRECATED) | //ACTIVITY_UPDATES: 2, (DEPRECATED) | ||||
PING: 3, | PING: 3, | ||||
ACK_UPDATES: 4, | ACK_UPDATES: 4, | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | |||||
export const fullStateSyncActionType = 'FULL_STATE_SYNC'; | export const fullStateSyncActionType = 'FULL_STATE_SYNC'; | ||||
export type BaseFullStateSync = { | export type BaseFullStateSync = { | ||||
+messagesResult: MessagesResponse, | +messagesResult: MessagesResponse, | ||||
+threadInfos: { +[id: string]: RawThreadInfo }, | +threadInfos: { +[id: string]: RawThreadInfo }, | ||||
+rawEntryInfos: $ReadOnlyArray<RawEntryInfo>, | +rawEntryInfos: $ReadOnlyArray<RawEntryInfo>, | ||||
+userInfos: $ReadOnlyArray<UserInfo>, | +userInfos: $ReadOnlyArray<UserInfo>, | ||||
+updatesCurrentAsOf: number, | +updatesCurrentAsOf: number, | ||||
}; | }; | ||||
const baseFullStateSyncValidator = tShape<BaseFullStateSync>({ | |||||
messagesResult: messagesResponseValidator, | |||||
threadInfos: t.dict(tID, rawThreadInfoValidator), | |||||
rawEntryInfos: t.list(rawEntryInfoValidator), | |||||
userInfos: t.list(userInfoValidator), | |||||
updatesCurrentAsOf: t.Number, | |||||
}); | |||||
export type ClientFullStateSync = { | export type ClientFullStateSync = { | ||||
...BaseFullStateSync, | ...BaseFullStateSync, | ||||
+currentUserInfo: CurrentUserInfo, | +currentUserInfo: CurrentUserInfo, | ||||
}; | }; | ||||
export type StateSyncFullActionPayload = { | export type StateSyncFullActionPayload = { | ||||
...ClientFullStateSync, | ...ClientFullStateSync, | ||||
+calendarQuery: CalendarQuery, | +calendarQuery: CalendarQuery, | ||||
}; | }; | ||||
export type ClientStateSyncFullSocketPayload = { | export type ClientStateSyncFullSocketPayload = { | ||||
...ClientFullStateSync, | ...ClientFullStateSync, | ||||
+type: 0, | +type: 0, | ||||
// Included iff client is using sessionIdentifierTypes.BODY_SESSION_ID | // Included iff client is using sessionIdentifierTypes.BODY_SESSION_ID | ||||
+sessionID?: string, | +sessionID?: string, | ||||
}; | }; | ||||
export type ServerFullStateSync = { | export type ServerFullStateSync = { | ||||
...BaseFullStateSync, | ...BaseFullStateSync, | ||||
+currentUserInfo: CurrentUserInfo | OldCurrentUserInfo, | +currentUserInfo: CurrentUserInfo | OldCurrentUserInfo, | ||||
}; | }; | ||||
const serverFullStateSyncValidator = tShape<ServerFullStateSync>({ | |||||
...baseFullStateSyncValidator.meta.props, | |||||
currentUserInfo: t.union([ | |||||
currentUserInfoValidator, | |||||
oldCurrentUserInfoValidator, | |||||
]), | |||||
}); | |||||
export type ServerStateSyncFullSocketPayload = { | export type ServerStateSyncFullSocketPayload = { | ||||
...ServerFullStateSync, | ...ServerFullStateSync, | ||||
+type: 0, | +type: 0, | ||||
// Included iff client is using sessionIdentifierTypes.BODY_SESSION_ID | // Included iff client is using sessionIdentifierTypes.BODY_SESSION_ID | ||||
+sessionID?: string, | +sessionID?: string, | ||||
}; | }; | ||||
const serverStateSyncFullSocketPayloadValidator = | |||||
tShape<ServerStateSyncFullSocketPayload>({ | |||||
...serverFullStateSyncValidator.meta.props, | |||||
type: tNumber(stateSyncPayloadTypes.FULL), | |||||
sessionID: t.maybe(t.String), | |||||
}); | |||||
export const incrementalStateSyncActionType = 'INCREMENTAL_STATE_SYNC'; | export const incrementalStateSyncActionType = 'INCREMENTAL_STATE_SYNC'; | ||||
export type BaseIncrementalStateSync = { | export type BaseIncrementalStateSync = { | ||||
+messagesResult: MessagesResponse, | +messagesResult: MessagesResponse, | ||||
+deltaEntryInfos: $ReadOnlyArray<RawEntryInfo>, | +deltaEntryInfos: $ReadOnlyArray<RawEntryInfo>, | ||||
+deletedEntryIDs: $ReadOnlyArray<string>, | +deletedEntryIDs: $ReadOnlyArray<string>, | ||||
+userInfos: $ReadOnlyArray<UserInfo>, | +userInfos: $ReadOnlyArray<UserInfo>, | ||||
}; | }; | ||||
const baseIncrementalStateSyncValidator = tShape<BaseIncrementalStateSync>({ | |||||
messagesResult: messagesResponseValidator, | |||||
deltaEntryInfos: t.list(rawEntryInfoValidator), | |||||
deletedEntryIDs: t.list(tID), | |||||
userInfos: t.list(userInfoValidator), | |||||
}); | |||||
export type ClientIncrementalStateSync = { | export type ClientIncrementalStateSync = { | ||||
...BaseIncrementalStateSync, | ...BaseIncrementalStateSync, | ||||
+updatesResult: ClientUpdatesResult, | +updatesResult: ClientUpdatesResult, | ||||
}; | }; | ||||
export type StateSyncIncrementalActionPayload = { | export type StateSyncIncrementalActionPayload = { | ||||
...ClientIncrementalStateSync, | ...ClientIncrementalStateSync, | ||||
+calendarQuery: CalendarQuery, | +calendarQuery: CalendarQuery, | ||||
}; | }; | ||||
type ClientStateSyncIncrementalSocketPayload = { | type ClientStateSyncIncrementalSocketPayload = { | ||||
+type: 1, | +type: 1, | ||||
...ClientIncrementalStateSync, | ...ClientIncrementalStateSync, | ||||
}; | }; | ||||
export type ServerIncrementalStateSync = { | export type ServerIncrementalStateSync = { | ||||
...BaseIncrementalStateSync, | ...BaseIncrementalStateSync, | ||||
+updatesResult: ServerUpdatesResult, | +updatesResult: ServerUpdatesResult, | ||||
}; | }; | ||||
const serverIncrementalStateSyncValidator = tShape<ServerIncrementalStateSync>({ | |||||
...baseIncrementalStateSyncValidator.meta.props, | |||||
updatesResult: serverUpdatesResultValidator, | |||||
}); | |||||
type ServerStateSyncIncrementalSocketPayload = { | type ServerStateSyncIncrementalSocketPayload = { | ||||
+type: 1, | +type: 1, | ||||
...ServerIncrementalStateSync, | ...ServerIncrementalStateSync, | ||||
}; | }; | ||||
const serverStateSyncIncrementalSocketPayloadValidator = | |||||
tShape<ServerStateSyncIncrementalSocketPayload>({ | |||||
type: tNumber(stateSyncPayloadTypes.INCREMENTAL), | |||||
...serverIncrementalStateSyncValidator.meta.props, | |||||
}); | |||||
export type ClientStateSyncSocketPayload = | export type ClientStateSyncSocketPayload = | ||||
| ClientStateSyncFullSocketPayload | | ClientStateSyncFullSocketPayload | ||||
| ClientStateSyncIncrementalSocketPayload; | | ClientStateSyncIncrementalSocketPayload; | ||||
export type ServerStateSyncSocketPayload = | export type ServerStateSyncSocketPayload = | ||||
| ServerStateSyncFullSocketPayload | | ServerStateSyncFullSocketPayload | ||||
| ServerStateSyncIncrementalSocketPayload; | | ServerStateSyncIncrementalSocketPayload; | ||||
const serverStateSyncSocketPayloadValidator = t.union([ | |||||
serverStateSyncFullSocketPayloadValidator, | |||||
serverStateSyncIncrementalSocketPayloadValidator, | |||||
]); | |||||
export type ServerStateSyncServerSocketMessage = { | export type ServerStateSyncServerSocketMessage = { | ||||
+type: 0, | +type: 0, | ||||
+responseTo: number, | +responseTo: number, | ||||
+payload: ServerStateSyncSocketPayload, | +payload: ServerStateSyncSocketPayload, | ||||
}; | }; | ||||
export const serverStateSyncServerSocketMessageValidator: TInterface<ServerStateSyncServerSocketMessage> = | |||||
tShape<ServerStateSyncServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.STATE_SYNC), | |||||
responseTo: t.Number, | |||||
payload: serverStateSyncSocketPayloadValidator, | |||||
}); | |||||
export type ServerRequestsServerSocketMessage = { | export type ServerRequestsServerSocketMessage = { | ||||
+type: 1, | +type: 1, | ||||
+responseTo?: number, | +responseTo?: number, | ||||
+payload: { | +payload: { | ||||
+serverRequests: $ReadOnlyArray<ServerServerRequest>, | +serverRequests: $ReadOnlyArray<ServerServerRequest>, | ||||
}, | }, | ||||
}; | }; | ||||
export const serverRequestsServerSocketMessageValidator: TInterface<ServerRequestsServerSocketMessage> = | |||||
tShape<ServerRequestsServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.REQUESTS), | |||||
responseTo: t.maybe(t.Number), | |||||
payload: tShape({ | |||||
serverRequests: t.list(serverServerRequestValidator), | |||||
}), | |||||
}); | |||||
export type ErrorServerSocketMessage = { | export type ErrorServerSocketMessage = { | ||||
type: 2, | type: 2, | ||||
responseTo?: number, | responseTo?: number, | ||||
message: string, | message: string, | ||||
payload?: Object, | payload?: Object, | ||||
}; | }; | ||||
export const errorServerSocketMessageValidator: TInterface<ErrorServerSocketMessage> = | |||||
tShape<ErrorServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.ERROR), | |||||
responseTo: t.maybe(t.Number), | |||||
message: t.String, | |||||
payload: t.maybe(t.Object), | |||||
}); | |||||
export type AuthErrorServerSocketMessage = { | export type AuthErrorServerSocketMessage = { | ||||
type: 3, | type: 3, | ||||
responseTo: number, | responseTo: number, | ||||
message: string, | message: string, | ||||
// If unspecified, it is because the client is using cookieSources.HEADER, | // If unspecified, it is because the client is using cookieSources.HEADER, | ||||
// which means the server can't update the cookie from a socket message. | // which means the server can't update the cookie from a socket message. | ||||
sessionChange?: { | sessionChange?: { | ||||
cookie: string, | cookie: string, | ||||
currentUserInfo: LoggedOutUserInfo, | currentUserInfo: LoggedOutUserInfo, | ||||
}, | }, | ||||
}; | }; | ||||
export const authErrorServerSocketMessageValidator: TInterface<AuthErrorServerSocketMessage> = | |||||
tShape<AuthErrorServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.AUTH_ERROR), | |||||
responseTo: t.Number, | |||||
message: t.String, | |||||
sessionChange: t.maybe( | |||||
tShape({ cookie: t.String, currentUserInfo: loggedOutUserInfoValidator }), | |||||
), | |||||
}); | |||||
export type ActivityUpdateResponseServerSocketMessage = { | export type ActivityUpdateResponseServerSocketMessage = { | ||||
+type: 4, | +type: 4, | ||||
+responseTo: number, | +responseTo: number, | ||||
+payload: UpdateActivityResult, | +payload: UpdateActivityResult, | ||||
}; | }; | ||||
export const activityUpdateResponseServerSocketMessageValidator: TInterface<ActivityUpdateResponseServerSocketMessage> = | |||||
tShape<ActivityUpdateResponseServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.ACTIVITY_UPDATE_RESPONSE), | |||||
responseTo: t.Number, | |||||
payload: updateActivityResultValidator, | |||||
}); | |||||
export type PongServerSocketMessage = { | export type PongServerSocketMessage = { | ||||
+type: 5, | +type: 5, | ||||
+responseTo: number, | +responseTo: number, | ||||
}; | }; | ||||
export const pongServerSocketMessageValidator: TInterface<PongServerSocketMessage> = | |||||
tShape<PongServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.PONG), | |||||
responseTo: t.Number, | |||||
}); | |||||
export type ServerUpdatesServerSocketMessage = { | export type ServerUpdatesServerSocketMessage = { | ||||
+type: 6, | +type: 6, | ||||
+payload: ServerUpdatesResultWithUserInfos, | +payload: ServerUpdatesResultWithUserInfos, | ||||
}; | }; | ||||
export const serverUpdatesServerSocketMessageValidator: TInterface<ServerUpdatesServerSocketMessage> = | |||||
tShape<ServerUpdatesServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.UPDATES), | |||||
payload: serverUpdatesResultWithUserInfosValidator, | |||||
}); | |||||
export type MessagesServerSocketMessage = { | export type MessagesServerSocketMessage = { | ||||
+type: 7, | +type: 7, | ||||
+payload: NewMessagesPayload, | +payload: NewMessagesPayload, | ||||
}; | }; | ||||
export const messagesServerSocketMessageValidator: TInterface<MessagesServerSocketMessage> = | |||||
tShape<MessagesServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.MESSAGES), | |||||
payload: newMessagesPayloadValidator, | |||||
}); | |||||
export type APIResponseServerSocketMessage = { | export type APIResponseServerSocketMessage = { | ||||
+type: 8, | +type: 8, | ||||
+responseTo: number, | +responseTo: number, | ||||
+payload: Object, | +payload?: Object, | ||||
}; | }; | ||||
export const apiResponseServerSocketMessageValidator: TInterface<APIResponseServerSocketMessage> = | |||||
tShape<APIResponseServerSocketMessage>({ | |||||
type: tNumber(serverSocketMessageTypes.API_RESPONSE), | |||||
responseTo: t.Number, | |||||
payload: t.maybe(t.Object), | |||||
}); | |||||
export type ServerServerSocketMessage = | export type ServerServerSocketMessage = | ||||
| ServerStateSyncServerSocketMessage | | ServerStateSyncServerSocketMessage | ||||
| ServerRequestsServerSocketMessage | | ServerRequestsServerSocketMessage | ||||
| ErrorServerSocketMessage | | ErrorServerSocketMessage | ||||
| AuthErrorServerSocketMessage | | AuthErrorServerSocketMessage | ||||
| ActivityUpdateResponseServerSocketMessage | | ActivityUpdateResponseServerSocketMessage | ||||
| PongServerSocketMessage | | PongServerSocketMessage | ||||
| ServerUpdatesServerSocketMessage | | ServerUpdatesServerSocketMessage | ||||
| MessagesServerSocketMessage | | MessagesServerSocketMessage | ||||
| APIResponseServerSocketMessage; | | APIResponseServerSocketMessage; | ||||
export const serverServerSocketMessageValidator: TUnion<ServerServerSocketMessage> = | |||||
t.union([ | |||||
serverStateSyncServerSocketMessageValidator, | |||||
serverRequestsServerSocketMessageValidator, | |||||
errorServerSocketMessageValidator, | |||||
authErrorServerSocketMessageValidator, | |||||
activityUpdateResponseServerSocketMessageValidator, | |||||
pongServerSocketMessageValidator, | |||||
serverUpdatesServerSocketMessageValidator, | |||||
messagesServerSocketMessageValidator, | |||||
apiResponseServerSocketMessageValidator, | |||||
]); | |||||
export type ClientRequestsServerSocketMessage = { | export type ClientRequestsServerSocketMessage = { | ||||
+type: 1, | +type: 1, | ||||
+responseTo?: number, | +responseTo?: number, | ||||
+payload: { | +payload: { | ||||
+serverRequests: $ReadOnlyArray<ClientServerRequest>, | +serverRequests: $ReadOnlyArray<ClientServerRequest>, | ||||
}, | }, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |