2023-07-18 23:57:38 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
// Note that this file should not important any binary addons or Node.js modules
|
|
|
|
// because it can be imported by storybook
|
|
|
|
import {
|
|
|
|
CallMode,
|
|
|
|
CallState,
|
|
|
|
GroupCallConnectionState,
|
|
|
|
} from '../../types/Calling';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { AciString } from '../../types/ServiceId';
|
2023-07-18 23:57:38 +00:00
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
|
|
|
import type {
|
|
|
|
DirectCallStateType,
|
|
|
|
CallsByConversationType,
|
|
|
|
GroupCallPeekInfoType,
|
|
|
|
GroupCallStateType,
|
|
|
|
} from './calling';
|
|
|
|
|
|
|
|
// In theory, there could be multiple incoming calls, or an incoming call while there's
|
|
|
|
// an active call. In practice, the UI is not ready for this, and RingRTC doesn't
|
|
|
|
// support it for direct calls.
|
|
|
|
export const getIncomingCall = (
|
|
|
|
callsByConversation: Readonly<CallsByConversationType>,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: AciString
|
2023-07-18 23:57:38 +00:00
|
|
|
): undefined | DirectCallStateType | GroupCallStateType =>
|
|
|
|
Object.values(callsByConversation).find(call => {
|
|
|
|
switch (call.callMode) {
|
|
|
|
case CallMode.Direct:
|
|
|
|
return call.isIncoming && call.callState === CallState.Ringing;
|
|
|
|
case CallMode.Group:
|
|
|
|
return (
|
2023-08-10 16:43:33 +00:00
|
|
|
call.ringerAci &&
|
2023-07-18 23:57:38 +00:00
|
|
|
call.connectionState === GroupCallConnectionState.NotConnected &&
|
2023-08-10 16:43:33 +00:00
|
|
|
isAnybodyElseInGroupCall(call.peekInfo, ourAci)
|
2023-07-18 23:57:38 +00:00
|
|
|
);
|
|
|
|
default:
|
|
|
|
throw missingCaseError(call);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const isAnybodyElseInGroupCall = (
|
2023-08-10 16:43:33 +00:00
|
|
|
peekInfo: undefined | Readonly<Pick<GroupCallPeekInfoType, 'acis'>>,
|
|
|
|
ourAci: AciString
|
|
|
|
): boolean => Boolean(peekInfo?.acis.some(id => id !== ourAci));
|