From e5772d6b855ad418d6d3b1acf168704828c74959 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Mon, 30 Oct 2023 13:59:54 -0400 Subject: [PATCH] =?UTF-8?q?allow=20dial=20referHook=20to=20return=20applic?= =?UTF-8?q?ation=20to=20execute=20on=20the=20other=20le=E2=80=A6=20(#505)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * allow dial referHook to return application to execute on the other leg; fixes #504 * fix session tracking * minor logging * minor --- lib/tasks/dial.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/tasks/dial.js b/lib/tasks/dial.js index fd702431..14d33b96 100644 --- a/lib/tasks/dial.js +++ b/lib/tasks/dial.js @@ -322,7 +322,7 @@ class TaskDial extends Task { const to = parseUri(req.getParsedHeader('Refer-To').uri); const by = parseUri(req.getParsedHeader('Referred-By').uri); this.logger.info({to}, 'refer to parsed'); - await cs.requestor.request('verb:hook', this.referHook, { + const json = await cs.requestor.request('verb:hook', this.referHook, { ...callInfo, refer_details: { sip_refer_to: req.get('Refer-To'), @@ -334,6 +334,33 @@ class TaskDial extends Task { referred_call_sid } }, httpHeaders); + if (json && Array.isArray(json)) { + try { + let logger = this.logger; + if (isChild) { + const {parentLogger} = this.srf.locals; + logger = parentLogger.child({callId: cs.callId, callSid: this.sd.callSid}); + } + const tasks = normalizeJambones(logger, json).map((tdata) => makeTask(this.logger, tdata)); + if (tasks && tasks.length > 0) { + const legs = isChild ? ['child', 'parent'] : ['parent', 'child']; + this.logger.info(`Dial:handleRefer received REFER on ${legs[0]} leg, setting new app on ${legs[1]} leg`); + if (isChild) this.redirect(cs, tasks); + else { + this.logger.info({tasks: json}, 'Dial:handleRefer - new application for for child leg'); + const adultingSession = await this.sd.doAdulting({ + logger, + application: cs.application, + tasks + }); + /* need to update the callSid of the child with its own (new) AdultingCallSession */ + sessionTracker.add(adultingSession.callSid, adultingSession); + } + } + } catch (err) { + this.logger.info(err, 'Dial:handleRefer - error setting new application after receiving REFER'); + } + } res.send(202); this.logger.info('DialTask:handleRefer - sent 202 Accepted'); } catch (err) {