diff --git a/keyserver/src/shared/state-sync/state-sync-spec.js b/keyserver/src/shared/state-sync/state-sync-spec.js --- a/keyserver/src/shared/state-sync/state-sync-spec.js +++ b/keyserver/src/shared/state-sync/state-sync-spec.js @@ -13,5 +13,5 @@ ) => Promise, +getServerInfosHash: (infos: Infos) => number, +getServerInfoHash: (info: Info) => number, - ...StateSyncSpec, + ...StateSyncSpec, }; diff --git a/lib/selectors/socket-selectors.js b/lib/selectors/socket-selectors.js --- a/lib/selectors/socket-selectors.js +++ b/lib/selectors/socket-selectors.js @@ -39,12 +39,12 @@ ); const boundStateSyncSpecsSelector: AppState => { - specsPerHashKey: { +[string]: BoundStateSyncSpec }, - specPerInnerHashKey: { +[string]: BoundStateSyncSpec }, + specsPerHashKey: { +[string]: BoundStateSyncSpec }, + specPerInnerHashKey: { +[string]: BoundStateSyncSpec }, } = // $FlowFixMe - createSelector only handles finite number of subselectors createSelector(stateSyncSpecSelectors, (...specs) => { - const boundSpecs = (specs: BoundStateSyncSpec[]); + const boundSpecs = (specs: BoundStateSyncSpec[]); const specsPerHashKey = Object.fromEntries( boundSpecs.map(spec => [spec.hashKey, spec]), ); diff --git a/lib/shared/state-sync/current-user-state-sync-spec.js b/lib/shared/state-sync/current-user-state-sync-spec.js --- a/lib/shared/state-sync/current-user-state-sync-spec.js +++ b/lib/shared/state-sync/current-user-state-sync-spec.js @@ -4,33 +4,19 @@ import type { StateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types'; -import { - type CurrentUserInfo, - currentUserInfoValidator, -} from '../../types/user-types.js'; -import { convertClientIDsToServerIDs } from '../../utils/conversion-utils.js'; +import { type CurrentUserInfo } from '../../types/user-types.js'; import { hash } from '../../utils/objects.js'; -import { ashoatKeyserverID } from '../../utils/validation-utils.js'; -export const currentUserStateSyncSpec: StateSyncSpec< - CurrentUserInfo, - CurrentUserInfo, -> = Object.freeze({ - hashKey: 'currentUserInfo', - convertClientToServerInfos(info: CurrentUserInfo) { - return convertClientIDsToServerIDs( - ashoatKeyserverID, - currentUserInfoValidator, - info, - ); - }, - selector: createSelector( - (state: AppState) => state.currentUserInfo, - currentUserInfo => ({ - ...currentUserStateSyncSpec, - getInfoHash: () => hash(currentUserInfo), - getAllInfosHash: () => hash(currentUserInfo), - getIDs: () => [], - }), - ), -}); +export const currentUserStateSyncSpec: StateSyncSpec = + Object.freeze({ + hashKey: 'currentUserInfo', + selector: createSelector( + (state: AppState) => state.currentUserInfo, + currentUserInfo => ({ + ...currentUserStateSyncSpec, + getInfoHash: () => hash(currentUserInfo), + getAllInfosHash: () => hash(currentUserInfo), + getIDs: () => [], + }), + ), + }); diff --git a/lib/shared/state-sync/entries-state-sync-spec.js b/lib/shared/state-sync/entries-state-sync-spec.js --- a/lib/shared/state-sync/entries-state-sync-spec.js +++ b/lib/shared/state-sync/entries-state-sync-spec.js @@ -1,58 +1,39 @@ // @flow import { createSelector } from 'reselect'; -import t from 'tcomb'; import type { StateSyncSpec } from './state-sync-spec.js'; import { type CalendarQuery, type RawEntryInfos, - rawEntryInfoValidator, type RawEntryInfo, } from '../../types/entry-types.js'; import type { AppState } from '../../types/redux-types.js'; -import { convertClientIDsToServerIDs } from '../../utils/conversion-utils.js'; import { values, combineUnorderedHashes, hash } from '../../utils/objects.js'; -import { ashoatKeyserverID, tID } from '../../utils/validation-utils.js'; +import { ashoatKeyserverID } from '../../utils/validation-utils.js'; import { filterRawEntryInfosByCalendarQuery, serverEntryInfosObject, } from '../entry-utils.js'; -export const entriesStateSyncSpec: StateSyncSpec = - Object.freeze({ - hashKey: 'entryInfos', - innerHashSpec: { - hashKey: 'entryInfo', - deleteKey: 'deleteEntryIDs', - rawInfosKey: 'rawEntryInfos', - }, - convertClientToServerInfos( - infos: RawEntryInfos, - calendarQuery: CalendarQuery, - ) { - const filteredEntryInfos = filterRawEntryInfosByCalendarQuery( - serverEntryInfosObject(values(infos)), - calendarQuery, - ); - - return convertClientIDsToServerIDs( - ashoatKeyserverID, - t.dict(tID, rawEntryInfoValidator), - filteredEntryInfos, - ); - }, - selector: createSelector( - (state: AppState) => state.entryStore.entryInfos, - entryInfos => ({ - ...entriesStateSyncSpec, - getInfoHash: id => hash(entryInfos[`${ashoatKeyserverID}|${id}`]), - getAllInfosHash: calendarQuery => - getEntryInfosHash(entryInfos, calendarQuery), - getIDs: calendarQuery => getEntryIDs(entryInfos, calendarQuery), - }), - ), - }); +export const entriesStateSyncSpec: StateSyncSpec = Object.freeze({ + hashKey: 'entryInfos', + innerHashSpec: { + hashKey: 'entryInfo', + deleteKey: 'deleteEntryIDs', + rawInfosKey: 'rawEntryInfos', + }, + selector: createSelector( + (state: AppState) => state.entryStore.entryInfos, + entryInfos => ({ + ...entriesStateSyncSpec, + getInfoHash: id => hash(entryInfos[`${ashoatKeyserverID}|${id}`]), + getAllInfosHash: calendarQuery => + getEntryInfosHash(entryInfos, calendarQuery), + getIDs: calendarQuery => getEntryIDs(entryInfos, calendarQuery), + }), + ), +}); function getEntryInfosHash( entryInfos: RawEntryInfos, diff --git a/lib/shared/state-sync/state-sync-spec.js b/lib/shared/state-sync/state-sync-spec.js --- a/lib/shared/state-sync/state-sync-spec.js +++ b/lib/shared/state-sync/state-sync-spec.js @@ -3,7 +3,7 @@ import type { CalendarQuery } from '../../types/entry-types.js'; import type { AppState } from '../../types/redux-types.js'; -export type StateSyncSpec = { +export type StateSyncSpec = { +hashKey: string, +innerHashSpec?: { +hashKey: string, @@ -11,21 +11,17 @@ +rawInfosKey: string, +additionalDeleteCondition?: Info => boolean, }, - +convertClientToServerInfos: ( - infos: Infos, - calendarQuery: CalendarQuery, - ) => Infos, - +selector: (state: AppState) => BoundStateSyncSpec, + +selector: (state: AppState) => BoundStateSyncSpec, }; // All ids specified here (getInfoHash and getIDs) are server ids. // E.g. in the case of threadStore or entryStore the keyserver prefix // needs to be handled additionaly -export type BoundStateSyncSpec = { +export type BoundStateSyncSpec = { // If these function depend on background hashing that is still not complete // they should return null, to indicate that the hashes aren't available yet +getInfoHash: (id: string) => ?number, +getAllInfosHash: (query: CalendarQuery) => ?number, +getIDs: (query: CalendarQuery) => ?Array, - ...StateSyncSpec, + ...StateSyncSpec, }; diff --git a/lib/shared/state-sync/state-sync-specs.js b/lib/shared/state-sync/state-sync-specs.js --- a/lib/shared/state-sync/state-sync-specs.js +++ b/lib/shared/state-sync/state-sync-specs.js @@ -14,5 +14,5 @@ }); (stateSyncSpecs: { - +[string]: StateSyncSpec, + +[string]: StateSyncSpec, }); diff --git a/lib/shared/state-sync/threads-state-sync-spec.js b/lib/shared/state-sync/threads-state-sync-spec.js --- a/lib/shared/state-sync/threads-state-sync-spec.js +++ b/lib/shared/state-sync/threads-state-sync-spec.js @@ -1,48 +1,34 @@ // @flow import { createSelector } from 'reselect'; -import t from 'tcomb'; import type { StateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types.js'; -import { - type RawThreadInfos, - type RawThreadInfo, - rawThreadInfoValidator, -} from '../../types/thread-types.js'; -import { convertClientIDsToServerIDs } from '../../utils/conversion-utils.js'; +import { type RawThreadInfo } from '../../types/thread-types.js'; import { combineUnorderedHashes, values } from '../../utils/objects.js'; -import { ashoatKeyserverID, tID } from '../../utils/validation-utils.js'; +import { ashoatKeyserverID } from '../../utils/validation-utils.js'; -export const threadsStateSyncSpec: StateSyncSpec< - RawThreadInfos, - RawThreadInfo, -> = Object.freeze({ - hashKey: 'threadInfos', - innerHashSpec: { - hashKey: 'threadInfo', - deleteKey: 'deleteThreadIDs', - rawInfosKey: 'rawThreadInfos', +export const threadsStateSyncSpec: StateSyncSpec = Object.freeze( + { + hashKey: 'threadInfos', + innerHashSpec: { + hashKey: 'threadInfo', + deleteKey: 'deleteThreadIDs', + rawInfosKey: 'rawThreadInfos', + }, + selector: createSelector( + (state: AppState) => state.integrityStore.threadHashes, + (state: AppState) => state.integrityStore.threadHashingComplete, + (threadHashes, threadHashingComplete) => ({ + ...threadsStateSyncSpec, + getInfoHash: id => threadHashes[`${ashoatKeyserverID}|${id}`], + getAllInfosHash: threadHashingComplete + ? () => combineUnorderedHashes(values(threadHashes)) + : () => null, + getIDs: threadHashingComplete + ? () => Object.keys(threadHashes).map(id => id.split('|')[1]) + : () => null, + }), + ), }, - convertClientToServerInfos(infos: RawThreadInfos) { - return convertClientIDsToServerIDs( - ashoatKeyserverID, - t.dict(tID, rawThreadInfoValidator), - infos, - ); - }, - selector: createSelector( - (state: AppState) => state.integrityStore.threadHashes, - (state: AppState) => state.integrityStore.threadHashingComplete, - (threadHashes, threadHashingComplete) => ({ - ...threadsStateSyncSpec, - getInfoHash: id => threadHashes[`${ashoatKeyserverID}|${id}`], - getAllInfosHash: threadHashingComplete - ? () => combineUnorderedHashes(values(threadHashes)) - : () => null, - getIDs: threadHashingComplete - ? () => Object.keys(threadHashes).map(id => id.split('|')[1]) - : () => null, - }), - ), -}); +); diff --git a/lib/shared/state-sync/users-state-sync-spec.js b/lib/shared/state-sync/users-state-sync-spec.js --- a/lib/shared/state-sync/users-state-sync-spec.js +++ b/lib/shared/state-sync/users-state-sync-spec.js @@ -4,41 +4,27 @@ import type { StateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types'; -import { - type UserInfo, - type UserInfos, - userInfosValidator, -} from '../../types/user-types.js'; -import { convertClientIDsToServerIDs } from '../../utils/conversion-utils.js'; +import { type UserInfo } from '../../types/user-types.js'; import { combineUnorderedHashes, hash } from '../../utils/objects.js'; -import { ashoatKeyserverID } from '../../utils/validation-utils.js'; -export const usersStateSyncSpec: StateSyncSpec = - Object.freeze({ - hashKey: 'userInfos', - innerHashSpec: { - hashKey: 'userInfo', - deleteKey: 'deleteUserInfoIDs', - rawInfosKey: 'userInfos', - additionalDeleteCondition(user: UserInfo) { - return !user.username; - }, +export const usersStateSyncSpec: StateSyncSpec = Object.freeze({ + hashKey: 'userInfos', + innerHashSpec: { + hashKey: 'userInfo', + deleteKey: 'deleteUserInfoIDs', + rawInfosKey: 'userInfos', + additionalDeleteCondition(user: UserInfo) { + return !user.username; }, - convertClientToServerInfos(infos: UserInfos) { - return convertClientIDsToServerIDs( - ashoatKeyserverID, - userInfosValidator, - infos, - ); - }, - selector: createSelector( - (state: AppState) => state.userStore.userInfos, - userInfos => ({ - ...usersStateSyncSpec, - getInfoHash: id => hash(userInfos[id]), - getAllInfosHash: () => - combineUnorderedHashes(Object.values(userInfos).map(hash)), - getIDs: () => Object.keys(userInfos), - }), - ), - }); + }, + selector: createSelector( + (state: AppState) => state.userStore.userInfos, + userInfos => ({ + ...usersStateSyncSpec, + getInfoHash: id => hash(userInfos[id]), + getAllInfosHash: () => + combineUnorderedHashes(Object.values(userInfos).map(hash)), + getIDs: () => Object.keys(userInfos), + }), + ), +});