From 3171b138f9abcb44c132633374189539887c8bf9 Mon Sep 17 00:00:00 2001 From: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:54:59 +0700 Subject: [PATCH] Feat/support device call (#495) * support dequeue from registered user * support dequeue from registered user * wip * wip * wip * wip * fix review comments --- lib/middleware.js | 114 +++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 36 deletions(-) diff --git a/lib/middleware.js b/lib/middleware.js index 9006c406..aa444396 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -29,8 +29,9 @@ module.exports = function(srf, logger) { } = srf.locals; const {lookupAccountDetails} = dbUtils(logger, srf); - function initLocals(req, res, next) { + async function initLocals(req, res, next) { const callId = req.get('Call-ID'); + const uri = parseUri(req.uri); logger.info({ callId, callingNumber: req.callingNumber, @@ -43,12 +44,39 @@ module.exports = function(srf, logger) { const callSid = req.has('X-Retain-Call-Sid') ? req.get('X-Retain-Call-Sid') : uuidv4(); const account_sid = req.get('X-Account-Sid'); req.locals = {callSid, account_sid, callId}; - if (req.has('X-Application-Sid')) { + + if (req.has('X-Authenticated-User')) req.locals.originatingUser = req.get('X-Authenticated-User'); + + // check for call to application + if (uri.user.startsWith('app-') && req.locals.originatingUser) { + const application_sid = uri.user.match(/app-(.*)/)[1]; + logger.debug(`got application from Request URI header: ${application_sid}`); + req.locals.application_sid = application_sid; + } else if (req.has('X-Application-Sid')) { const application_sid = req.get('X-Application-Sid'); logger.debug(`got application from X-Application-Sid header: ${application_sid}`); req.locals.application_sid = application_sid; } - if (req.has('X-Authenticated-User')) req.locals.originatingUser = req.get('X-Authenticated-User'); + // check for call to queue + if (uri.user.startsWith('queue-') && req.locals.originatingUser) { + const queue_name = uri.user.match(/queue-(.*)/)[1]; + logger.debug(`got Queue from Request URI header: ${queue_name}`); + req.locals.queue_name = queue_name; + } + // check for call to registered user + if (req.locals.originatingUser) { + const arr = /^(.*)@(.*)/.exec(req.locals.originatingUser); + if (arr) { + const sipRealm = arr[2]; + const called_user = `${req.calledNumber}@${sipRealm}`; + const reg = await registrar.query(called_user); + if (reg) { + logger.debug(`got called Number is a registered user: ${called_user}`); + req.locals.called_user = called_user; + } + } + } + if (req.has('X-MS-Teams-Tenant-FQDN')) req.locals.msTeamsTenant = req.get('X-MS-Teams-Tenant-FQDN'); if (req.has('X-Cisco-Recording-Participant')) { const ciscoParticipants = req.get('X-Cisco-Recording-Participant'); @@ -185,42 +213,56 @@ module.exports = function(srf, logger) { const {span} = rootSpan.startChildSpan('lookupApplication'); try { let app; - if (req.locals.application_sid) app = await lookupAppBySid(req.locals.application_sid); - else if (req.locals.originatingUser) { + if (req.locals.queue_name) { + logger.debug(`calling to queue ${req.locals.queue_name}, generating queue app`); + app = { + // Dummy hook to follow later feature server logic. + call_hook: { + url: 'https://jambonz.org', + method: 'GET' + }, + account_sid, + app_json: JSON.stringify( + [{ + verb: 'dequeue', + name: req.locals.queue_name, + timeout: 5 + }] + ) + }; + } else if (req.locals.called_user) { + logger.debug(`calling to registered user ${req.locals.called_user}, generating dial app`); + app = { + // Dummy hook to follow later feature server logic. + call_hook: { + url: 'https://jambonz.org', + method: 'GET' + }, + account_sid, + app_json: JSON.stringify( + [{ + verb: 'dial', + callerId: req.locals.callingNumber, + answerOnBridge: true, + target: [ + { + type: 'user', + name: req.locals.called_user + } + ] + }] + ) + }; + } else if (req.locals.application_sid) { + app = await lookupAppBySid(req.locals.application_sid); + } else if (req.locals.originatingUser) { const arr = /^(.*)@(.*)/.exec(req.locals.originatingUser); if (arr) { const sipRealm = arr[2]; - const calledAor = `${req.calledNumber}@${sipRealm}`; - const reg = await registrar.query(calledAor); - if (reg) { - logger.debug(`called client ${calledAor} is registered, forwarding call to called client.`); - app = { - // Dummy hook to follow later feature server logic. - call_hook: { - url: 'https://jambonz.org', - method: 'GET' - }, - account_sid, - app_json: JSON.stringify( - [{ - verb: 'dial', - callerId: arr[1], - answerOnBridge: true, - target: [ - { - type: 'user', - name: calledAor - } - ] - }] - ) - }; - } else { - logger.debug(`looking for device calling app for realm ${sipRealm}`); - app = await lookupAppByRealm(sipRealm); - if (app) { - logger.debug({app}, `retrieved device calling app for realm ${sipRealm}`); - } + logger.debug(`looking for device calling app for realm ${sipRealm}`); + app = await lookupAppByRealm(sipRealm); + if (app) { + logger.debug({app}, `retrieved device calling app for realm ${sipRealm}`); } } }