2020-11-06 17:36:37 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
|
2020-12-02 18:11:54 +00:00
|
|
|
import { StateType } from '../reducer';
|
|
|
|
import {
|
|
|
|
CallingStateType,
|
|
|
|
CallsByConversationType,
|
|
|
|
DirectCallStateType,
|
2021-06-17 17:15:10 +00:00
|
|
|
GroupCallStateType,
|
2020-12-02 18:11:54 +00:00
|
|
|
} from '../ducks/calling';
|
2020-11-13 19:57:55 +00:00
|
|
|
import { CallMode, CallState } from '../../types/Calling';
|
2020-12-07 20:43:19 +00:00
|
|
|
import { getOwn } from '../../util/getOwn';
|
2020-11-06 17:36:37 +00:00
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
export type CallStateType = DirectCallStateType | GroupCallStateType;
|
|
|
|
|
2020-12-02 18:11:54 +00:00
|
|
|
const getCalling = (state: StateType): CallingStateType => state.calling;
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
export const getActiveCallState = createSelector(
|
2021-01-29 22:58:28 +00:00
|
|
|
getCalling,
|
2021-06-17 17:15:10 +00:00
|
|
|
(state: CallingStateType) => state.activeCallState
|
2021-01-29 22:58:28 +00:00
|
|
|
);
|
|
|
|
|
2020-12-07 20:43:19 +00:00
|
|
|
export const getCallsByConversation = createSelector(
|
2020-12-02 18:11:54 +00:00
|
|
|
getCalling,
|
|
|
|
(state: CallingStateType): CallsByConversationType =>
|
|
|
|
state.callsByConversation
|
|
|
|
);
|
2020-11-06 17:36:37 +00:00
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
export type CallSelectorType = (
|
|
|
|
conversationId: string
|
|
|
|
) => CallStateType | undefined;
|
2020-12-07 20:43:19 +00:00
|
|
|
export const getCallSelector = createSelector(
|
|
|
|
getCallsByConversation,
|
2021-06-17 17:15:10 +00:00
|
|
|
(callsByConversation: CallsByConversationType): CallSelectorType => (
|
|
|
|
conversationId: string
|
|
|
|
) => getOwn(callsByConversation, conversationId)
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getActiveCall = createSelector(
|
|
|
|
getActiveCallState,
|
|
|
|
getCallSelector,
|
|
|
|
(activeCallState, callSelector): undefined | CallStateType => {
|
|
|
|
if (activeCallState && activeCallState.conversationId) {
|
|
|
|
return callSelector(activeCallState.conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const isInCall = createSelector(
|
|
|
|
getActiveCall,
|
|
|
|
(call: CallStateType | undefined): boolean => Boolean(call)
|
2020-12-07 20:43:19 +00:00
|
|
|
);
|
|
|
|
|
2020-11-06 17:36:37 +00:00
|
|
|
// In theory, there could be multiple incoming calls. In practice, neither RingRTC nor the
|
|
|
|
// UI are ready to handle this.
|
|
|
|
export const getIncomingCall = createSelector(
|
|
|
|
getCallsByConversation,
|
2020-12-02 18:11:54 +00:00
|
|
|
(
|
|
|
|
callsByConversation: CallsByConversationType
|
|
|
|
): undefined | DirectCallStateType => {
|
2020-11-13 19:57:55 +00:00
|
|
|
const result = Object.values(callsByConversation).find(
|
|
|
|
call =>
|
|
|
|
call.callMode === CallMode.Direct &&
|
|
|
|
call.isIncoming &&
|
|
|
|
call.callState === CallState.Ringing
|
|
|
|
);
|
|
|
|
// TypeScript needs a little help to be sure that this is a direct call.
|
|
|
|
return result?.callMode === CallMode.Direct ? result : undefined;
|
|
|
|
}
|
2020-11-06 17:36:37 +00:00
|
|
|
);
|