Skip to content

Commit

Permalink
Correctly set call peer
Browse files Browse the repository at this point in the history
  • Loading branch information
lpradovera committed Jul 31, 2019
1 parent 1e37bf1 commit 2b9a3ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/signalwire/relay/calling/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,15 +57,14 @@ 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

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)
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions spec/signalwire/relay/calling/call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion spec/signalwire/relay/calling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions spec/support/mock_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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}",
Expand Down

0 comments on commit 2b9a3ad

Please sign in to comment.