<commit_before>/* @flow */

"use strict";

import _ from 'lodash';
import Db from './db.js';
import Lru from 'lru-cache';
import Twilio from 'twilio';
import Err from './err_class.js';
import Config from '../../config.json';

let log = console.log;

let lru_options = {
		max: 1000,
		length: function(n) { return 1 },  //since we're counting objects, this tells the cache that every entry set count as 1
		maxAge: 1000 * 60 * 60
	};

class CallRouter {
	// Flow annotations.
	
	/*:: client: Object; */
	/*:: activeCalls: Object; */
	/*:: pendingCalls: Object; */
	/*:: failedCalls: Object; */
	/*:: pendingTasks: Object; */
	/*:: activeTasks: Object; */
	/*:: callChannel: Object; */
	
	constructor() {
		this.client = new Twilio.RestClient(Config.twilio.production.account_sid, Config.twilio.production.auth_token);
		this.activeCalls = Lru(lru_options);
		this.pendingCalls = Lru(lru_options);
		this.failedCalls = Lru(lru_options);
		this.pendingTasks = Lru(lru_options);
		this.activeTasks = Lru(lru_options);

		this.callChannel = {};

		Object.observe(this.callChannel, this.processCallChannel.bind(this));
	}

	processCallChannel(changes) {
		let value = changes.shift();
		if (value != undefined) {
			if (value.type === 'add') {
				this.processCalls(value.object[value.name]);
			}
		}
	}

	//queue new calls passing queue_sid, call_side, and params object
	queue(csid/*: string*/, userid/*: string*/, params/*:Object*/) {
		params.id = userid;
		this.pendingCalls.set(csid, params);
		this.callChannel[csid] = params;
	}

	//remove a call from the pedingCall queue
	dequeue(csid/*: string*/, status/*: string*/) {
		let self = this;
		let promises = [];

		if (status === 'hangup') {
			let a_call = this.activeCalls.get(csid);
			let p_call = this.pendingCalls.get(csid);

			Promise.all(
				this.hangupCall(a_call),
				this.hangupCall(p_call)
			)
			.then(function(resp) {
				log('CallRouter:Dequeue - clearing state - ', resp)
				self.activeCalls.del(csid);
				self.pendingCalls.del(csid);
			})
			.catch(function(err) {
				log('CallRouter:Dequeue|hangup - failed to hangup call - ', err);
			});
		} else if (status === 'queue-full') {
			log('CallRouter:Dequeue|Queue-Full');
			this.activeCalls.del(csid);
			this.pendingCalls.del(csid);
		} else if (status === 'system-error' || status === 'error') {
			log('CallRouter: Dequeue|Error');
			if (this.activeCalls.has(csid)) {
				let call = this.activeCalls.get(csid);
				this.hangupCall(call);
				self.activeCalls.del(csid);
			}
			this.pendingCalls.del(csid);
		} else if (status === 'bridged' || status === 'leave' || status === 'redirected') {
			let call = this.pendingCalls.get(csid);
			this.activeCalls.set(csid, call);
			this.pendingCalls.del(csid);
		} else {
			this.cleanUpState(csid);
		}
	}<commit_msg>properly create array of promises for call hangup<commit_after>/* @flow */

"use strict";

import _ from 'lodash';
import Db from './db.js';
import Lru from 'lru-cache';
import Twilio from 'twilio';
import Err from './err_class.js';
import Config from '../../config.json';

let log = console.log;

let lru_options = {
		max: 1000,
		length: function(n) { return 1 },  //since we're counting objects, this tells the cache that every entry set count as 1
		maxAge: 1000 * 60 * 60
	};

class CallRouter {
	// Flow annotations.
	
	/*:: client: Object; */
	/*:: activeCalls: Object; */
	/*:: pendingCalls: Object; */
	/*:: failedCalls: Object; */
	/*:: pendingTasks: Object; */
	/*:: activeTasks: Object; */
	/*:: callChannel: Object; */
	
	constructor() {
		this.client = new Twilio.RestClient(Config.twilio.production.account_sid, Config.twilio.production.auth_token);
		this.activeCalls = Lru(lru_options);
		this.pendingCalls = Lru(lru_options);
		this.failedCalls = Lru(lru_options);
		this.pendingTasks = Lru(lru_options);
		this.activeTasks = Lru(lru_options);

		this.callChannel = {};

		Object.observe(this.callChannel, this.processCallChannel.bind(this));
	}

	processCallChannel(changes) {
		let value = changes.shift();
		if (value != undefined) {
			if (value.type === 'add') {
				this.processCalls(value.object[value.name]);
			}
		}
	}

	//queue new calls passing queue_sid, call_side, and params object
	queue(csid/*: string*/, userid/*: string*/, params/*:Object*/) {
		params.id = userid;
		this.pendingCalls.set(csid, params);
		this.callChannel[csid] = params;
	}

	//remove a call from the pedingCall queue
	dequeue(csid/*: string*/, status/*: string*/) {
		let self = this;
		let promises = [];

		if (status === 'hangup') {
			let a_call = this.activeCalls.get(csid);
			let p_call = this.pendingCalls.get(csid);

			let promises = [];
			promises[0] = this.hangupCall(a_call);
			promises[1] = this.hangupCall(p_call);

			Promise.all(promises)
			.then(function(resp) {
				log('CallRouter:Dequeue - clearing state - ', resp)
				self.activeCalls.del(csid);
				self.pendingCalls.del(csid);
			})
			.catch(function(err) {
				log('CallRouter:Dequeue|hangup - failed to hangup call - ', err);
			});
		} else if (status === 'queue-full') {
			log('CallRouter:Dequeue|Queue-Full');
			this.activeCalls.del(csid);
			this.pendingCalls.del(csid);
		} else if (status === 'system-error' || status === 'error') {
			log('CallRouter: Dequeue|Error');
			if (this.activeCalls.has(csid)) {
				let call = this.activeCalls.get(csid);
				this.hangupCall(call);
				self.activeCalls.del(csid);
			}
			this.pendingCalls.del(csid);
		} else if (status === 'bridged' || status === 'leave' || status === 'redirected') {
			let call = this.pendingCalls.get(csid);
			this.activeCalls.set(csid, call);
			this.pendingCalls.del(csid);
		} else {
			this.cleanUpState(csid);
		}
	}
{'fraction_non_alphanumeric': 0.16983456529391058, 'fraction_numerical': 0.005279831045406547, 'mean_word_length': 3.6735197368421053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968199', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2006, 'n_words': 421}