From cd06333ccbfa12daf77e9359a639203e410f8819 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 28 Mar 2023 16:35:57 -0700 Subject: [PATCH] adding endpoints used for webrtc coordination --- app/mobile/src/api/addCall.js | 8 +++++ app/mobile/src/api/addContactRing.js | 7 +++++ app/mobile/src/api/keepCall.js | 7 +++++ app/mobile/src/api/removeCall.js | 7 +++++ app/mobile/src/api/removeContactCall.js | 6 ++++ app/mobile/src/context/useRingContext.hook.js | 29 ++++++++++++++++++- 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/mobile/src/api/addCall.js create mode 100644 app/mobile/src/api/addContactRing.js create mode 100644 app/mobile/src/api/keepCall.js create mode 100644 app/mobile/src/api/removeCall.js create mode 100644 app/mobile/src/api/removeContactCall.js diff --git a/app/mobile/src/api/addCall.js b/app/mobile/src/api/addCall.js new file mode 100644 index 00000000..caa11954 --- /dev/null +++ b/app/mobile/src/api/addCall.js @@ -0,0 +1,8 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function addCall(server, token, cardId) { + let call = await fetchWithTimeout(`https://${server}/talk/calls?agent=${token}`, { method: 'POST', body: JSON.stringify(cardId)} ); + checkResponse(call); + return await call.json(); +} + diff --git a/app/mobile/src/api/addContactRing.js b/app/mobile/src/api/addContactRing.js new file mode 100644 index 00000000..6754ea00 --- /dev/null +++ b/app/mobile/src/api/addContactRing.js @@ -0,0 +1,7 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function addContactRing(server, token, call) { + let ring = await fetchWithTimeout(`https://${server}/talk/rings?contact=${token}`, { method: 'POST', body: JSON.stringify(call) }); + checkResponse(ring); +} + diff --git a/app/mobile/src/api/keepCall.js b/app/mobile/src/api/keepCall.js new file mode 100644 index 00000000..1219de40 --- /dev/null +++ b/app/mobile/src/api/keepCall.js @@ -0,0 +1,7 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function keepCall(server, token, callId) { + let call = await fetchWithTimeout(`https://${server}/talk/calls/${callId}?agent=${token}`, { method: 'PUT' }); + checkResponse(call); +} + diff --git a/app/mobile/src/api/removeCall.js b/app/mobile/src/api/removeCall.js new file mode 100644 index 00000000..602d1d59 --- /dev/null +++ b/app/mobile/src/api/removeCall.js @@ -0,0 +1,7 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function removeCall(server, token, callId) { + let call = await fetchWithTimeout(`https://${server}/talk/calls/${callId}?agent=${token}` + param, { method: 'DELETE' }); + checkResponse(call) +} + diff --git a/app/mobile/src/api/removeContactCall.js b/app/mobile/src/api/removeContactCall.js new file mode 100644 index 00000000..2d2dd315 --- /dev/null +++ b/app/mobile/src/api/removeContactCall.js @@ -0,0 +1,6 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function removeContactCall(server, token, calllId) { + const call = await fetchWithTimeout(`https://${server}/talk/calls/${callId}?contact=${token}`, { method: 'DELETE' }); + checkResponse(call); +} diff --git a/app/mobile/src/context/useRingContext.hook.js b/app/mobile/src/context/useRingContext.hook.js index a7b86ed0..2bbf3e57 100644 --- a/app/mobile/src/context/useRingContext.hook.js +++ b/app/mobile/src/context/useRingContext.hook.js @@ -62,11 +62,38 @@ export function useRingContext() { access.current = null; }, ring: (cardId, callId, calleeToken) => { -console.log("RING"); + const key = `${cardId}:${callId}` + const call = ringing.current.get(key) || { cardId, calleeToken, callId } + call.expires = Date.now() + EXPIRE; + ringing.current.set(key, call); + updateState({ ringing: ringing.current }); + setTimeout(() => { + updateState({ ringing: ringing.current }); + }, EXPIRE); }, ignore: (cardId, callId) => { + const key = `${cardId}:${callId}` + const call = ringing.current.get(key); + if (call) { + call.status = 'ignored' + ringing.current.set(key, call); + updateState({ ringing: ringing.current }); + } }, decline: async (cardId, contactNode, contactToken, callId) => { + const key = `${cardId}:${callId}` + const call = ringing.current.get(key); + if (call) { + call.status = 'declined' + ringing.current.set(key, call); + updateState({ ringing: ringing.current }); + try { + await removeContactCall(contactNode, contactToken, callId); + } + catch (err) { + console.log(err); + } + } }, accept: async (cardId, callId, contactNode, contactToken, calleeToken) => { },