UX: Use user locale for locations. (#6527)

* UX: Use user locale for locations.

* DEV: Added MaxMindDB test data and fixed test.
This commit is contained in:
Bianca Nenciu 2018-10-25 13:54:01 +03:00 committed by Régis Hanol
parent 8e274f7296
commit effbef7d0b
4 changed files with 43 additions and 10 deletions

View File

@ -20,7 +20,7 @@ module UserAuthTokensMixin
end end
def location 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(", ") location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ")
return I18n.t('staff_action_logs.unknown') if location.blank? return I18n.t('staff_action_logs.unknown') if location.blank?

View File

@ -4,8 +4,12 @@ class DiscourseIpInfo
include Singleton include Singleton
def initialize def initialize
open_db(File.join(Rails.root, 'vendor', 'data'))
end
def open_db(path)
begin 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) @mmdb = MaxMindDB.new(@mmdb_filename, MaxMindDB::LOW_MEMORY_FILE_READER)
@cache = LruRedux::ThreadSafeCache.new(1000) @cache = LruRedux::ThreadSafeCache.new(1000)
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
@ -15,7 +19,7 @@ class DiscourseIpInfo
end end
end end
def lookup(ip) def lookup(ip, locale = :en)
return {} unless @mmdb return {} unless @mmdb
begin begin
@ -26,22 +30,28 @@ class DiscourseIpInfo
return {} if !result || !result.found? 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, country_code: result.country.iso_code,
region: result.subdivisions.most_specific.name, region: result.subdivisions.most_specific.name(locale) || result.subdivisions.most_specific.name,
city: result.city.name, city: result.city.name(locale) || result.city.name,
} }
end end
def get(ip) def get(ip, locale = :en)
return {} unless @mmdb return {} unless @mmdb
ip = ip.to_s ip = ip.to_s
@cache[ip] ||= lookup(ip) @cache["#{ip}-#{locale}"] ||= lookup(ip, locale)
end end
def self.get(ip) def self.open_db(path)
instance.get(ip) instance.open_db(path)
end
def self.get(ip, locale = :en)
instance.get(ip, locale)
end end
end end

BIN
spec/fixtures/mmdb/GeoLite2-City.mmdb vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -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