Skip to content

Commit

Permalink
support list all conferences
Browse files Browse the repository at this point in the history
  • Loading branch information
xquanluu committed May 7, 2024
1 parent 9fbd2c3 commit e6b749f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {noopLogger} = require('./lib/utils');
const {noopLogger, makePatternForConferenceScan} = require('./lib/utils');
const Redis = require('ioredis');
const fs = require('fs');

Expand Down Expand Up @@ -68,6 +68,7 @@ module.exports = (opts, logger) => {
deleteCall: require('./lib/delete-call').bind(null, client, logger),
listCalls: require('./lib/list-calls').bind(null, client, logger),
listQueues: require('./lib/list-queues').bind(null, client, logger),
listConferences: require('./lib/hash/list-hash').bind(null, client, logger, makePatternForConferenceScan, 'conf'),
purgeCalls: require('./lib/purge-calls').bind(null, client, logger),
createSet: require('./lib/set/create-set').bind(null, client, logger),
addToSet: require('./lib/set/add-to-set').bind(null, client, logger),
Expand Down
37 changes: 37 additions & 0 deletions lib/hash/list-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { noopLogger} = require('../utils');
const debug = require('debug')('jambonz:realtimedb-helpers');

const scan = async(client, logger, cursor, pattern) => {
try {
const res = await client.scan(cursor, 'MATCH', pattern);
debug(`scanned pattern ${pattern} cursor ${cursor} result: ${JSON.stringify(res)}`);
return {next: res[0], matches: res[1]}; // return updated cursor
} catch (err) {
logger.error(err, `Error scanning ${pattern}`);
return {next: '0'};
}
};

async function listHashes(client, logger, createPaternForScan, hashPrefix, accountSid, glob) {
logger = logger || noopLogger;
let cursor = '0';
const pattern = createPaternForScan(accountSid, glob);
const hashes = [];
try {
debug(`listHashes: scanning with match ${pattern}`);
do {
const {next, matches} = await scan(client, logger, cursor, pattern);
if (matches.length > 0) hashes.push.apply(hashes, matches);
cursor = next;
} while ('0' !== cursor);

logger.debug(`listHashes retrieved ${hashes.length} ${hashPrefix}`);

return hashes;
} catch (err) {
debug(err, `listHashes: Error retrieving ${hashPrefix} for account sid ${accountSid}`);
logger.error(err, `listHashes: Error retrieving ${hashPrefix} for account sid ${accountSid}`);
}
}

module.exports = listHashes;
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ function makeCallKey(accountSid, callSid) {
function makePatternForCallScan(accountSid) {
return `call:${accountSid}:*`;
}

function makePatternForQueueScan(accountSid, pattern) {
return `queue:${accountSid}:${pattern || '*'}`;
}

function makePatternForConferenceScan(accountSid, pattern) {
return `conf:${accountSid}:${pattern || '*'}`;
}

const toBase64 = (str) => Buffer.from(str || '', 'utf8').toString('base64');

const noopLogger = {
Expand All @@ -47,12 +52,12 @@ function filterNullsAndObjects(obj) {
debug(filtered, 'filtering done');
return filtered;
}

module.exports = {
makeCallKey,
makeBasicAuthHeader,
makePatternForCallScan,
makePatternForQueueScan,
makePatternForConferenceScan,
noopLogger,
filterNullsAndObjects,
CALL_SET: 'active-call-sids',
Expand Down
39 changes: 39 additions & 0 deletions test/conference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const test = require('tape').test ;
const config = require('config');
const opts = config.get('redis');


process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});

test('Conference tests', async(t) => {
const fn = require('..');
const {listConferences, createHash, client} = fn(opts);

//wait 1 sec for connection
//await sleep(1);

try {

await createHash('conf:account-sid:conf1', 'url1');
await createHash('conf:account-sid:conf2', 'url2');
await createHash('conf:account-sid:conf3', 'url3');
await createHash('conf:account-sid:conf4', 'url4');
await createHash('conf:account-sid:ss-conf5', 'a value');
await createHash('conf:account-sid:ss-conf6', 'another value');

let confs = await listConferences('account-sid');
t.ok(confs.length === 6, 'retrieved 6 total queues');

confs = await listConferences('account-sid', 'ss-*');
t.ok(confs.length === 2, 'retrieved 2 total queues by pattern');

t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('./set');
require('./hash');
require('./key');
require('./queues');
require('./conference');
require('./auth');
require('./sorted-set');
require('./sorted-set-queues');
Expand Down

0 comments on commit e6b749f

Please sign in to comment.