Skip to content

Commit

Permalink
UX: Use user locale for locations. (#6527)
Browse files Browse the repository at this point in the history
* UX: Use user locale for locations.

* DEV: Added MaxMindDB test data and fixed test.
  • Loading branch information
nbianca authored and ZogStriP committed Oct 25, 2018
1 parent 8e274f7 commit effbef7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/serializers/concerns/user_auth_tokens_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def client_ip
end

def location
ipinfo = DiscourseIpInfo.get(client_ip)
ipinfo = DiscourseIpInfo.get(client_ip, I18n.locale)

location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ")
return I18n.t('staff_action_logs.unknown') if location.blank?
Expand Down
28 changes: 19 additions & 9 deletions lib/discourse_ip_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ class DiscourseIpInfo
include Singleton

def initialize
open_db(File.join(Rails.root, 'vendor', 'data'))
end

def open_db(path)
begin
@mmdb_filename = File.join(Rails.root, 'vendor', 'data', 'GeoLite2-City.mmdb')
@mmdb_filename = File.join(path, 'GeoLite2-City.mmdb')
@mmdb = MaxMindDB.new(@mmdb_filename, MaxMindDB::LOW_MEMORY_FILE_READER)
@cache = LruRedux::ThreadSafeCache.new(1000)
rescue Errno::ENOENT => e
Expand All @@ -15,7 +19,7 @@ def initialize
end
end

def lookup(ip)
def lookup(ip, locale = :en)
return {} unless @mmdb

begin
Expand All @@ -26,22 +30,28 @@ def lookup(ip)

return {} if !result || !result.found?

locale = locale.to_s.sub('_', '-')

{
country: result.country.name,
country: result.country.name(locale) || result.country.name,
country_code: result.country.iso_code,
region: result.subdivisions.most_specific.name,
city: result.city.name,
region: result.subdivisions.most_specific.name(locale) || result.subdivisions.most_specific.name,
city: result.city.name(locale) || result.city.name,
}
end

def get(ip)
def get(ip, locale = :en)
return {} unless @mmdb

ip = ip.to_s
@cache[ip] ||= lookup(ip)
@cache["#{ip}-#{locale}"] ||= lookup(ip, locale)
end

def self.open_db(path)
instance.open_db(path)
end

def self.get(ip)
instance.get(ip)
def self.get(ip, locale = :en)
instance.get(ip, locale)
end
end
Binary file added spec/fixtures/mmdb/GeoLite2-City.mmdb
Binary file not shown.
23 changes: 23 additions & 0 deletions spec/serializers/user_auth_token_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rails_helper'

describe UserAuthTokenSerializer do

let(:user) { Fabricate(:user) }
let(:token) { UserAuthToken.generate!(user_id: user.id, client_ip: '2a02:ea00::') }

before(:each) do
DiscourseIpInfo.open_db(File.join(Rails.root, 'spec', 'fixtures', 'mmdb'))
end

it 'serializes user auth tokens with respect to user locale' do
I18n.locale = 'de'
json = UserAuthTokenSerializer.new(token, scope: Guardian.new(user), root: false).as_json
expect(json[:location]).to include('Schweiz')
end

it 'correctly translates Discourse locale to MaxMindDb locale' do
I18n.locale = 'zh_CN'
json = UserAuthTokenSerializer.new(token, scope: Guardian.new(user), root: false).as_json
expect(json[:location]).to include('瑞士')
end
end

0 comments on commit effbef7

Please sign in to comment.