diff --git a/app/controllers/integration/sipgate_controller.rb b/app/controllers/integration/sipgate_controller.rb index 888094541..e7c511969 100644 --- a/app/controllers/integration/sipgate_controller.rb +++ b/app/controllers/integration/sipgate_controller.rb @@ -28,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! @@ -77,7 +77,7 @@ class Integration::SipgateController < ApplicationController if from params['from'] = from end - update_log(params) + Cti::Log.process(params) end private @@ -99,107 +99,6 @@ class Integration::SipgateController < ApplicationController @config ||= Setting.get('sipgate_config') end - def update_log(params) - 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 = update_log_item('from') - else - from_comment = user - to_comment, preferences = update_log_item('to') - end - - comment = params['cause'] - - case params['event'] - when '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, - ) - when '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 - when '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 - end - def xml_error(error) xml = Builder::XmlMarkup.new(indent: 2) xml.instruct! 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|