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
|
||||
|
||||
@@ -72,59 +72,32 @@ class Twilio::VoiceController < ActionController::Base
|
||||
|
||||
def handle_recording
|
||||
call_sid = params['CallSid']
|
||||
from_number = params['From']
|
||||
to_number = params['To']
|
||||
recording_url = params['RecordingUrl']
|
||||
recording_sid = params['RecordingSid']
|
||||
direction = params['Direction']
|
||||
|
||||
# Determine if outbound call
|
||||
is_outbound = direction == 'outbound-api'
|
||||
|
||||
# Find inbox and save recording if available
|
||||
return unless recording_url.present? && call_sid.present?
|
||||
|
||||
inbox_number = is_outbound ? from_number : to_number
|
||||
inbox = find_inbox(inbox_number)
|
||||
|
||||
return unless inbox.present?
|
||||
|
||||
contact_number = is_outbound ? to_number : from_number
|
||||
conversation = find_or_create_conversation(inbox, contact_number, call_sid)
|
||||
|
||||
# Process the recording using RecordingService
|
||||
begin
|
||||
Voice::RecordingService.new(
|
||||
conversation: conversation,
|
||||
recording_url: recording_url,
|
||||
recording_sid: recording_sid,
|
||||
call_sid: call_sid
|
||||
).process
|
||||
|
||||
# End the call after a single recording
|
||||
response = Twilio::TwiML::VoiceResponse.new do |r|
|
||||
r.say(message: 'Thank you for your feedback. Goodbye.')
|
||||
r.hangup
|
||||
end
|
||||
render xml: response.to_s, status: :ok
|
||||
rescue StandardError => e
|
||||
# Log the error but don't crash
|
||||
Rails.logger.error("Error processing recording: #{e.message}")
|
||||
|
||||
# Return a simple TwiML in case of error
|
||||
response = Twilio::TwiML::VoiceResponse.new do |r|
|
||||
r.say(message: 'We encountered an issue processing your feedback. Goodbye.')
|
||||
r.hangup
|
||||
end
|
||||
render xml: response.to_s, status: :ok
|
||||
|
||||
# Log the recording information for future implementation
|
||||
Rails.logger.info("Recording received: CallSid=#{call_sid}, RecordingSid=#{recording_sid}")
|
||||
Rails.logger.info("Recording URL: #{recording_url}")
|
||||
|
||||
# Recording functionality has been removed for now
|
||||
# In the future, we'll implement this to save call recordings and attach them to conversations
|
||||
|
||||
# Return a simple response to end the call
|
||||
response = Twilio::TwiML::VoiceResponse.new do |r|
|
||||
r.say(message: 'Thank you for your feedback. Goodbye.')
|
||||
r.hangup
|
||||
end
|
||||
render xml: response.to_s, status: :ok
|
||||
end
|
||||
|
||||
def transcription_callback
|
||||
# Process the transcription asynchronously
|
||||
# Logging transcription details for future implementation
|
||||
if params['CallSid'].present?
|
||||
# Queue the processing as a background job
|
||||
CallTranscriptionJob.perform_later(params.permit!.to_h)
|
||||
Rails.logger.info("Transcription received for CallSid=#{params['CallSid']}")
|
||||
Rails.logger.info("Transcription text: #{params['TranscriptionText']}")
|
||||
|
||||
# Transcription functionality has been removed for now
|
||||
# In the future, we'll implement this to save call transcriptions and attach them to conversations
|
||||
end
|
||||
|
||||
# Return an empty TwiML response to satisfy Twilio
|
||||
@@ -165,14 +138,13 @@ class Twilio::VoiceController < ActionController::Base
|
||||
inbox: inbox
|
||||
).perform
|
||||
|
||||
# Use TwilioCallStatusService to handle status update
|
||||
Voice::TwilioCallStatusService.new(
|
||||
# Use the unified CallStatusManager to handle status update
|
||||
# The CallStatusManager will determine if the call is outbound internally
|
||||
Voice::CallStatus::Manager.new(
|
||||
conversation: conversation,
|
||||
call_sid: call_sid,
|
||||
call_status: call_status,
|
||||
is_outbound: is_outbound,
|
||||
duration: duration
|
||||
).process(params['IsFirstResponseForStatus'] == 'true')
|
||||
provider: :twilio
|
||||
).process_status_update(call_status, duration, params['IsFirstResponseForStatus'] == 'true')
|
||||
|
||||
# Return an empty response
|
||||
head :ok
|
||||
@@ -214,14 +186,13 @@ class Twilio::VoiceController < ActionController::Base
|
||||
inbox: inbox
|
||||
).perform
|
||||
|
||||
# Add call activity message
|
||||
Voice::TwilioCallStatusService.new(
|
||||
# Add call activity message using unified CallStatusManager
|
||||
# The CallStatusManager will determine if the call is outbound internally
|
||||
Voice::CallStatus::Manager.new(
|
||||
conversation: conversation,
|
||||
call_sid: call_sid,
|
||||
call_status: 'in-progress',
|
||||
is_outbound: is_outbound,
|
||||
duration: nil
|
||||
).process(true)
|
||||
provider: :twilio
|
||||
).process_status_update('in-progress', nil, true)
|
||||
|
||||
# IMPORTANT: Use the provided conference_name if available, otherwise use the one from conversation
|
||||
if conference_name_param.present?
|
||||
|
||||
Reference in New Issue
Block a user