From 77c7cc303e679ac9b3d6a6dba0db7672978f0793 Mon Sep 17 00:00:00 2001 From: Christopher Date: Sun, 17 Jan 2021 22:03:52 +0100 Subject: [PATCH] feat(calling/client|server): partical functions for calling Should probably reuse data --- phone/src/Phone.tsx | 27 +++++++- .../apps/dialer/components/DialerHistory.tsx | 2 +- phone/src/common/typings/call.ts | 7 ++ phone/src/modal/components/CallModal.tsx | 15 ++--- resources/client/cl_call.ts | 59 ++++++++++------ resources/server/functions.ts | 4 +- resources/server/sv_call.ts | 67 +++++++++++++------ resources/utils/events.ts | 8 ++- 8 files changed, 133 insertions(+), 56 deletions(-) create mode 100644 phone/src/common/typings/call.ts diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 8b9d195..09f76cf 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -28,6 +28,8 @@ import { settingsState } from './apps/settings/hooks/useSettings'; import config from './config/default.json'; import { useRecoilState } from 'recoil'; +import { useCallService } from './modal/hooks/useCallService'; +import { useModal } from './modal/hooks/useModal'; // Inject mock data when in development env. if (process.env.NODE_ENV === 'development') { @@ -52,6 +54,24 @@ if (process.env.NODE_ENV === 'development') { ); }, 1000); + setTimeout(() => { + window.dispatchEvent( + new MessageEvent('message', { + data: { + app: 'CALL', + method: 'setCaller', + data: { + accepted: false, + transmitter: false, + caller: 'Chip', + target: 'Christian', + phone_number: '860-4504', + } + }, + }) + ); + }, 1000); + setTimeout(() => { window.dispatchEvent( new MessageEvent('message', { @@ -258,6 +278,9 @@ function Phone() { useMessagesService(); useNotesService(); usePhotoService(); + useCallService(); + + const { modal } = useModal(); // the calling modal const [settings] = useRecoilState(settingsState); @@ -267,8 +290,6 @@ function Phone() { const currentTheme = () => createMuiTheme(config.themes[settings.theme]); - const calling = false; - document.onkeyup = function (data) { if (data.which === 27) { Nui.send('phone:close'); @@ -303,7 +324,7 @@ function Phone() { ]} />
- {calling ? ( + {modal ? ( ) : ( <> diff --git a/phone/src/apps/dialer/components/DialerHistory.tsx b/phone/src/apps/dialer/components/DialerHistory.tsx index 4c73ab7..5b2f34a 100644 --- a/phone/src/apps/dialer/components/DialerHistory.tsx +++ b/phone/src/apps/dialer/components/DialerHistory.tsx @@ -14,7 +14,7 @@ export const DialerHistory = ({ calls }) => { return ( {calls.map((call) => ( - + {CallTypeIcon[call.type]} diff --git a/phone/src/common/typings/call.ts b/phone/src/common/typings/call.ts new file mode 100644 index 0000000..60a31d4 --- /dev/null +++ b/phone/src/common/typings/call.ts @@ -0,0 +1,7 @@ +export interface CallProps { + accepted: boolean; + transmitter: boolean; + caller: string; + target: string; + phone_number: string; +} \ No newline at end of file diff --git a/phone/src/modal/components/CallModal.tsx b/phone/src/modal/components/CallModal.tsx index 3dd9169..cbffbbc 100644 --- a/phone/src/modal/components/CallModal.tsx +++ b/phone/src/modal/components/CallModal.tsx @@ -5,39 +5,38 @@ import { AppTitle } from '../../ui/components/AppTitle'; import { AppContent } from '../../ui/components/AppContent'; import { useCall } from '../hooks/useCall'; import CallIcon from '@material-ui/icons/Call'; -import PhoneDisabledIcon from '@material-ui/icons/PhoneDisabled'; import CallEndIcon from '@material-ui/icons/CallEnd'; import useStyles from './modal.styles'; import { useModal } from '../hooks/useModal'; +import Nui from '../../os/nui-events/utils/Nui'; export const CallModal = () => { - const { call, setCall } = useCall(); + const { call } = useCall(); const { setModal } = useModal() - const [accepted, setAccepted] = useState(false) const classes = useStyles() const handleAcceptCall = () => { - setAccepted(true) + Nui.send('phone:acceptCall', { + phoneNumber: call.phone_number + }) } const handleRejectCall = () => { setModal(false) - setCall(null) } const handleEndCall = () => { setModal(false) - setCall(null) } return ( -

{call.target}

+

{call.transmitter ? call.target : call.caller}

- {!accepted ? ( + {!call.accepted ? ( <> { - emitNet(events.PHONE_BEGIN_CALL, args[0]); - }, - false -); - -setImmediate(() => { - emit("chat:addSuggestion", "/call", "Call a player", [ - { - name: "Phone Number", - help: "The phone number to call", - }, - ]); -}); - RegisterNuiCallbackType(events.PHONE_BEGIN_CALL); on(`__cfx_nui:${events.PHONE_BEGIN_CALL}`, (data: any) => { + console.log('PHONE NUMBER YOU ARE TRYING TO CALL', data.number); emitNet(events.PHONE_BEGIN_CALL, data.number); }); -onNet(events.PHONE_START_CALL, (target: string, phoneNumber: string) => { +onNet(events.PHONE_START_CALL, (target: string, phoneNumber: string, isTransmitter: boolean, id: any) => { console.log(`You are talking to ${target} who has the number ${phoneNumber}`); - // Temporary for testing - exp["mumble-voip"].SetCallChannel(1); + exp["mumble-voip"].SetCallChannel(id+1); + SendNuiMessage( + JSON.stringify({ + app: "CALL", + method: "callModal", + data: true + }) + ) + SendNuiMessage( + JSON.stringify({ + app: "CALL", + method: "setCaller", + data: { + target: target, + caller: phoneNumber, + transmitter: isTransmitter, + accepted: false + } + }) + ) }); -onNet(events.PHONE_CALL_WAS_ENDED, () => { + +RegisterNuiCallbackType(events.PHONE_ACCEPT_CALL); +on(`__cfx_nui:${events.PHONE_ACCEPT_CALL}`, (data: any) => { + emitNet(events.PHONE_ACCEPT_CALL, data.phoneNumber) +}) + +onNet('phone:callAccepted', (id: number) => { + exp["mumble-voip"].SetRadioChannel(id); +}) + + + + +/* onNet(events.PHONE_CALL_WAS_ENDED, () => { console.log("Call ended"); exp["mumble-voip"].SetCallChannel(0); -}); +}); */ diff --git a/resources/server/functions.ts b/resources/server/functions.ts index 6a7127b..0b0f24d 100644 --- a/resources/server/functions.ts +++ b/resources/server/functions.ts @@ -5,7 +5,7 @@ interface IPhoneNumber { phone_number: string; } -interface Iidentifier { +interface Identifier { identifier: string; } @@ -27,6 +27,6 @@ export async function getIdentifierByPhoneNumber( ): Promise { const query = 'SELECT identifier FROM users WHERE phone_number = ?'; const [results] = await pool.query(query, [phoneNumber]); - const identifier = results; + const identifier = results; return identifier[0].identifier; } diff --git a/resources/server/sv_call.ts b/resources/server/sv_call.ts index 9eb4f65..98ef0f3 100644 --- a/resources/server/sv_call.ts +++ b/resources/server/sv_call.ts @@ -1,12 +1,12 @@ import { ESX } from "./server"; import events from "../utils/events"; -import { getIdentifierByPhoneNumber, getPhoneNumberFromID } from "./functions"; +import { getIdentifierByPhoneNumber, usePhoneNumber } from "./functions"; import { XPlayer } from "esx.js/@types/server"; /** * Returns the player phoneNumber for a passed identifier * @param identifier The players phone number */ -function getPlayerFromIdentifier(identifier: string): Promise { +async function getPlayerFromIdentifier(identifier: string): Promise { return new Promise((res, rej) => { const xPlayers = ESX.GetPlayers(); @@ -25,26 +25,55 @@ function getPlayerFromIdentifier(identifier: string): Promise { } onNet(events.PHONE_BEGIN_CALL, async (phoneNumber: string) => { - const callSource = (global as any).source; - const xPlayer = ESX.GetPlayerFromId(callSource); - - const _identifier = xPlayer.getIdentifier(); - const callerName = xPlayer.getName(); - const callerNumber = getPhoneNumberFromID(_identifier); - try { - const targetPlayer = await getPlayerFromIdentifier(phoneNumber); - const targetName = xPlayer.getName(); + const pSource = (global as any).source; + const xPlayer = ESX.GetPlayerFromId(pSource); + + const _identifier = xPlayer.getIdentifier(); + const callerName = xPlayer.getName(); + const callerNumber = await usePhoneNumber(_identifier); + const targetIdentifier = await getIdentifierByPhoneNumber(phoneNumber) + + const targetPlayer = await getPlayerFromIdentifier(targetIdentifier); + console.log('got the targetPlayer') + const targetName = targetPlayer.getName() + console.log('got the target name: ', targetName) + + // target + // Sends information to the client: sourec, playerName, number, isTransmitter + emitNet(events.PHONE_START_CALL, targetPlayer.source, callerName, phoneNumber, false, pSource); + + // source + // Sends information to the client: sourec, playerName, number, isTransmitter + emitNet(events.PHONE_START_CALL, pSource, targetName, callerNumber, true, targetPlayer.source); - emitNet( - events.PHONE_START_CALL, - targetPlayer.source, - callerName, - phoneNumber - ); - emitNet(events.PHONE_START_CALL, targetName, callerNumber); } catch (e) { console.error(e); - emit(events.PHONE_CALL_ERORR, callSource); + console.log('Failed to call monkaS'); + //emit(events.PHONE_CALL_ERROR, callSource); } }); + +// phoneNumber is the number you're calling +onNet(events.PHONE_ACCEPT_CALL, async (phoneNumber: string) => { + try { + const pSource = (global as any).source + // target + const targetIdentifier = await getIdentifierByPhoneNumber(phoneNumber) + const targetPlayer = await getPlayerFromIdentifier(targetIdentifier); + + + // client that is calling + emitNet('phone:callAccepted', pSource, targetPlayer.source) + + // client that is being called + emitNet('phone:callAccepted', targetPlayer.source, pSource) + + + + + + } catch (error) { + + } +}) \ No newline at end of file diff --git a/resources/utils/events.ts b/resources/utils/events.ts index f5a0370..63c7f05 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -93,5 +93,11 @@ export default { CAMERA_DELETE_PHOTO_SUCCESS: 'phone:deletePhotoSuccess', CONTACTS_ACTION_RESULT: 'phone:contactActionResult', // copy - PHONE_USE_CLIPBOARD: 'phone:useClipboard', + PHONE_USE_CLIPBOARD: "phone:useClipboard", + + // calling + PHONE_BEGIN_CALL: "phone:beginCall", + PHONE_START_CALL: "phone:startCall", + PHONE_ACCEPT_CALL: 'phone:acceptCall', + PHONE_END_CALL: 'phone:endCall' };