sipgate: Move logic from controller to models
This commit is contained in:
parent
d0e3810a20
commit
89e6dcf0b5
3 changed files with 104 additions and 104 deletions
|
@ -28,12 +28,12 @@ class Integration::SipgateController < ApplicationController
|
||||||
if params['user']
|
if params['user']
|
||||||
params['comment'] = "#{params['user']} -> reject, busy"
|
params['comment'] = "#{params['user']} -> reject, busy"
|
||||||
end
|
end
|
||||||
update_log(params)
|
Cti::Log.process(params)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
update_log(params)
|
Cti::Log.process(params)
|
||||||
|
|
||||||
xml = Builder::XmlMarkup.new(indent: 2)
|
xml = Builder::XmlMarkup.new(indent: 2)
|
||||||
xml.instruct!
|
xml.instruct!
|
||||||
|
@ -77,7 +77,7 @@ class Integration::SipgateController < ApplicationController
|
||||||
if from
|
if from
|
||||||
params['from'] = from
|
params['from'] = from
|
||||||
end
|
end
|
||||||
update_log(params)
|
Cti::Log.process(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -99,107 +99,6 @@ class Integration::SipgateController < ApplicationController
|
||||||
@config ||= Setting.get('sipgate_config')
|
@config ||= Setting.get('sipgate_config')
|
||||||
end
|
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)
|
def xml_error(error)
|
||||||
xml = Builder::XmlMarkup.new(indent: 2)
|
xml = Builder::XmlMarkup.new(indent: 2)
|
||||||
xml.instruct!
|
xml.instruct!
|
||||||
|
|
|
@ -233,5 +233,45 @@ returns
|
||||||
caller_ids
|
caller_ids
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -254,6 +254,67 @@ returns
|
||||||
}
|
}
|
||||||
end
|
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
|
def push_event
|
||||||
users = User.with_permissions('cti.agent')
|
users = User.with_permissions('cti.agent')
|
||||||
users.each { |user|
|
users.each { |user|
|
||||||
|
|
Loading…
Reference in a new issue