From f6bbe0fd24ef01d27d2bfe87a302a5ac61bea18a Mon Sep 17 00:00:00 2001 From: balzack Date: Wed, 29 Jan 2025 13:36:16 -0800 Subject: [PATCH] supporting ignore and decline calls --- app/client/mobile/src/calling/Calling.tsx | 52 ++++++++++++++++--- .../mobile/src/calling/useCalling.hook.ts | 32 ++++++++---- app/sdk/src/link.ts | 4 +- app/sdk/src/ring.ts | 4 +- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/app/client/mobile/src/calling/Calling.tsx b/app/client/mobile/src/calling/Calling.tsx index f472c081..c979e96c 100644 --- a/app/client/mobile/src/calling/Calling.tsx +++ b/app/client/mobile/src/calling/Calling.tsx @@ -11,6 +11,7 @@ import LinearGradient from 'react-native-linear-gradient'; import { Colors } from '../constants/Colors'; import { RTCView } from 'react-native-webrtc'; import { Card } from '../card/Card'; +import { activateKeepAwake, deactivateKeepAwake} from "@sayem314/react-native-keep-awake"; export function Calling({ callCard }: { callCard: string }) { const { state, actions } = useCalling(); @@ -20,6 +21,9 @@ export function Calling({ callCard }: { callCard: string }) { const {height, width} = useWindowDimensions(); const [applyingVideo, setApplyingVideo] = useState(false); const [applyingAudio, setApplyingAudio] = useState(false); + const [accepting, setAccepting] = useState(null as null|string); + const [ignoring, setIgnoring] = useState(null as null|string); + const [declining, setDeclining] = useState(null as null|string); const toggleVideo = async () => { if (!applyingVideo) { @@ -82,15 +86,41 @@ export function Calling({ callCard }: { callCard: string }) { } const accept = async (callId, card) => { - if (!connecting) { - setConnecting(true); + if (!accepting) { + setAccepting(callId); try { await actions.accept(callId, card); } catch (err) { console.log(err); setAlert(true); } - setConnecting(false); + setAccepting(null); + } + } + + const ignore = async (callId, card) => { + if (!ignoring) { + setIgnoring(callId); + try { + await actions.ignore(callId, card); + } catch (err) { + console.log(err); + setAlert(true); + } + setIgnoring(null); + } + } + + const decline = async (callId, card) => { + if (!declining) { + setDeclining(callId); + try { + await actions.decline(callId, card); + } catch (err) { + console.log(err); + setAlert(true); + } + setDeclining(null); } } @@ -112,15 +142,23 @@ export function Calling({ callCard }: { callCard: string }) { } }, [callCard]); + useEffect(() => { + if (state.calling) { + activateKeepAwake(); + } else { + deactivateKeepAwake(); + } + }, [state.calling]); + const calls = state.calls.map((contact, index) => { const { callId, card } = contact; const { name, handle, node, imageUrl } = card; - const ignore = {}} /> - const decline = {}} /> - const accept = actions.accept(callId, card)} /> + const ignoreButton = ignore(callId, card)} /> + const declineButton = decline(callId, card)} /> + const acceptButton = accept(callId, card)} /> return ( - + ) }); diff --git a/app/client/mobile/src/calling/useCalling.hook.ts b/app/client/mobile/src/calling/useCalling.hook.ts index 06e42f24..f66c1bd7 100644 --- a/app/client/mobile/src/calling/useCalling.hook.ts +++ b/app/client/mobile/src/calling/useCalling.hook.ts @@ -96,16 +96,7 @@ export function useCalling() { updateState({ failed: true }); } } else if (status === 'closed') { - call.current = null; - try { - peer.close(); - link.close(); - } catch (err) { - console.log(err); - } - localStream.current = null; - remoteStream.current = null, - updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null }); + updatePeer('close'); } } } @@ -196,6 +187,19 @@ export function useCalling() { } } else if (type === 'local_track') { await peerTrack(data); + } else if (type === 'close' && call.current) { + peerUpdate.current = []; + const { peer, link } = call.current; + call.current = null; + try { + peer.close(); + link.close(); + } catch (err) { + console.log(err); + } + localStream.current = null; + remoteStream.current = null, + updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null }); } } updatingPeer.current = false; @@ -248,6 +252,14 @@ export function useCalling() { }, [app.state.session]); const actions = { + ignore: async (callId: string, card: Card) => { + const ring = app.state.session.getRing(); + await ring.ignore(card.cardId, callId); + }, + decline: async (callId: string, card: Card) => { + const ring = app.state.session.getRing(); + await ring.decline(card.cardId, callId); + }, end: async () => { if (!call.current) { throw new Error('no active call'); diff --git a/app/sdk/src/link.ts b/app/sdk/src/link.ts index f1c38027..aa29add2 100644 --- a/app/sdk/src/link.ts +++ b/app/sdk/src/link.ts @@ -83,7 +83,7 @@ export class LinkModule implements Link { }, RING_INTERVAL); this.ice = ice; - this.connect(callerToken, node, secure); + this.websocket = this.setWebSocket(callerToken, node, secure); } public async join(server: string, secure: boolean, token: string, ice: { urls: string; username: string; credential: string }[], endCall: ()=>Promise) { @@ -96,7 +96,7 @@ export class LinkModule implements Link { this.log.error(err); } } - this.websocket = this.setWebSocket(token, node, secure); + this.websocket = this.setWebSocket(token, server, secure); } public async close() { diff --git a/app/sdk/src/ring.ts b/app/sdk/src/ring.ts index 6436f2f0..5eb1be5e 100644 --- a/app/sdk/src/ring.ts +++ b/app/sdk/src/ring.ts @@ -71,7 +71,8 @@ export class RingModule implements Ring { return link; } - public async ignore(cardId: stirng, callId: string): Promise { + public async ignore(cardId: string, callId: string): Promise { + const now = (new Date()).getTime(); const id = `${cardId}:${callId}`; const entry = this.calls.get(id); if (!entry || entry.expires < now || entry.status !== 'ringing') { @@ -82,6 +83,7 @@ export class RingModule implements Ring { } public async decline(cardId: string, callId: string): Promise { + const now = (new Date()).getTime(); const id = `${cardId}:${callId}`; const entry = this.calls.get(id); if (!entry || entry.expires < now || entry.status !== 'ringing') {