~singpolyma/sgx-jmp

b754e6f5c0fa8e41270afb3530ee15bf4b934bd7 — Stephen Paul Weber 2 months ago 1c92ee3
Switch to the new Ring verb with answerCall=false

Should actually cause incoming calls to ring properly, and the bug that required
pseudo_call_id seems gone.
3 files changed, 19 insertions(+), 41 deletions(-)

R views/{pause.slim => ring.slim}
M views/voicemail.slim
M web.rb
R views/pause.slim => views/ring.slim +1 -1
@@ 1,3 1,3 @@
doctype xml
Response
	Pause duration=duration
	Ring duration=duration answerCall="false"

M views/voicemail.slim => views/voicemail.slim +2 -2
@@ 5,6 5,6 @@ Response
	PlayAudio= "/beep.mp3"
	Record{
		transcribe=transcription_enabled.to_s
		recordingAvailableUrl="/inbound/calls/#{pseudo_call_id}/voicemail/audio"
		transcriptionAvailableUrl="/inbound/calls/#{pseudo_call_id}/voicemail/transcription"
		recordingAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/audio"
		transcriptionAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/transcription"
		fileFormat="mp3"} /

M web.rb => web.rb +16 -38
@@ 55,11 55,10 @@ class Web < Roda
	plugin RodaEMPromise # Must go last!

	class << self
		attr_reader :customer_repo, :log, :true_inbound_call, :outbound_transfers
		attr_reader :customer_repo, :log, :outbound_transfers

		def run(log, *listen_on)
			plugin :common_logger, log, method: :info
			@true_inbound_call = {}
			@outbound_transfers = {}
			Thin::Logging.logger = log
			Thin::Server.start(


@@ 71,7 70,7 @@ class Web < Roda
	end

	extend Forwardable
	def_delegators :'self.class', :true_inbound_call, :outbound_transfers
	def_delegators :'self.class', :outbound_transfers
	def_delegators :request, :params

	def log


@@ 98,11 97,6 @@ class Web < Roda
		end
	end

	def pseudo_call_id
		request.captures_hash[:pseudo_call_id] ||
			Digest::SHA256.hexdigest("#{params['from']},#{params['to']}")
	end

	TEL_CANDIDATES = {
		"Restricted" => "14",
		"anonymous" => "15",


@@ 130,7 124,7 @@ class Web < Roda
	end

	def inbound_calls_path(suffix)
		["/inbound/calls/#{pseudo_call_id}", suffix].compact.join("/")
		["/inbound/calls/#{params['callId']}", suffix].compact.join("/")
	end

	def url(path)


@@ 152,38 146,29 @@ class Web < Roda
			r.on "calls" do
				r.post "status" do
					if params["eventType"] == "disconnect"
						p_call_id = pseudo_call_id
						call_id = params["callId"]
						EM.promise_timer(2).then {
							next unless true_inbound_call[p_call_id] == call_id

							true_inbound_call.delete(p_call_id)

							if (outbound_leg = outbound_transfers.delete(p_call_id))
								modify_call(outbound_leg) do |call|
									call.state = "completed"
								end
						if (outbound_leg = outbound_transfers.delete(params["callId"]))
							modify_call(outbound_leg) do |call|
								call.state = "completed"
							end
						end

							CustomerRepo.new.find_by_tel(params["to"]).then do |customer|
								CDR.for_inbound(customer.customer_id, params).save
							end
						}.catch(&method(:log_error))
						CustomerRepo.new.find_by_tel(params["to"]).then do |customer|
							CDR.for_inbound(customer.customer_id, params).save
						end
					end
					"OK"
				end

				r.on :pseudo_call_id do |pseudo_call_id|
				r.on :call_id do |call_id|
					r.post "transfer_complete" do
						outbound_leg = outbound_transfers.delete(pseudo_call_id)
						outbound_leg = outbound_transfers.delete(call_id)
						if params["cause"] == "hangup"
							log.debug "Normal hangup", loggable_params
						elsif !outbound_leg
							log.debug "Inbound disconnected", loggable_params
						else
							log.debug "Go to voicemail", loggable_params
							true_call_id = true_inbound_call[pseudo_call_id]
							modify_call(true_call_id) do |call|
							modify_call(call_id) do |call|
								call.redirect_url = url inbound_calls_path(:voicemail)
							end
						end


@@ 245,22 230,15 @@ class Web < Roda
					end

					r.post do
						true_call_id = true_inbound_call[pseudo_call_id]
						render :bridge, locals: { call_id: true_call_id }
						render :bridge, locals: { call_id: call_id }
					end
				end

				r.post do
					if true_inbound_call[pseudo_call_id]
						true_inbound_call[pseudo_call_id] = params["callId"]
						return render :pause, locals: { duration: 300 }
					end

					CustomerRepo.new(
						sgx_repo: Bwmsgsv2Repo.new
					).find_by_tel(params["to"]).then(&:fwd).then do |fwd|
						call = fwd.create_call(CONFIG[:creds][:account]) { |cc|
							true_inbound_call[pseudo_call_id] = params["callId"]
							cc.from = params["from"]
							cc.application_id = params["applicationId"]
							cc.answer_url = url inbound_calls_path(nil)


@@ 268,8 246,8 @@ class Web < Roda
						}

						if call
							outbound_transfers[pseudo_call_id] = call
							render :pause, locals: { duration: 300 }
							outbound_transfers[params["callId"]] = call
							render :ring, locals: { duration: 300 }
						else
							render :redirect, locals: { to: inbound_calls_path(:voicemail) }
						end