diff --git a/lib/actions/activity-actions.js b/lib/actions/activity-actions.js --- a/lib/actions/activity-actions.js +++ b/lib/actions/activity-actions.js @@ -43,8 +43,15 @@ ); } + const sortedActivityUpdates: { + [keyserverID: string]: $ReadOnlyArray, + } = {}; + for (const keyserverID in requests) { + sortedActivityUpdates[keyserverID] = requests[keyserverID].updates; + } + return { - activityUpdates, + activityUpdates: sortedActivityUpdates, result: { unfocusedToUnread, }, diff --git a/lib/reducers/connection-reducer.js b/lib/reducers/connection-reducer.js --- a/lib/reducers/connection-reducer.js +++ b/lib/reducers/connection-reducer.js @@ -1,16 +1,12 @@ // @flow -import { unsupervisedBackgroundActionType } from './lifecycle-state-reducer.js'; -import { updateActivityActionTypes } from '../actions/activity-actions.js'; import { logOutActionTypes, deleteAccountActionTypes, } from '../actions/user-actions.js'; -import { queueActivityUpdatesActionType } from '../types/activity-types.js'; import { type BaseAction } from '../types/redux-types.js'; import { type ConnectionInfo, - updateConnectionStatusActionType, setLateResponseActionType, updateDisconnectedBarActionType, } from '../types/socket-types.js'; @@ -20,40 +16,7 @@ state: ConnectionInfo, action: BaseAction, ): ConnectionInfo { - if (action.type === updateConnectionStatusActionType) { - return { ...state, status: action.payload.status, lateResponses: [] }; - } else if (action.type === unsupervisedBackgroundActionType) { - return { ...state, status: 'disconnected', lateResponses: [] }; - } else if (action.type === queueActivityUpdatesActionType) { - const { activityUpdates } = action.payload; - return { - ...state, - queuedActivityUpdates: [ - ...state.queuedActivityUpdates.filter(existingUpdate => { - for (const activityUpdate of activityUpdates) { - if ( - ((existingUpdate.focus && activityUpdate.focus) || - (existingUpdate.focus === false && - activityUpdate.focus !== undefined)) && - existingUpdate.threadID === activityUpdate.threadID - ) { - return false; - } - } - return true; - }), - ...activityUpdates, - ], - }; - } else if (action.type === updateActivityActionTypes.success) { - const { payload } = action; - return { - ...state, - queuedActivityUpdates: state.queuedActivityUpdates.filter( - activityUpdate => !payload.activityUpdates.includes(activityUpdate), - ), - }; - } else if ( + if ( action.type === logOutActionTypes.success || action.type === deleteAccountActionTypes.success || (action.type === setNewSessionActionType && diff --git a/lib/reducers/keyserver-reducer.js b/lib/reducers/keyserver-reducer.js --- a/lib/reducers/keyserver-reducer.js +++ b/lib/reducers/keyserver-reducer.js @@ -2,6 +2,8 @@ import reduceConnectionInfo from './connection-reducer.js'; import { reduceDeviceToken } from './device-token-reducer.js'; +import { unsupervisedBackgroundActionType } from './lifecycle-state-reducer.js'; +import { updateActivityActionTypes } from '../actions/activity-actions.js'; import { updateLastCommunicatedPlatformDetailsActionType } from '../actions/device-actions.js'; import { addKeyserverActionType } from '../actions/keyserver-actions.js'; import { siweAuthActionTypes } from '../actions/siwe-actions.js'; @@ -11,11 +13,13 @@ logOutActionTypes, deleteAccountActionTypes, } from '../actions/user-actions.js'; +import { queueActivityUpdatesActionType } from '../types/activity-types.js'; import type { KeyserverStore } from '../types/keyserver-types'; import type { BaseAction } from '../types/redux-types.js'; import { fullStateSyncActionType, incrementalStateSyncActionType, + updateConnectionStatusActionType, } from '../types/socket-types.js'; import { processUpdatesActionType } from '../types/update-types.js'; import { setNewSessionActionType } from '../utils/action-utils.js'; @@ -159,6 +163,93 @@ }, }, }; + } else if (action.type === updateConnectionStatusActionType) { + const { keyserverID, status } = action.payload; + state = { + ...state, + keyserverInfos: { + ...state.keyserverInfos, + [keyserverID]: { + ...state.keyserverInfos[keyserverID], + connection: { + ...state.keyserverInfos[keyserverID].connection, + status, + lateResponses: [], + }, + }, + }, + }; + } else if (action.type === unsupervisedBackgroundActionType) { + const { keyserverID } = action.payload; + state = { + ...state, + keyserverInfos: { + ...state.keyserverInfos, + [keyserverID]: { + ...state.keyserverInfos[keyserverID], + connection: { + ...state.keyserverInfos[keyserverID].connection, + status: 'disconnected', + lateResponses: [], + }, + }, + }, + }; + } else if (action.type === queueActivityUpdatesActionType) { + const { activityUpdates, keyserverID } = action.payload; + const oldConnection = state.keyserverInfos[keyserverID].connection; + const connection = { + ...oldConnection, + queuedActivityUpdates: [ + ...oldConnection.queuedActivityUpdates.filter(existingUpdate => { + for (const activityUpdate of activityUpdates) { + if ( + ((existingUpdate.focus && activityUpdate.focus) || + (existingUpdate.focus === false && + activityUpdate.focus !== undefined)) && + existingUpdate.threadID === activityUpdate.threadID + ) { + return false; + } + } + return true; + }), + ...activityUpdates, + ], + }; + + state = { + ...state, + keyserverInfos: { + ...state.keyserverInfos, + [keyserverID]: { + ...state.keyserverInfos[keyserverID], + connection, + }, + }, + }; + } else if (action.type === updateActivityActionTypes.success) { + const { activityUpdates } = action.payload; + let keyserverInfos = { ...state.keyserverInfos }; + for (const keyserverID in activityUpdates) { + const oldConnection = keyserverInfos[keyserverID].connection; + const queuedActivityUpdates = oldConnection.queuedActivityUpdates.filter( + activityUpdate => + !activityUpdates[keyserverID].includes(activityUpdate), + ); + + keyserverInfos = { + ...keyserverInfos, + [keyserverID]: { + ...keyserverInfos[keyserverID], + connection: { ...oldConnection, queuedActivityUpdates }, + }, + }; + } + state = { + ...state, + keyserverInfos, + }; } const connection = reduceConnectionInfo( diff --git a/lib/socket/activity-handler.react.js b/lib/socket/activity-handler.react.js --- a/lib/socket/activity-handler.react.js +++ b/lib/socket/activity-handler.react.js @@ -106,7 +106,7 @@ if (activityUpdates.length > 0) { dispatch({ type: queueActivityUpdatesActionType, - payload: { activityUpdates }, + payload: { activityUpdates, keyserverID: ashoatKeyserverID }, }); } diff --git a/lib/socket/socket.react.js b/lib/socket/socket.react.js --- a/lib/socket/socket.react.js +++ b/lib/socket/socket.react.js @@ -160,7 +160,7 @@ } this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: newStatus }, + payload: { status: newStatus, keyserverID: ashoatKeyserverID }, }); const socket = this.props.openSocket(); @@ -211,7 +211,7 @@ markSocketInitialized() { this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: 'connected' }, + payload: { status: 'connected', keyserverID: ashoatKeyserverID }, }); this.resetPing(); } @@ -232,7 +232,7 @@ this.stopPing(); this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: 'disconnecting' }, + payload: { status: 'disconnecting', keyserverID: ashoatKeyserverID }, }); if (!activityUpdatePending) { this.finishClosingSocket(); @@ -245,7 +245,10 @@ if (status !== 'forcedDisconnecting' && status !== 'disconnected') { this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: 'forcedDisconnecting' }, + payload: { + status: 'forcedDisconnecting', + keyserverID: ashoatKeyserverID, + }, }); } this.finishClosingSocket(); @@ -269,7 +272,7 @@ if (this.props.connection.status !== 'disconnected') { this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: 'disconnected' }, + payload: { status: 'disconnected', keyserverID: ashoatKeyserverID }, }); } if (this.reopenConnectionAfterClosing) { @@ -537,7 +540,7 @@ if (!handled && status !== 'disconnected') { this.props.dispatch({ type: updateConnectionStatusActionType, - payload: { status: 'disconnected' }, + payload: { status: 'disconnected', keyserverID: ashoatKeyserverID }, }); } }; @@ -612,7 +615,7 @@ this.props.dispatch({ type: updateActivityActionTypes.success, payload: { - activityUpdates: queuedActivityUpdates, + activityUpdates: { [ashoatKeyserverID]: queuedActivityUpdates }, result: activityUpdateMessage.payload, }, }); @@ -803,7 +806,7 @@ } this.props.dispatch({ type: unsupervisedBackgroundActionType, - payload: null, + payload: { keyserverID: ashoatKeyserverID }, }); return true; }; diff --git a/lib/types/activity-types.js b/lib/types/activity-types.js --- a/lib/types/activity-types.js +++ b/lib/types/activity-types.js @@ -29,13 +29,14 @@ }); export type ActivityUpdateSuccessPayload = { - +activityUpdates: $ReadOnlyArray, + +activityUpdates: { +[keyserverID: string]: $ReadOnlyArray }, +result: UpdateActivityResult, }; export const queueActivityUpdatesActionType = 'QUEUE_ACTIVITY_UPDATES'; export type QueueActivityUpdatesPayload = { +activityUpdates: $ReadOnlyArray, + +keyserverID: string, }; export type SetThreadUnreadStatusRequest = { diff --git a/lib/types/redux-types.js b/lib/types/redux-types.js --- a/lib/types/redux-types.js +++ b/lib/types/redux-types.js @@ -800,7 +800,7 @@ } | { +type: 'UNSUPERVISED_BACKGROUND', - +payload?: void, + +payload: { +keyserverID: string }, } | { +type: 'UPDATE_LIFECYCLE_STATE', diff --git a/lib/types/socket-types.js b/lib/types/socket-types.js --- a/lib/types/socket-types.js +++ b/lib/types/socket-types.js @@ -510,6 +510,7 @@ export const updateConnectionStatusActionType = 'UPDATE_CONNECTION_STATUS'; export type UpdateConnectionStatusPayload = { +status: ConnectionStatus, + +keyserverID: string, }; export const setLateResponseActionType = 'SET_LATE_RESPONSE';