forked from chatwoot/chatwoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(poc): Disable widget based on country (chatwoot#6658)
- Loading branch information
1 parent
e8a174f
commit eb7070d
Showing
8 changed files
with
72 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
release: POSTGRES_STATEMENT_TIMEOUT=600s bundle exec rails db:chatwoot_prepare | ||
web: bin/rails server -p $PORT -e $RAILS_ENV | ||
worker: bundle exec sidekiq -C config/sidekiq.yml | ||
web: bundle exec rails ip_lookup:setup && bin/rails server -p $PORT -e $RAILS_ENV | ||
worker: bundle exec rails ip_lookup:setup && bundle exec sidekiq -C config/sidekiq.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
enterprise/app/controllers/enterprise/widgets_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Enterprise::WidgetsController | ||
private | ||
|
||
def ensure_location_is_supported | ||
countries = @web_widget.inbox.account.custom_attributes['allowed_countries'] | ||
return if countries.blank? | ||
|
||
geocoder_result = IpLookupService.new.perform(request.remote_ip) | ||
return unless geocoder_result | ||
|
||
country_enabled = countries.include?(geocoder_result.country_code) | ||
render json: { error: 'Location is not supported' }, status: :unauthorized unless country_enabled | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'rubygems/package' | ||
|
||
namespace :ip_lookup do | ||
task setup: :environment do | ||
next if File.exist?(GeocoderConfiguration::LOOK_UP_DB) | ||
|
||
ip_lookup_api_key = ENV.fetch('IP_LOOKUP_API_KEY') | ||
next if ip_lookup_api_key.blank? | ||
|
||
puts '[rake ip_lookup:setup] Fetch GeoLite2-City database' | ||
|
||
begin | ||
base_url = 'https://download.maxmind.com/app/geoip_download' | ||
source_file = Down.download( | ||
"#{base_url}?edition_id=GeoLite2-City&suffix=tar.gz&license_key=#{ip_lookup_api_key}" | ||
) | ||
|
||
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 | ||
end | ||
puts '[rake ip_lookup:setup] Fetch complete' | ||
rescue StandardError => e | ||
puts "[rake ip_lookup:setup] #{e.message}" | ||
end | ||
end | ||
end |