diff --git a/lib/selectors/keyserver-selectors.js b/lib/selectors/keyserver-selectors.js index bda27f7da..c5beff4c5 100644 --- a/lib/selectors/keyserver-selectors.js +++ b/lib/selectors/keyserver-selectors.js @@ -1,61 +1,92 @@ // @flow import { createSelector } from 'reselect'; import type { PlatformDetails } from '../types/device-types'; -import type { KeyserverInfo } from '../types/keyserver-types'; +import type { + KeyserverInfo, + KeyserverInfos, + SelectedKeyserverInfo, +} from '../types/keyserver-types'; import type { AppState } from '../types/redux-types.js'; import type { ConnectionInfo } from '../types/socket-types.js'; +import type { UserInfos } from '../types/user-types.js'; import { ashoatKeyserverID } from '../utils/validation-utils.js'; const cookieSelector: (state: AppState) => ?string = (state: AppState) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.cookie; const cookiesSelector: (state: AppState) => { +[keyserverID: string]: string, } = createSelector( (state: AppState) => state.keyserverStore.keyserverInfos, (infos: { +[key: string]: KeyserverInfo }) => { const cookies = {}; for (const keyserverID in infos) { cookies[keyserverID] = infos[keyserverID].cookie; } return cookies; }, ); const sessionIDSelector: (state: AppState) => ?string = (state: AppState) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.sessionID; const updatesCurrentAsOfSelector: (state: AppState) => number = ( state: AppState, ) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.updatesCurrentAsOf ?? 0; const currentAsOfSelector: (state: AppState) => number = (state: AppState) => state.messageStore.currentAsOf[ashoatKeyserverID] ?? 0; const urlPrefixSelector: (state: AppState) => ?string = (state: AppState) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.urlPrefix; const connectionSelector: (state: AppState) => ?ConnectionInfo = ( state: AppState, ) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.connection; const lastCommunicatedPlatformDetailsSelector: ( state: AppState, ) => ?PlatformDetails = (state: AppState) => state.keyserverStore.keyserverInfos[ashoatKeyserverID] ?.lastCommunicatedPlatformDetails; +const selectedKeyserversSelector: ( + state: AppState, +) => $ReadOnlyArray = createSelector( + (state: AppState) => state.keyserverStore.keyserverInfos, + (state: AppState) => state.userStore.userInfos, + (keyserverInfos: KeyserverInfos, userInfos: UserInfos) => { + const result = []; + + for (const key in keyserverInfos) { + const keyserverInfo = keyserverInfos[key]; + const keyserverAdminUsername = userInfos[key]?.username; + + if (!keyserverAdminUsername) { + continue; + } + + result.push({ + keyserverAdminUsername, + keyserverInfo, + }); + } + return result; + }, +); + export { cookieSelector, cookiesSelector, sessionIDSelector, updatesCurrentAsOfSelector, currentAsOfSelector, urlPrefixSelector, connectionSelector, lastCommunicatedPlatformDetailsSelector, + selectedKeyserversSelector, }; diff --git a/lib/types/keyserver-types.js b/lib/types/keyserver-types.js index 7df8c84a9..b1de12f20 100644 --- a/lib/types/keyserver-types.js +++ b/lib/types/keyserver-types.js @@ -1,38 +1,43 @@ // @flow import t, { type TInterface } from 'tcomb'; import type { PlatformDetails } from './device-types.js'; import { connectionInfoValidator } from './socket-types.js'; import type { ConnectionInfo } from './socket-types.js'; import { tShape, tPlatformDetails } from '../utils/validation-utils.js'; export type KeyserverInfo = { +cookie: ?string, +sessionID?: ?string, +updatesCurrentAsOf: number, // millisecond timestamp +urlPrefix: string, +connection: ConnectionInfo, +lastCommunicatedPlatformDetails: ?PlatformDetails, }; export type KeyserverInfos = { +[key: string]: KeyserverInfo }; export type KeyserverStore = { +keyserverInfos: KeyserverInfos, }; +export type SelectedKeyserverInfo = { + +keyserverAdminUsername: string, + +keyserverInfo: KeyserverInfo, +}; + export const keyserverInfoValidator: TInterface = tShape({ cookie: t.maybe(t.String), sessionID: t.maybe(t.String), updatesCurrentAsOf: t.Number, urlPrefix: t.String, connection: connectionInfoValidator, lastCommunicatedPlatformDetails: t.maybe(tPlatformDetails), }); export const keyserverStoreValidator: TInterface = tShape({ keyserverInfos: t.dict(t.String, keyserverInfoValidator), });