chore: Clean up some code andn simplify
This commit is contained in:
@@ -49,34 +49,16 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
if call.status == 'in-progress' || call.status == 'ringing'
|
||||
client.calls(call_sid).update(status: 'completed')
|
||||
|
||||
# Update conversation call status
|
||||
@conversation.additional_attributes['call_status'] = 'completed'
|
||||
@conversation.additional_attributes['call_ended_at'] = Time.now.to_i
|
||||
# Update call status using the unified CallStatusManager
|
||||
# The CallStatusManager will determine if the call is outbound internally
|
||||
status_manager = Voice::CallStatus::Manager.new(
|
||||
conversation: @conversation,
|
||||
call_sid: call_sid,
|
||||
provider: :twilio
|
||||
)
|
||||
status_manager.process_status_update('completed')
|
||||
|
||||
# Calculate call duration if we have a start time
|
||||
if @conversation.additional_attributes['call_started_at']
|
||||
@conversation.additional_attributes['call_duration'] = Time.now.to_i - @conversation.additional_attributes['call_started_at'].to_i
|
||||
end
|
||||
|
||||
# Update the voice call message status
|
||||
if call_message = find_voice_call_message
|
||||
content_attributes = call_message.content_attributes || {}
|
||||
content_attributes['data'] ||= {}
|
||||
content_attributes['data']['status'] = 'completed'
|
||||
content_attributes['data']['status_updated'] = Time.now.to_i
|
||||
content_attributes['data']['meta'] ||= {}
|
||||
content_attributes['data']['meta']['completed_at'] = Time.now.to_i
|
||||
content_attributes['data']['ended_at'] = Time.now.to_i
|
||||
|
||||
# Add duration if available
|
||||
if @conversation.additional_attributes['call_duration']
|
||||
content_attributes['data']['duration'] = @conversation.additional_attributes['call_duration']
|
||||
end
|
||||
|
||||
call_message.update(content_attributes: content_attributes)
|
||||
end
|
||||
|
||||
@conversation.save!
|
||||
# CallStatusManager handles all voice call message updates
|
||||
|
||||
# Create an activity message noting the call has ended
|
||||
Messages::MessageBuilder.new(
|
||||
@@ -92,6 +74,21 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
}
|
||||
}
|
||||
).perform
|
||||
|
||||
# Broadcast call status update on the account channel
|
||||
ActionCable.server.broadcast(
|
||||
"account_#{@conversation.account_id}",
|
||||
{
|
||||
event_name: 'call_status_changed',
|
||||
data: {
|
||||
call_sid: call_sid,
|
||||
status: 'completed',
|
||||
conversation_id: @conversation.id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
timestamp: Time.now.to_i
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
render json: { status: 'success', message: 'Call successfully ended' }
|
||||
else
|
||||
@@ -138,7 +135,7 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
|
||||
# Agent joining call via WebRTC
|
||||
|
||||
# Update conversation to show agent joined and set call status to active
|
||||
# Update conversation to show agent joined
|
||||
@conversation.additional_attributes['agent_joined'] = true
|
||||
@conversation.additional_attributes['joined_at'] = Time.now.to_i
|
||||
@conversation.additional_attributes['joined_by'] = {
|
||||
@@ -146,24 +143,16 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
name: current_user.name
|
||||
}
|
||||
|
||||
# CRITICAL: Update call status to 'in-progress' to ensure UI updates properly
|
||||
# This is especially important for incoming calls where the status might not get updated otherwise
|
||||
@conversation.additional_attributes['call_status'] = 'in-progress'
|
||||
|
||||
# Also record started_at timestamp if not already set
|
||||
@conversation.additional_attributes['call_started_at'] = Time.now.to_i unless @conversation.additional_attributes['call_started_at']
|
||||
|
||||
# Update the call data in the voice call message
|
||||
if call_message = find_voice_call_message
|
||||
content_attributes = call_message.content_attributes || {}
|
||||
content_attributes['data'] ||= {}
|
||||
content_attributes['data']['status'] = 'in-progress'
|
||||
content_attributes['data']['status_updated'] = Time.now.to_i
|
||||
content_attributes['data']['meta'] ||= {}
|
||||
content_attributes['data']['meta']['active_at'] = Time.now.to_i
|
||||
call_message.update(content_attributes: content_attributes)
|
||||
end
|
||||
# Update call status using the unified CallStatusManager
|
||||
# The CallStatusManager will determine if the call is outbound internally
|
||||
status_manager = Voice::CallStatus::Manager.new(
|
||||
conversation: @conversation,
|
||||
call_sid: call_sid,
|
||||
provider: :twilio
|
||||
)
|
||||
status_manager.process_status_update('in-progress')
|
||||
|
||||
# Save the conversation with agent join details
|
||||
@conversation.save!
|
||||
|
||||
# Create an activity message
|
||||
@@ -181,6 +170,21 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
}
|
||||
}
|
||||
).perform
|
||||
|
||||
# Broadcast call status update on the account channel
|
||||
ActionCable.server.broadcast(
|
||||
"account_#{@conversation.account_id}",
|
||||
{
|
||||
event_name: 'call_status_changed',
|
||||
data: {
|
||||
call_sid: call_sid,
|
||||
status: 'in-progress',
|
||||
conversation_id: @conversation.id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
timestamp: Time.now.to_i
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
# Return conference information for the WebRTC client with detailed logging
|
||||
response_data = {
|
||||
@@ -488,28 +492,7 @@ class Api::V1::Accounts::VoiceController < Api::V1::Accounts::BaseController
|
||||
@conversation = Current.account.conversations.find(params[:id] || params[:conversation_id])
|
||||
end
|
||||
|
||||
# Helper method to find the voice call message for the current call
|
||||
# Similar to the one in Voice::MessageUpdateService but simplified
|
||||
def find_voice_call_message
|
||||
return nil unless @conversation.present?
|
||||
|
||||
# Try to find by call_sid first
|
||||
if call_sid = params[:call_sid] || @conversation.additional_attributes&.dig('call_sid')
|
||||
message = @conversation.messages
|
||||
.where(content_type: 'voice_call')
|
||||
.where("content_attributes->'data'->>'call_sid' = ?", call_sid)
|
||||
.first
|
||||
|
||||
# If found, return it
|
||||
return message if message
|
||||
end
|
||||
|
||||
# Fall back to the most recent voice call message
|
||||
@conversation.messages
|
||||
.where(content_type: 'voice_call')
|
||||
.order(created_at: :desc)
|
||||
.first
|
||||
end
|
||||
# Voice call message related functionality is now handled by Voice::CallStatus::Manager
|
||||
|
||||
# Helper method to get base URL with extra resilience
|
||||
def base_url
|
||||
|
||||
Reference in New Issue
Block a user