ip addresses: move more logic to Danbooru::IpAddress.

* Move `is_local?` from IpLookup to Danbooru::IpAddress.
* Refactor more things to use Danbooru::IpAddress instead of using
  IPAddress directly.
This commit is contained in:
evazion
2021-03-01 16:35:55 -06:00
parent 35a0c6b11f
commit 92b8f24724
8 changed files with 47 additions and 37 deletions

View File

@@ -37,7 +37,7 @@ class IpAddress < ApplicationRecord
end
def lookup
@lookup ||= IpLookup.new(ip_addr)
@lookup ||= ip_addr.ip_lookup
end
def to_s

View File

@@ -1,6 +1,9 @@
# An IpGeolocation contains metadata associated with an IP address, primarily geolocation data.
class IpGeolocation < ApplicationRecord
attribute :ip_addr, :ip_address
attribute :network, :ip_address
has_many :user_sessions, foreign_key: :ip_addr, primary_key: :ip_addr
def self.visible(user)
@@ -17,13 +20,12 @@ class IpGeolocation < ApplicationRecord
q
end
def self.create_or_update!(ip_addr)
ip_lookup = IpLookup.new(ip_addr)
return nil if ip_lookup.is_local?
return nil if ip_lookup.ip_info.blank?
def self.create_or_update!(ip)
ip_address = Danbooru::IpAddress.new(ip)
return nil if ip_address.ip_info.blank?
ip_geolocation = IpGeolocation.create_with(**ip_lookup.ip_info).create_or_find_by!(ip_addr: ip_addr)
ip_geolocation.update!(**ip_lookup.ip_info)
ip_geolocation = IpGeolocation.create_with(**ip_address.ip_info).create_or_find_by!(ip_addr: ip_address)
ip_geolocation.update!(**ip_address.ip_info)
ip_geolocation
end
end

View File

@@ -3,6 +3,8 @@
# and their browser user agent. This is used to track logins and other events.
class UserSession < ApplicationRecord
attribute :ip_addr, :ip_address
belongs_to :ip_geolocation, foreign_key: :ip_addr, primary_key: :ip_addr, optional: true
def self.visible(user)