feat(poc): Disable widget based on country (#6658)
This commit is contained in:
@@ -5,6 +5,7 @@ class WidgetsController < ActionController::Base
|
||||
before_action :set_global_config
|
||||
before_action :set_web_widget
|
||||
before_action :ensure_account_is_active
|
||||
before_action :ensure_location_is_supported
|
||||
before_action :set_token
|
||||
before_action :set_contact
|
||||
before_action :build_contact
|
||||
@@ -54,6 +55,8 @@ class WidgetsController < ActionController::Base
|
||||
render json: { error: 'Account is suspended' }, status: :unauthorized unless @web_widget.inbox.account.active?
|
||||
end
|
||||
|
||||
def ensure_location_is_supported; end
|
||||
|
||||
def additional_attributes
|
||||
if @web_widget.inbox.account.feature_enabled?('ip_lookup')
|
||||
{ created_at_ip: request.remote_ip }
|
||||
@@ -70,3 +73,5 @@ class WidgetsController < ActionController::Base
|
||||
response.headers.delete('X-Frame-Options')
|
||||
end
|
||||
end
|
||||
|
||||
WidgetsController.prepend_mod_with('WidgetsController')
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
require 'rubygems/package'
|
||||
|
||||
class ContactIpLookupJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(contact)
|
||||
return unless ensure_look_up_service
|
||||
|
||||
update_contact_location_from_ip(contact)
|
||||
rescue Errno::ETIMEDOUT => e
|
||||
Rails.logger.warn "Exception: ip resolution failed : #{e.message}"
|
||||
@@ -13,18 +9,8 @@ class ContactIpLookupJob < ApplicationJob
|
||||
|
||||
private
|
||||
|
||||
def ensure_look_up_service
|
||||
return if ENV['IP_LOOKUP_SERVICE'].blank? || ENV['IP_LOOKUP_API_KEY'].blank?
|
||||
return true if ENV['IP_LOOKUP_SERVICE'].to_sym != :geoip2
|
||||
|
||||
ensure_look_up_db
|
||||
end
|
||||
|
||||
def update_contact_location_from_ip(contact)
|
||||
ip = get_contact_ip(contact)
|
||||
return if ip.blank?
|
||||
|
||||
geocoder_result = Geocoder.search(ip).first
|
||||
geocoder_result = IpLookupService.new.perform(get_contact_ip(contact))
|
||||
return unless geocoder_result
|
||||
|
||||
contact.additional_attributes ||= {}
|
||||
@@ -37,28 +23,4 @@ class ContactIpLookupJob < ApplicationJob
|
||||
def get_contact_ip(contact)
|
||||
contact.additional_attributes&.dig('updated_at_ip') || contact.additional_attributes&.dig('created_at_ip')
|
||||
end
|
||||
|
||||
def ensure_look_up_db
|
||||
return true if File.exist?(GeocoderConfiguration::LOOK_UP_DB)
|
||||
|
||||
setup_vendor_db
|
||||
end
|
||||
|
||||
def setup_vendor_db
|
||||
base_url = 'https://download.maxmind.com/app/geoip_download'
|
||||
source_file = Down.download(
|
||||
"#{base_url}?edition_id=GeoLite2-City&suffix=tar.gz&license_key=#{ENV.fetch('IP_LOOKUP_API_KEY', nil)}"
|
||||
)
|
||||
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(source_file))
|
||||
tar_extract.rewind
|
||||
|
||||
tar_extract.each do |entry|
|
||||
next unless entry.full_name.include?('GeoLite2-City.mmdb') && entry.file?
|
||||
|
||||
File.open GeocoderConfiguration::LOOK_UP_DB, 'wb' do |f|
|
||||
f.print entry.read
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
15
app/services/ip_lookup_service.rb
Normal file
15
app/services/ip_lookup_service.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class IpLookupService
|
||||
def perform(ip_address)
|
||||
return if ip_address.blank? || !ip_database_available?
|
||||
|
||||
Geocoder.search(ip_address).first
|
||||
rescue Errno::ETIMEDOUT => e
|
||||
Rails.logger.warn "Exception: IP resolution failed :#{e.message}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ip_database_available?
|
||||
File.exist?(GeocoderConfiguration::LOOK_UP_DB)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user