Changeset View
Changeset View
Standalone View
Standalone View
lib/types/request-types.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import t, { type TUnion } from 'tcomb'; | |||||
import { type ActivityUpdate } from './activity-types.js'; | import { type ActivityUpdate } from './activity-types.js'; | ||||
import type { Shape } from './core.js'; | import type { Shape } from './core.js'; | ||||
import type { SignedIdentityKeysBlob } from './crypto-types.js'; | import type { SignedIdentityKeysBlob } from './crypto-types.js'; | ||||
import type { Platform, PlatformDetails } from './device-types.js'; | import type { Platform, PlatformDetails } from './device-types.js'; | ||||
import type { RawEntryInfo, CalendarQuery } from './entry-types.js'; | import { | ||||
type RawEntryInfo, | |||||
type CalendarQuery, | |||||
rawEntryInfoValidator, | |||||
} from './entry-types.js'; | |||||
import type { | import type { | ||||
ThreadInconsistencyReportShape, | ThreadInconsistencyReportShape, | ||||
EntryInconsistencyReportShape, | EntryInconsistencyReportShape, | ||||
ClientThreadInconsistencyReportShape, | ClientThreadInconsistencyReportShape, | ||||
ClientEntryInconsistencyReportShape, | ClientEntryInconsistencyReportShape, | ||||
} from './report-types.js'; | } from './report-types.js'; | ||||
import type { RawThreadInfo } from './thread-types.js'; | import { type RawThreadInfo, rawThreadInfoValidator } from './thread-types.js'; | ||||
import type { | import { | ||||
CurrentUserInfo, | type CurrentUserInfo, | ||||
OldCurrentUserInfo, | currentUserInfoValidator, | ||||
AccountUserInfo, | type OldCurrentUserInfo, | ||||
oldCurrentUserInfoValidator, | |||||
type AccountUserInfo, | |||||
accountUserInfoValidator, | |||||
} from './user-types.js'; | } from './user-types.js'; | ||||
import { tNumber, tShape, tID } from '../utils/validation-utils.js'; | |||||
// "Server requests" are requests for information that the server delivers to | // "Server requests" are requests for information that the server delivers to | ||||
// clients. Clients then respond to those requests with a "client response". | // clients. Clients then respond to those requests with a "client response". | ||||
export const serverRequestTypes = Object.freeze({ | export const serverRequestTypes = Object.freeze({ | ||||
PLATFORM: 0, | PLATFORM: 0, | ||||
//DEVICE_TOKEN: 1, (DEPRECATED) | //DEVICE_TOKEN: 1, (DEPRECATED) | ||||
THREAD_INCONSISTENCY: 2, | THREAD_INCONSISTENCY: 2, | ||||
PLATFORM_DETAILS: 3, | PLATFORM_DETAILS: 3, | ||||
Show All 20 Lines | invariant( | ||||
'number is not ServerRequestType enum', | 'number is not ServerRequestType enum', | ||||
); | ); | ||||
return serverRequestType; | return serverRequestType; | ||||
} | } | ||||
type PlatformServerRequest = { | type PlatformServerRequest = { | ||||
+type: 0, | +type: 0, | ||||
}; | }; | ||||
const platformServerRequestValidator = tShape<PlatformServerRequest>({ | |||||
type: tNumber(serverRequestTypes.PLATFORM), | |||||
}); | |||||
type PlatformClientResponse = { | type PlatformClientResponse = { | ||||
+type: 0, | +type: 0, | ||||
+platform: Platform, | +platform: Platform, | ||||
}; | }; | ||||
export type ThreadInconsistencyClientResponse = { | export type ThreadInconsistencyClientResponse = { | ||||
...ThreadInconsistencyReportShape, | ...ThreadInconsistencyReportShape, | ||||
+type: 2, | +type: 2, | ||||
}; | }; | ||||
type PlatformDetailsServerRequest = { | type PlatformDetailsServerRequest = { | ||||
type: 3, | type: 3, | ||||
}; | }; | ||||
const platformDetailsServerRequestValidator = | |||||
tShape<PlatformDetailsServerRequest>({ | |||||
type: tNumber(serverRequestTypes.PLATFORM_DETAILS), | |||||
}); | |||||
type PlatformDetailsClientResponse = { | type PlatformDetailsClientResponse = { | ||||
type: 3, | type: 3, | ||||
platformDetails: PlatformDetails, | platformDetails: PlatformDetails, | ||||
}; | }; | ||||
export type EntryInconsistencyClientResponse = { | export type EntryInconsistencyClientResponse = { | ||||
type: 5, | type: 5, | ||||
...EntryInconsistencyReportShape, | ...EntryInconsistencyReportShape, | ||||
Show All 12 Lines | +stateChanges?: Shape<{ | ||||
+rawEntryInfos: RawEntryInfo[], | +rawEntryInfos: RawEntryInfo[], | ||||
+currentUserInfo: CurrentUserInfo | OldCurrentUserInfo, | +currentUserInfo: CurrentUserInfo | OldCurrentUserInfo, | ||||
+userInfos: AccountUserInfo[], | +userInfos: AccountUserInfo[], | ||||
+deleteThreadIDs: string[], | +deleteThreadIDs: string[], | ||||
+deleteEntryIDs: string[], | +deleteEntryIDs: string[], | ||||
+deleteUserInfoIDs: string[], | +deleteUserInfoIDs: string[], | ||||
}>, | }>, | ||||
}; | }; | ||||
const serverCheckStateServerRequestValidator = | |||||
tShape<ServerCheckStateServerRequest>({ | |||||
type: tNumber(serverRequestTypes.CHECK_STATE), | |||||
hashesToCheck: t.dict(t.String, t.Number), | |||||
failUnmentioned: t.maybe( | |||||
tShape({ | |||||
threadInfos: t.maybe(t.Boolean), | |||||
entryInfos: t.maybe(t.Boolean), | |||||
userInfos: t.maybe(t.Boolean), | |||||
}), | |||||
), | |||||
stateChanges: t.maybe( | |||||
tShape({ | |||||
rawThreadInfos: t.maybe(t.list(rawThreadInfoValidator)), | |||||
rawEntryInfos: t.maybe(t.list(rawEntryInfoValidator)), | |||||
currentUserInfo: t.maybe( | |||||
t.union([currentUserInfoValidator, oldCurrentUserInfoValidator]), | |||||
), | |||||
userInfos: t.maybe(t.list(accountUserInfoValidator)), | |||||
deleteThreadIDs: t.maybe(t.list(tID)), | |||||
deleteEntryIDs: t.maybe(t.list(tID)), | |||||
deleteUserInfoIDs: t.maybe(t.list(t.String)), | |||||
}), | |||||
), | |||||
}); | |||||
type CheckStateClientResponse = { | type CheckStateClientResponse = { | ||||
+type: 6, | +type: 6, | ||||
+hashResults: { +[key: string]: boolean }, | +hashResults: { +[key: string]: boolean }, | ||||
}; | }; | ||||
type InitialActivityUpdatesClientResponse = { | type InitialActivityUpdatesClientResponse = { | ||||
+type: 7, | +type: 7, | ||||
+activityUpdates: $ReadOnlyArray<ActivityUpdate>, | +activityUpdates: $ReadOnlyArray<ActivityUpdate>, | ||||
}; | }; | ||||
type MoreOneTimeKeysServerRequest = { | type MoreOneTimeKeysServerRequest = { | ||||
+type: 8, | +type: 8, | ||||
}; | }; | ||||
const moreOneTimeKeysServerRequestValidator = | |||||
tShape<MoreOneTimeKeysServerRequest>({ | |||||
type: tNumber(serverRequestTypes.MORE_ONE_TIME_KEYS), | |||||
}); | |||||
type MoreOneTimeKeysClientResponse = { | type MoreOneTimeKeysClientResponse = { | ||||
+type: 8, | +type: 8, | ||||
+keys: $ReadOnlyArray<string>, | +keys: $ReadOnlyArray<string>, | ||||
}; | }; | ||||
type SignedIdentityKeysBlobServerRequest = { | type SignedIdentityKeysBlobServerRequest = { | ||||
+type: 9, | +type: 9, | ||||
}; | }; | ||||
const signedIdentityKeysBlobServerRequestValidator = | |||||
tShape<SignedIdentityKeysBlobServerRequest>({ | |||||
type: tNumber(serverRequestTypes.SIGNED_IDENTITY_KEYS_BLOB), | |||||
}); | |||||
type SignedIdentityKeysBlobClientResponse = { | type SignedIdentityKeysBlobClientResponse = { | ||||
+type: 9, | +type: 9, | ||||
+signedIdentityKeysBlob: SignedIdentityKeysBlob, | +signedIdentityKeysBlob: SignedIdentityKeysBlob, | ||||
}; | }; | ||||
export type ServerServerRequest = | export type ServerServerRequest = | ||||
| PlatformServerRequest | | PlatformServerRequest | ||||
| PlatformDetailsServerRequest | | PlatformDetailsServerRequest | ||||
| ServerCheckStateServerRequest | | ServerCheckStateServerRequest | ||||
| MoreOneTimeKeysServerRequest | | MoreOneTimeKeysServerRequest | ||||
| SignedIdentityKeysBlobServerRequest; | | SignedIdentityKeysBlobServerRequest; | ||||
export const serverServerRequestValidator: TUnion<ServerServerRequest> = | |||||
t.union([ | |||||
platformServerRequestValidator, | |||||
platformDetailsServerRequestValidator, | |||||
serverCheckStateServerRequestValidator, | |||||
moreOneTimeKeysServerRequestValidator, | |||||
signedIdentityKeysBlobServerRequestValidator, | |||||
]); | |||||
export type ClientResponse = | export type ClientResponse = | ||||
| PlatformClientResponse | | PlatformClientResponse | ||||
| ThreadInconsistencyClientResponse | | ThreadInconsistencyClientResponse | ||||
| PlatformDetailsClientResponse | | PlatformDetailsClientResponse | ||||
| EntryInconsistencyClientResponse | | EntryInconsistencyClientResponse | ||||
| CheckStateClientResponse | | CheckStateClientResponse | ||||
| InitialActivityUpdatesClientResponse | | InitialActivityUpdatesClientResponse | ||||
| MoreOneTimeKeysClientResponse | | MoreOneTimeKeysClientResponse | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |