diff --git a/lib/signalwire/relay/calling/call.rb b/lib/signalwire/relay/calling/call.rb index c9bd56f..ce72079 100644 --- a/lib/signalwire/relay/calling/call.rb +++ b/lib/signalwire/relay/calling/call.rb @@ -46,6 +46,7 @@ def setup_call_event_handlers change_call_state(event.event_params) end + update_call_fields(event.call_params) broadcast :event, event broadcast :state_change, event end @@ -56,8 +57,6 @@ def change_call_state(event_params) @previous_state = @state @state = call_state[:call_state] broadcast :call_state_change, previous_state: @previous_state, state: @state - - update_call_fields(call_state) broadcast @state.to_sym, previous_state: @previous_state, state: @state finish_call(event_params) if @state == Relay::CallState::ENDED end @@ -65,6 +64,7 @@ def change_call_state(event_params) def update_call_fields(call_state) @id = call_state[:call_id] if call_state[:call_id] @node_id = call_state[:node_id] if call_state[:node_id] + @peer = call_state[:peer] if call_state[:peer] end def change_connect_state(new_connect_state) @@ -95,6 +95,10 @@ def active? !ended? end + def peer + @client.calling.find_call_by_id(@peer[:call_id]) if @peer && @peer[:call_id] + end + def answer answer_component = Signalwire::Relay::Calling::Answer.new(call: self) answer_component.wait_for(Relay::CallState::ANSWERED, Relay::CallState::ENDING, Relay::CallState::ENDED) diff --git a/spec/signalwire/relay/calling/call_spec.rb b/spec/signalwire/relay/calling/call_spec.rb index 280acd8..796879a 100644 --- a/spec/signalwire/relay/calling/call_spec.rb +++ b/spec/signalwire/relay/calling/call_spec.rb @@ -16,6 +16,23 @@ end end + describe 'connect_state_change' do + let(:peered_call){ described_class.new(client, mock_call_hash('created', 'some-call-id').dig(:params, :params, :params)) } + + before do + client.calling.calls << peered_call + end + + fit 'sets the peer and fires the event' do + subject.on :connect_state_change do |event| + expect(event).to eq({ previous_state: nil, state: "connected" }) + end + + mock_message subject.client.session, mock_connect_state(subject.id, peered_call.id) + expect(subject.peer).to be peered_call + end + end + describe 'ending a call' do it 'sets the busy state' do message = mock_call_state(subject.id, Relay::CallState::ENDED) diff --git a/spec/signalwire/relay/calling_spec.rb b/spec/signalwire/relay/calling_spec.rb index e4cdd1c..2e31fb5 100644 --- a/spec/signalwire/relay/calling_spec.rb +++ b/spec/signalwire/relay/calling_spec.rb @@ -24,7 +24,6 @@ sleep 0.1 mock_message subject.session, relay_response(receive_command['id']) - # expect(subject.calling.contexts).to eq ['pbx'] call = nil mock_message subject.session, mock_call_hash diff --git a/spec/support/mock_helpers.rb b/spec/support/mock_helpers.rb index 2440a43..2697926 100644 --- a/spec/support/mock_helpers.rb +++ b/spec/support/mock_helpers.rb @@ -17,7 +17,7 @@ def relay_response(id, code = '200', message = 'Some message goes here') } end - def mock_call_hash(state = 'created') + def mock_call_hash(state = 'created', call_id='fb102ad7-4479-42a9-88ac-999999999999') mock_relay_message({ "event_type": 'calling.call.receive', "timestamp": 1558539404.4556861, @@ -34,7 +34,7 @@ def mock_call_hash(state = 'created') } }, "direction": 'inbound', - "call_id": 'fb102ad7-4479-42a9-88ac-999999999999', + "call_id": call_id, "node_id": '2f25c10f-68cd-4b0c-8259-999999999999' }, "event_channel": 'signalwire_calling_abc123' @@ -64,6 +64,32 @@ def mock_call_state(call_id, state=Relay::CallState::ANSWERED) }) end + def mock_connect_state(call_id, peer_id='some-call-id', state=Relay::CallConnectState::CONNECTED) + mock_relay_message({ + "event_type": "calling.call.connect", + "event_channel": "signalwire_calling-999999999999", + "timestamp": 1558539404.79576, + "project_id": 'fmyspace345', + "space_id": 'fmyspace345', + "params": { + "connect_state": state, + "device": { + "type": "phone", + "params": { + "from_number": "+12029085665", + "to_number": "12069286532" + } + }, + "call_id": call_id, + "node_id": "2f25c10f-68cd-4b0c-8259-999999999999", + "peer": { + "call_id": peer_id, + "node_id": "2f25c10f-68cd-4b0c-8259-999999999999" + } + } + }) + end + def mock_message_event(event='receive') mock_relay_message({ "event_type": "messaging.#{event}",