fix(calls): Fix incorrect data type being sent over net

This commit is contained in:
Taso committed 2021-07-29 20:29:27 -04:00
1 parent 60b9e35ae8
commit 5263a83d1f
10 files changed
+99 -107

No files matched your search

+3 -1
View File
@@ -21,12 +21,14 @@ const useStyles = makeStyles({
}, },
}); });
export const CallModal = () => { export const CallModal: React.FC = () => {
const [settings] = useSettings(); const [settings] = useSettings();
const { call } = useCall(); const { call } = useCall();
const classes = useStyles(); const classes = useStyles();
if (!call) return null;
return ( return (
<AppWrapper> <AppWrapper>
<AppContent <AppContent
+4 -2
View File
@@ -1,8 +1,8 @@
import { atom } from 'recoil'; import { atom, useRecoilState } from 'recoil';
import { ActiveCall } from '../../../../../typings/call'; import { ActiveCall } from '../../../../../typings/call';
export const callerState = { export const callerState = {
currentCall: atom<ActiveCall>({ currentCall: atom<ActiveCall | null>({
key: 'currentCall', key: 'currentCall',
default: null, default: null,
}), }),
@@ -15,3 +15,5 @@ export const callerState = {
default: { ms: 0, s: 0, m: 0, h: 0 }, default: { ms: 0, s: 0, m: 0, h: 0 },
}), }),
}; };
export const useCurrentCall = () => useRecoilState(callerState.currentCall);
+3 -4
View File
@@ -1,6 +1,5 @@
import { useRecoilState } from 'recoil';
import { ActiveCall } from '../../../../../typings/call'; import { ActiveCall } from '../../../../../typings/call';
import { callerState } from './state'; import { useCurrentCall } from './state';
import { CallEvents } from '../../../../../typings/call'; import { CallEvents } from '../../../../../typings/call';
import { fetchNui } from '../../../utils/fetchNui'; import { fetchNui } from '../../../utils/fetchNui';
import { useCallback } from 'react'; import { useCallback } from 'react';
@@ -11,7 +10,7 @@ import { ServerPromiseResp } from '../../../../../typings/common';
interface CallHook { interface CallHook {
call: ActiveCall; call: ActiveCall;
setCall: (details: any) => void; setCall: (call: ActiveCall) => void;
acceptCall(): void; acceptCall(): void;
rejectCall(): void; rejectCall(): void;
endCall(): void; endCall(): void;
@@ -21,7 +20,7 @@ interface CallHook {
// const TIME_TILL_AUTO_HANGUP = 15000; // const TIME_TILL_AUTO_HANGUP = 15000;
export const useCall = (): CallHook => { export const useCall = (): CallHook => {
const [call, setCall] = useRecoilState(callerState.currentCall); const [call, setCall] = useCurrentCall();
// const [dialRing, setDialRing] = useState(false); // const [dialRing, setDialRing] = useState(false);
const myPhoneNumber = useMyPhoneNumber(); const myPhoneNumber = useMyPhoneNumber();
const { t } = useTranslation(); const { t } = useTranslation();
@@ -27,12 +27,18 @@ export const useCallNotifications = () => {
notificationIcon, notificationIcon,
}; };
const clearNotification = (): void => {
removeId(NOTIFICATION_ID);
stop();
};
const setNotification = (call: ActiveCall) => { const setNotification = (call: ActiveCall) => {
stop(); stop();
if (!call || !call.active) { if (!call) {
removeId(NOTIFICATION_ID); removeId(NOTIFICATION_ID);
return; return;
} }
if (call.is_accepted) { if (call.is_accepted) {
removeId(NOTIFICATION_ID); removeId(NOTIFICATION_ID);
addNotification({ addNotification({
@@ -70,5 +76,5 @@ export const useCallNotifications = () => {
} }
}; };
return { setNotification }; return { setNotification, clearNotification };
}; };
+8 -8
View File
@@ -3,7 +3,6 @@ import { useSetRecoilState } from 'recoil';
import { useCall } from './useCall'; import { useCall } from './useCall';
import { CallEvents, ActiveCall } from '../../../../../typings/call'; import { CallEvents, ActiveCall } from '../../../../../typings/call';
// import InjectDebugData from '../../debug/InjectDebugData';
import { useCallModal } from './useCallModal'; import { useCallModal } from './useCallModal';
import { useHistory, useLocation } from 'react-router-dom'; import { useHistory, useLocation } from 'react-router-dom';
import { callerState } from './state'; import { callerState } from './state';
@@ -34,9 +33,9 @@ export const useCallService = () => {
const history = useHistory(); const history = useHistory();
const { pathname } = useLocation(); const { pathname } = useLocation();
const { call, setCall } = useCall(); const { setCall } = useCall();
const { setNotification } = useCallNotifications(); const { setNotification, clearNotification } = useCallNotifications();
const setModal = useSetRecoilState(callerState.callModal); const setModal = useSetRecoilState(callerState.callModal);
@@ -55,10 +54,11 @@ export const useCallService = () => {
} }
}, [history, modal, pathname, modalHasBeenOpenedThisCall]); }, [history, modal, pathname, modalHasBeenOpenedThisCall]);
const _setCall = (_call: ActiveCall) => { useNuiEvent<ActiveCall | null>('CALL', CallEvents.SET_CALLER, (callData) => {
setCall({ ...call, ..._call }); setCall(callData);
setNotification(_call);
}; if (!callData) return clearNotification();
useNuiEvent('CALL', CallEvents.SET_CALLER, _setCall); setNotification(callData);
});
useNuiEvent('CALL', CallEvents.SET_CALL_MODAL, setModal); useNuiEvent('CALL', CallEvents.SET_CALL_MODAL, setModal);
}; };
+15 -13
View File
@@ -1,6 +1,6 @@
import { import {
ActiveCall,
CallEvents, CallEvents,
CallWasAcceptedEvent,
EndCallDTO, EndCallDTO,
InitializeCallDTO, InitializeCallDTO,
StartCallEventData, StartCallEventData,
@@ -11,27 +11,32 @@ import { CallService } from './cl_calls.service';
import { animationService } from '../animations/animation.controller'; import { animationService } from '../animations/animation.controller';
import { emitNetTyped, onNetTyped } from '../../server/utils/miscUtils'; import { emitNetTyped, onNetTyped } from '../../server/utils/miscUtils';
import { RegisterNuiCB, RegisterNuiProxy } from '../cl_utils'; import { RegisterNuiCB, RegisterNuiProxy } from '../cl_utils';
import { NuiCallbackFunc } from '@project-error/pe-utils';
import { ClUtils } from '../client'; import { ClUtils } from '../client';
import { ServerPromiseResp } from '../../../typings/common'; import { ServerPromiseResp } from '../../../typings/common';
const callService = new CallService(); const callService = new CallService();
// Will trigger whenever somebody initializes a call to any number
RegisterNuiCB<InitializeCallDTO>(CallEvents.INITIALIZE_CALL, async (data, cb) => { RegisterNuiCB<InitializeCallDTO>(CallEvents.INITIALIZE_CALL, async (data, cb) => {
// If they are already in a call return without initializing a new one.
if (callService.isInCall()) return;
try { try {
const serverRes = await ClUtils.emitNetPromise<ServerPromiseResp<StartCallEventData>>( const serverRes = await ClUtils.emitNetPromise<ServerPromiseResp<ActiveCall>>(
CallEvents.INITIALIZE_CALL, CallEvents.INITIALIZE_CALL,
data, data,
); );
// If something went wrong lets inform the client
if (serverRes.status !== 'ok') { if (serverRes.status !== 'ok') {
cb(serverRes); return cb(serverRes);
} }
const { transmitter, isTransmitter, receiver } = serverRes.data; const { transmitter, isTransmitter, receiver } = serverRes.data;
// Start the process of giving NUI feedback by opening NUI modal
callService.handleStartCall(transmitter, receiver, isTransmitter); callService.handleStartCall(transmitter, receiver, isTransmitter);
cb({}); cb({ status: 'ok' });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
cb({ status: 'error', errorMsg: 'CLIENT_TIMED_OUT' }); cb({ status: 'error', errorMsg: 'CLIENT_TIMED_OUT' });
@@ -49,15 +54,12 @@ RegisterNuiCB<TransmitterNumDTO>(CallEvents.ACCEPT_CALL, (data, cb) => {
cb({}); cb({});
}); });
onNetTyped<CallWasAcceptedEvent>( onNetTyped<ActiveCall>(CallEvents.WAS_ACCEPTED, (callData) => {
CallEvents.WAS_ACCEPTED, callService.handleCallAccepted(callData);
({ channelId, currentCall, isTransmitter }) => { });
callService.handleCallAccepted(channelId, currentCall, isTransmitter);
},
);
// Rejected call // Rejected call
RegisterNuiCB<TransmitterNumDTO>(CallEvents.REJECTED, (data, cb: NuiCallbackFunc) => { RegisterNuiCB<TransmitterNumDTO>(CallEvents.REJECTED, (data, cb) => {
emitNetTyped<TransmitterNumDTO>(CallEvents.REJECTED, data); emitNetTyped<TransmitterNumDTO>(CallEvents.REJECTED, data);
cb({}); cb({});
}); });
+16 -51
View File
@@ -1,5 +1,5 @@
import { IAlertProps } from '../../../typings/alerts'; import { IAlertProps } from '../../../typings/alerts';
import { CallEvents, CallHistoryItem, CallRejectReasons } from '../../../typings/call'; import { ActiveCall, CallEvents, CallRejectReasons } from '../../../typings/call';
import { animationService } from '../animations/animation.controller'; import { animationService } from '../animations/animation.controller';
const exp = (global as any).exports; const exp = (global as any).exports;
@@ -11,33 +11,27 @@ export class CallService {
this.currentCall = 0; this.currentCall = 0;
} }
static sendCallAction<T>(method: CallEvents, data: T): void {
SendNUIMessage({
app: 'CALL',
method,
data,
});
}
isInCall() { isInCall() {
return this.currentCall !== 0; return this.currentCall !== 0;
} }
openCallModal(show: boolean) { openCallModal(show: boolean) {
SendNUIMessage({ CallService.sendCallAction<boolean>(CallEvents.SET_CALL_MODAL, show);
app: 'CALL',
method: CallEvents.SET_CALL_MODAL,
data: show,
});
} }
handleRejectCall() { handleRejectCall() {
// we don't want to reset our UI if we're in a call already. // we don't want to reset our UI if we're in a call already.
if (this.isInCall()) return; if (this.isInCall()) return;
this.openCallModal(false); this.openCallModal(false);
SendNUIMessage({ CallService.sendCallAction(CallEvents.SET_CALLER, null);
app: 'CALL',
method: CallEvents.SET_CALLER,
data: {
transmitter: null,
receiver: null,
isTransmitter: null,
accepted: false,
active: false,
},
});
} }
handleStartCall(transmitter: string, receiver: string, isTransmitter: boolean) { handleStartCall(transmitter: string, receiver: string, isTransmitter: boolean) {
@@ -61,21 +55,10 @@ export class CallService {
}); });
} }
handleCallAccepted(channelId: number, currentCall: CallHistoryItem, isTransmitter: boolean) { handleCallAccepted(callData: ActiveCall) {
this.currentCall = channelId; this.currentCall = callData.channelId;
exp['pma-voice'].setCallChannel(channelId); exp['pma-voice'].setCallChannel(callData.channelId);
// phoneCallStartAnim(); // Trigger call animation only if the call was accepted. CallService.sendCallAction<ActiveCall>(CallEvents.SET_CALLER, callData);
SendNUIMessage({
app: 'CALL',
method: CallEvents.SET_CALLER,
data: {
active: true,
transmitter: currentCall.transmitter,
receiver: currentCall.receiver,
isTransmitter: isTransmitter,
accepted: true,
},
});
} }
handleEndCall() { handleEndCall() {
@@ -83,17 +66,7 @@ export class CallService {
exp['pma-voice'].setCallChannel(0); exp['pma-voice'].setCallChannel(0);
this.openCallModal(false); this.openCallModal(false);
SendNUIMessage({ CallService.sendCallAction<null>(CallEvents.SET_CALLER, null);
app: 'CALL',
method: CallEvents.SET_CALLER,
data: {
transmitter: null,
receiver: null,
isTransmitter: null,
accepted: false,
active: false,
},
});
} }
handleSendAlert(alert: IAlertProps) { handleSendAlert(alert: IAlertProps) {
@@ -103,12 +76,4 @@ export class CallService {
data: alert, data: alert,
}); });
} }
handleFetchCalls(calls: CallHistoryItem[]) {
SendNUIMessage({
app: 'DIALER',
method: CallEvents.SET_CALL_HISTORY,
data: calls,
});
}
} }
+3 -3
View File
@@ -4,14 +4,14 @@ import {
TransmitterNumDTO, TransmitterNumDTO,
EndCallDTO, EndCallDTO,
InitializeCallDTO, InitializeCallDTO,
StartCallEventData, ActiveCall,
} from '../../../typings/call'; } from '../../../typings/call';
import { getSource, onNetTyped } from '../utils/miscUtils'; import { getSource, onNetTyped } from '../utils/miscUtils';
import CallService from './calls.service'; import CallService from './calls.service';
import { callLogger } from './calls.utils'; import { callLogger } from './calls.utils';
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise'; import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
onNetPromise<InitializeCallDTO, StartCallEventData>(CallEvents.INITIALIZE_CALL, (reqObj, resp) => { onNetPromise<InitializeCallDTO, ActiveCall>(CallEvents.INITIALIZE_CALL, (reqObj, resp) => {
CallService.handleInitializeCall(reqObj, resp).catch((e) => { CallService.handleInitializeCall(reqObj, resp).catch((e) => {
resp({ status: 'error', errorMsg: 'SERVER_ERROR' }); resp({ status: 'error', errorMsg: 'SERVER_ERROR' });
callLogger.error(`Error occured handling init call: ${e.message}`); callLogger.error(`Error occured handling init call: ${e.message}`);
@@ -27,7 +27,7 @@ onNetTyped<TransmitterNumDTO>(CallEvents.ACCEPT_CALL, ({ transmitterNumber }) =>
); );
}); });
// Fire and forget event, client doesn't care what response is we reject no matter what // Fire and forget event, client doesn't care what response is we reject no matter what.
// thats the reason its not promise // thats the reason its not promise
onNetTyped<TransmitterNumDTO>(CallEvents.REJECTED, (data) => { onNetTyped<TransmitterNumDTO>(CallEvents.REJECTED, (data) => {
const src = getSource(); const src = getSource();
+27 -17
View File
@@ -2,11 +2,10 @@ import Collection from '@discordjs/collection';
import { import {
CallEvents, CallEvents,
CallHistoryItem, CallHistoryItem,
InitializeCallResp,
InitializeCallDTO, InitializeCallDTO,
EndCallDTO, EndCallDTO,
StartCallEventData, ActiveCall,
CallWasAcceptedEvent, ActiveCallRaw,
} from '../../../typings/call'; } from '../../../typings/call';
import CallsDB, { CallsRepo } from './calls.db'; import CallsDB, { CallsRepo } from './calls.db';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
@@ -14,9 +13,10 @@ import PlayerService from '../players/player.service';
import { callLogger } from './calls.utils'; import { callLogger } from './calls.utils';
import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types'; import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types';
import { emitNetTyped } from '../utils/miscUtils'; import { emitNetTyped } from '../utils/miscUtils';
import { mainLogger } from '../sv_logger';
class CallsService { class CallsService {
private callMap: Collection<string, CallHistoryItem>; private callMap: Collection<string, ActiveCallRaw>;
private readonly callsDB: CallsRepo; private readonly callsDB: CallsRepo;
constructor() { constructor() {
@@ -25,7 +25,7 @@ class CallsService {
callLogger.debug('Call service started'); callLogger.debug('Call service started');
} }
private setCallInMap(transmitterNumber: string, callObj: CallHistoryItem): void { private setCallInMap(transmitterNumber: string, callObj: ActiveCallRaw): void {
this.callMap.set(transmitterNumber, callObj); this.callMap.set(transmitterNumber, callObj);
callLogger.debug(`Call obj set with key ${transmitterNumber}, value:`); callLogger.debug(`Call obj set with key ${transmitterNumber}, value:`);
callLogger.debug(callObj); callLogger.debug(callObj);
@@ -33,7 +33,7 @@ class CallsService {
async handleInitializeCall( async handleInitializeCall(
reqObj: PromiseRequest<InitializeCallDTO>, reqObj: PromiseRequest<InitializeCallDTO>,
resp: PromiseEventResp<StartCallEventData>, resp: PromiseEventResp<ActiveCall>,
): Promise<void> { ): Promise<void> {
// Create initial call data // Create initial call data
const transmittingPlayer = PlayerService.getPlayer(reqObj.source); const transmittingPlayer = PlayerService.getPlayer(reqObj.source);
@@ -53,6 +53,7 @@ class CallsService {
isTransmitter: true, isTransmitter: true,
receiver: reqObj.data.receiverNumber, receiver: reqObj.data.receiverNumber,
isUnavailable: true, isUnavailable: true,
is_accepted: false,
}, },
}); });
} }
@@ -66,7 +67,7 @@ class CallsService {
callLogger.debug(`Receiving Identifier: ${receiverIdentifier}`); callLogger.debug(`Receiving Identifier: ${receiverIdentifier}`);
callLogger.debug(`Receiving source: ${receivingPlayer.source} `); callLogger.debug(`Receiving source: ${receivingPlayer.source} `);
const callObj: CallHistoryItem = { const callObj: ActiveCallRaw = {
identifier: callIdentifier, identifier: callIdentifier,
transmitter: transmitterNumber, transmitter: transmitterNumber,
transmitterSource: transmittingPlayer.source, transmitterSource: transmittingPlayer.source,
@@ -82,6 +83,7 @@ class CallsService {
return resp({ return resp({
status: 'ok', status: 'ok',
data: { data: {
is_accepted: false,
transmitter: transmitterNumber, transmitter: transmitterNumber,
isTransmitter: true, isTransmitter: true,
receiver: reqObj.data.receiverNumber, receiver: reqObj.data.receiverNumber,
@@ -107,15 +109,17 @@ class CallsService {
resp({ resp({
status: 'ok', status: 'ok',
data: { data: {
is_accepted: false,
transmitter: transmitterNumber, transmitter: transmitterNumber,
receiver: reqObj.data.receiverNumber, receiver: reqObj.data.receiverNumber,
isTransmitter: true, isTransmitter: true,
}, },
}); });
emitNetTyped<StartCallEventData>( emitNetTyped<ActiveCall>(
CallEvents.START_CALL, CallEvents.START_CALL,
{ {
is_accepted: false,
transmitter: transmitterNumber, transmitter: transmitterNumber,
receiver: reqObj.data.receiverNumber, receiver: reqObj.data.receiverNumber,
isTransmitter: false, isTransmitter: false,
@@ -126,35 +130,41 @@ class CallsService {
async handleAcceptCall(src: number, transmitterNumber: string): Promise<void> { async handleAcceptCall(src: number, transmitterNumber: string): Promise<void> {
// We retrieve the call that was accepted from the current calls map // We retrieve the call that was accepted from the current calls map
const curCallAccepted = this.callMap.get(transmitterNumber); const targetCallItem = this.callMap.get(transmitterNumber);
// We update its reference // We update its reference
curCallAccepted.is_accepted = true; targetCallItem.is_accepted = true;
const channelId = src; const channelId = src;
await this.callsDB.updateCall(curCallAccepted, true, null); await this.callsDB.updateCall(targetCallItem, true, null);
callLogger.debug(`Call with key ${transmitterNumber} was updated to be accepted`); callLogger.debug(`Call with key ${transmitterNumber} was updated to be accepted`);
// player who is being called // player who is being called
emitNetTyped<CallWasAcceptedEvent>( emitNetTyped<ActiveCall>(
CallEvents.WAS_ACCEPTED, CallEvents.WAS_ACCEPTED,
{ {
currentCall: curCallAccepted, is_accepted: true,
transmitter: transmitterNumber,
receiver: targetCallItem.receiver,
isTransmitter: false, isTransmitter: false,
channelId, channelId,
}, },
curCallAccepted.receiverSource, targetCallItem.receiverSource,
); );
mainLogger.debug(targetCallItem);
// player who is calling // player who is calling
emitNetTyped<CallWasAcceptedEvent>( emitNetTyped<ActiveCall>(
CallEvents.WAS_ACCEPTED, CallEvents.WAS_ACCEPTED,
{ {
currentCall: curCallAccepted, is_accepted: true,
transmitter: transmitterNumber,
receiver: targetCallItem.receiver,
isTransmitter: true, isTransmitter: true,
channelId, channelId,
}, },
curCallAccepted.transmitterSource, targetCallItem.transmitterSource,
); );
} }
+12 -6
View File
@@ -1,19 +1,16 @@
export interface ActiveCall { export interface ActiveCall {
active: boolean;
is_accepted: boolean; is_accepted: boolean;
isTransmitter: boolean; isTransmitter: boolean;
transmitter: string; transmitter: string;
receiver: string; receiver: string;
channelId?: number;
isUnavailable?: boolean;
} }
export interface InitializeCallDTO { export interface InitializeCallDTO {
receiverNumber: string; receiverNumber: string;
} }
export interface InitializeCallResp {
available: boolean;
}
export interface StartCallEventData { export interface StartCallEventData {
transmitter: string; transmitter: string;
receiver: string; receiver: string;
@@ -36,6 +33,16 @@ export interface CallWasAcceptedEvent {
isTransmitter: boolean; isTransmitter: boolean;
} }
export interface ActiveCallRaw {
identifier: string;
transmitter: string;
transmitterSource: number;
receiver: string;
receiverSource: number;
start: string;
is_accepted: boolean;
}
export interface CallHistoryItem { export interface CallHistoryItem {
id?: number; id?: number;
identifier: string; identifier: string;
@@ -65,6 +72,5 @@ export enum CallEvents {
FETCH_CALLS = 'npwd:fetchCalls', FETCH_CALLS = 'npwd:fetchCalls',
SET_CALLER = 'npwd:setCaller', SET_CALLER = 'npwd:setCaller',
SET_CALL_MODAL = 'npwd:callModal', SET_CALL_MODAL = 'npwd:callModal',
SET_CALL_HISTORY = 'npwd:setCallHistory',
SEND_ALERT = 'npwd:callSetAlert', SEND_ALERT = 'npwd:callSetAlert',
} }