signal-desktop/ts/state/ducks/callingHelpers.ts

134 lines
3.8 KiB
TypeScript
Raw Normal View History

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 {
CallState,
GroupCallConnectionState,
GroupCallJoinState,
} from '../../types/Calling';
import { CallMode } from '../../types/CallDisposition';
import type { CallingConversationType } from '../../types/Calling';
import type { AciString } from '../../types/ServiceId';
2023-07-18 23:57:38 +00:00
import type {
DirectCallStateType,
CallsByConversationType,
GroupCallPeekInfoType,
GroupCallStateType,
GroupCallParticipantInfoType,
ActiveCallStateType,
2023-07-18 23:57:38 +00:00
} from './calling';
export const MAX_CALL_PARTICIPANTS_FOR_DEFAULT_MUTE = 8;
2023-07-18 23:57:38 +00:00
// 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.
// Adhoc calls can not be incoming, so we don't look for them here.
export const getRingingCall = (
2023-07-18 23:57:38 +00:00
callsByConversation: Readonly<CallsByConversationType>,
activeCallState: ActiveCallStateType | undefined,
ourAci: AciString
): DirectCallStateType | GroupCallStateType | undefined => {
const callList = Object.values(callsByConversation);
const ringingDirect = callList.find(call => {
if (call.callMode !== CallMode.Direct) {
return false;
}
if (
activeCallState?.state === 'Active' &&
activeCallState.conversationId !== call.conversationId
) {
return false;
}
return isRinging(call.callState) && call.callEndedReason == null;
});
if (ringingDirect) {
return ringingDirect;
}
return callList.find(call => {
if (call.callMode !== CallMode.Group) {
return false;
}
if (
activeCallState?.state === 'Active' &&
activeCallState.conversationId !== call.conversationId
) {
return false;
}
// Outgoing - ringerAci is not set for outgoing group calls
if (
activeCallState?.state === 'Active' &&
activeCallState.conversationId === call.conversationId &&
activeCallState.outgoingRing &&
isConnected(call.connectionState) &&
isJoined(call.joinState) &&
!hasRemoteParticipants(call.remoteParticipants)
) {
return true;
2023-07-18 23:57:38 +00:00
}
// Incoming
return (
call.ringerAci &&
call.ringerAci !== ourAci &&
!isConnected(call.connectionState) &&
!isJoined(call.joinState) &&
isAnybodyElseInGroupCall(call.peekInfo, ourAci)
);
2023-07-18 23:57:38 +00:00
});
};
2023-07-18 23:57:38 +00:00
export const isAnybodyElseInGroupCall = (
peekInfo: undefined | Readonly<Pick<GroupCallPeekInfoType, 'acis'>>,
ourAci: AciString
): boolean => Boolean(peekInfo?.acis.some(id => id !== ourAci));
export const isAnybodyInGroupCall = (
peekInfo: undefined | Readonly<Pick<GroupCallPeekInfoType, 'acis'>>
): boolean => {
if (!peekInfo?.acis) {
return false;
}
return peekInfo.acis.length > 0;
};
export const isGroupCallActiveOnServer = (
peekInfo: undefined | Readonly<Pick<GroupCallPeekInfoType, 'eraId'>>
): boolean => {
return Boolean(peekInfo?.eraId);
};
export function isLonelyGroup(conversation: CallingConversationType): boolean {
return (conversation.sortedGroupMembers?.length ?? 0) < 2;
}
function isRinging(callState: CallState | undefined): boolean {
return callState === CallState.Prering || callState === CallState.Ringing;
}
function isConnected(connectionState: GroupCallConnectionState): boolean {
return (
connectionState === GroupCallConnectionState.Connecting ||
connectionState === GroupCallConnectionState.Connected
);
}
function isJoined(joinState: GroupCallJoinState): boolean {
return joinState !== GroupCallJoinState.NotJoined;
}
function hasRemoteParticipants(
remoteParticipants: Array<GroupCallParticipantInfoType>
): boolean {
return remoteParticipants.length > 0;
}