mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
FEATURE: Show "Recently used devices" in user preferences (#6335)
* FEATURE: Added MaxMindDb to resolve IP information. * FEATURE: Added browser detection based on user agent. * FEATURE: Added recently used devices in user preferences. * DEV: Added acceptance test for recently used devices. * UX: Do not show 'Show more' button if there aren't more tokens. * DEV: Fix unit tests. * DEV: Make changes after code review. * Add more detailed unit tests. * Improve logging messages. * Minor coding style fixes. * DEV: Use DropdownSelectBoxComponent and run Prettier. * DEV: Fix unit tests.
This commit is contained in:

committed by
Régis Hanol

parent
1fb1f4c790
commit
1d26a473e7
46
lib/discourse_ip_info.rb
Normal file
46
lib/discourse_ip_info.rb
Normal file
@ -0,0 +1,46 @@
|
||||
require_dependency 'maxminddb'
|
||||
|
||||
class DiscourseIpInfo
|
||||
include Singleton
|
||||
|
||||
def initialize
|
||||
begin
|
||||
@mmdb_filename = File.join(Rails.root, 'vendor', 'data', 'GeoLite2-City.mmdb')
|
||||
@mmdb = MaxMindDB.new(@mmdb_filename, MaxMindDB::LOW_MEMORY_FILE_READER)
|
||||
@cache = LruRedux::ThreadSafeCache.new(1000)
|
||||
rescue Errno::ENOENT => e
|
||||
Rails.logger.warn("MaxMindDB could not be found: #{e}")
|
||||
rescue
|
||||
Rails.logger.warn("MaxMindDB could not be loaded.")
|
||||
end
|
||||
end
|
||||
|
||||
def lookup(ip)
|
||||
return {} unless @mmdb
|
||||
|
||||
begin
|
||||
result = @mmdb.lookup(ip)
|
||||
rescue
|
||||
Rails.logger.error("IP #{ip} could not be looked up in MaxMindDB.")
|
||||
end
|
||||
|
||||
return {} if !result || !result.found?
|
||||
|
||||
{
|
||||
country: result.country.name,
|
||||
country_code: result.country.iso_code,
|
||||
region: result.subdivisions.most_specific.name,
|
||||
city: result.city.name,
|
||||
}
|
||||
end
|
||||
|
||||
def get(ip)
|
||||
return {} unless @mmdb
|
||||
|
||||
@cache[ip] ||= lookup(ip)
|
||||
end
|
||||
|
||||
def self.get(ip)
|
||||
instance.get(ip)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user