diff --git a/app/controllers/integration/sipgate_controller.rb b/app/controllers/integration/sipgate_controller.rb index c45335cee..e7c511969 100644 --- a/app/controllers/integration/sipgate_controller.rb +++ b/app/controllers/integration/sipgate_controller.rb @@ -4,14 +4,11 @@ require 'builder' class Integration::SipgateController < ApplicationController + before_filter :check_configured + # notify about inbound call / block inbound call def in - http_log_config facility: 'sipgate.io' - return if !configured? - if params['event'] == 'newCall' - - config = Setting.get('sipgate_config') config_inbound = config[:inbound] || {} block_caller_ids = config_inbound[:block_caller_ids] || [] @@ -31,12 +28,12 @@ class Integration::SipgateController < ApplicationController if params['user'] params['comment'] = "#{params['user']} -> reject, busy" end - update_log(params) + Cti::Log.process(params) return true } end - update_log(params) + Cti::Log.process(params) xml = Builder::XmlMarkup.new(indent: 2) xml.instruct! @@ -46,10 +43,6 @@ class Integration::SipgateController < ApplicationController # set caller id of outbound call def out - http_log_config facility: 'sipgate.io' - return if !configured? - - config = Setting.get('sipgate_config') config_outbound = config[:outbound][:routing_table] default_caller_id = config[:outbound][:default_caller_id] @@ -84,125 +77,26 @@ class Integration::SipgateController < ApplicationController if from params['from'] = from end - update_log(params) + Cti::Log.process(params) end private - def configured? + def check_configured + http_log_config facility: 'sipgate.io' + if !Setting.get('sipgate_integration') xml_error('Feature is disable, please contact your admin to enable it!') - return false + return end - config = Setting.get('sipgate_config') if !config || !config[:inbound] || !config[:outbound] xml_error('Feature not configured, please contact your admin!') - return false + return end - true end - def update_log(params) - - user = params['user'] - if params['user'] && params['user'].class == Array - user = params['user'].join(', ') - end - from_comment = nil - to_comment = nil - preferences = nil - if params['direction'] == 'in' - to_comment = user - from_comment, preferences = update_log_item('from') - else - from_comment = user - to_comment, preferences = update_log_item('to') - end - - comment = nil - if params['cause'] - comment = params['cause'] - end - - if params['event'] == 'newCall' - Cti::Log.create( - direction: params['direction'], - from: params['from'], - from_comment: from_comment, - to: params['to'], - to_comment: to_comment, - call_id: params['callId'], - comment: comment, - state: params['event'], - preferences: preferences, - ) - elsif params['event'] == 'answer' - log = Cti::Log.find_by(call_id: params['callId']) - raise "No such call_id #{params['callId']}" if !log - log.state = 'answer' - log.start = Time.zone.now - if user - log.to_comment = user - end - log.comment = comment - log.save - elsif params['event'] == 'hangup' - log = Cti::Log.find_by(call_id: params['callId']) - raise "No such call_id #{params['callId']}" if !log - if params['direction'] == 'in' && log.state == 'newCall' - log.done = false - end - if params['direction'] == 'in' && log.to_comment == 'voicemail' - log.done = false - end - log.state = 'hangup' - log.end = Time.zone.now - log.comment = comment - log.save - else - raise "Unknown event #{params['event']}" - end - - end - - def update_log_item(direction) - from_comment_known = '' - from_comment_maybe = '' - preferences_known = {} - preferences_known[direction] = [] - preferences_maybe = {} - preferences_maybe[direction] = [] - caller_ids = Cti::CallerId.lookup(params[direction]) - caller_ids.each { |record| - if record.level == 'known' - preferences_known[direction].push record - else - preferences_maybe[direction].push record - end - comment = '' - if record.user_id - user = User.lookup(id: record.user_id) - if user - comment += user.fullname - end - elsif !record.comment.empty? - comment += record.comment - end - if record.level == 'known' - if !from_comment_known.empty? - from_comment_known += ',' - end - from_comment_known += comment - else - if !from_comment_maybe.empty? - from_comment_maybe += ',' - end - from_comment_maybe += comment - end - } - return [from_comment_known, preferences_known] if !from_comment_known.empty? - return ["maybe #{from_comment_maybe}", preferences_maybe] if !from_comment_maybe.empty? - nil + def config + @config ||= Setting.get('sipgate_config') end def xml_error(error) diff --git a/app/models/cti/caller_id.rb b/app/models/cti/caller_id.rb index f016d7d42..a00599c07 100644 --- a/app/models/cti/caller_id.rb +++ b/app/models/cti/caller_id.rb @@ -233,5 +233,45 @@ returns caller_ids end + def self.get_comment_preferences(caller_id, direction) + from_comment_known = '' + from_comment_maybe = '' + preferences_known = {} + preferences_known[direction] = [] + preferences_maybe = {} + preferences_maybe[direction] = [] + + self.lookup(caller_id).each { |record| + if record.level == 'known' + preferences_known[direction].push record + else + preferences_maybe[direction].push record + end + comment = '' + if record.user_id + user = User.lookup(id: record.user_id) + if user + comment += user.fullname + end + elsif !record.comment.empty? + comment += record.comment + end + if record.level == 'known' + if !from_comment_known.empty? + from_comment_known += ',' + end + from_comment_known += comment + else + if !from_comment_maybe.empty? + from_comment_maybe += ',' + end + from_comment_maybe += comment + end + } + return [from_comment_known, preferences_known] if !from_comment_known.empty? + return ["maybe #{from_comment_maybe}", preferences_maybe] if !from_comment_maybe.empty? + nil + end + end end diff --git a/app/models/cti/log.rb b/app/models/cti/log.rb index fc6046ae9..35baed1bc 100644 --- a/app/models/cti/log.rb +++ b/app/models/cti/log.rb @@ -254,6 +254,67 @@ returns } end + # processes a incoming event + def self.process(params) + comment = params['cause'] + event = params['event'] + user = params['user'] + if Array === user + user = user.join(', ') + end + + from_comment = nil + to_comment = nil + preferences = nil + if params['direction'] == 'in' + to_comment = user + from_comment, preferences = CallerId.get_comment_preferences(params['from'], params['direction']) + else + from_comment = user + to_comment, preferences = CallerId.get_comment_preferences(params['to'], params['direction']) + end + + case event + when 'newCall' + self.create( + direction: params['direction'], + from: params['from'], + from_comment: from_comment, + to: params['to'], + to_comment: to_comment, + call_id: params['callId'], + comment: comment, + state: event, + preferences: preferences, + ) + when 'answer' + log = self.find_by(call_id: params['callId']) + raise "No such call_id #{params['callId']}" if !log + log.state = 'answer' + log.start = Time.zone.now + if user + log.to_comment = user + end + log.comment = comment + log.save + when 'hangup' + log = self.find_by(call_id: params['callId']) + raise "No such call_id #{params['callId']}" if !log + if params['direction'] == 'in' && log.state == 'newCall' + log.done = false + end + if params['direction'] == 'in' && log.to_comment == 'voicemail' + log.done = false + end + log.state = 'hangup' + log.end = Time.zone.now + log.comment = comment + log.save + else + raise ArgumentError, "Unknown event #{event}" + end + end + def push_event users = User.with_permissions('cti.agent') users.each { |user|