FIX: UserProfileView: Do not log IP of logged-in users

This commit is contained in:
riking 2018-05-21 12:40:15 -07:00 committed by Sam
parent 7e8ef5d6e1
commit 501bc0e9af
3 changed files with 31 additions and 4 deletions

View File

@ -1,5 +1,5 @@
class UserProfileView < ActiveRecord::Base class UserProfileView < ActiveRecord::Base
validates_presence_of :user_profile_id, :ip_address, :viewed_at validates_presence_of :user_profile_id, :viewed_at
belongs_to :user_profile belongs_to :user_profile
@ -9,6 +9,7 @@ class UserProfileView < ActiveRecord::Base
if user_id if user_id
return if user_id < 1 return if user_id < 1
redis_key << ":user-#{user_id}" redis_key << ":user-#{user_id}"
ip = nil
else else
redis_key << ":ip-#{ip}" redis_key << ":ip-#{ip}"
end end
@ -59,13 +60,12 @@ end
# id :integer not null, primary key # id :integer not null, primary key
# user_profile_id :integer not null # user_profile_id :integer not null
# viewed_at :datetime not null # viewed_at :datetime not null
# ip_address :inet not null # ip_address :inet
# user_id :integer # user_id :integer
# #
# Indexes # Indexes
# #
# index_user_profile_views_on_user_id (user_id) # index_user_profile_views_on_user_id (user_id)
# index_user_profile_views_on_user_profile_id (user_profile_id) # index_user_profile_views_on_user_profile_id (user_profile_id)
# unique_profile_view_ip (viewed_at,ip_address,user_profile_id) UNIQUE # unique_profile_view_user_or_ip (viewed_at,user_id,ip_address,user_profile_id) UNIQUE
# unique_profile_view_user (viewed_at,user_id,user_profile_id) UNIQUE
# #

View File

@ -0,0 +1,25 @@
class AllowNullIpUserProfileView < ActiveRecord::Migration[5.1]
def up
begin
Migration::SafeMigrate.disable!
change_column :user_profile_views, :ip_address, :inet, null: true
ensure
Migration::SafeMigrate.enable!
end
remove_index :user_profile_views,
column: [:viewed_at, :ip_address, :user_profile_id],
name: :unique_profile_view_ip,
unique: true
remove_index :user_profile_views,
column: [:viewed_at, :user_id, :user_profile_id],
name: :unique_profile_view_user,
unique: true
add_index :user_profile_views, [:viewed_at, :user_id, :ip_address, :user_profile_id],
name: :unique_profile_view_user_or_ip,
unique: true
end
def down
end
end

View File

@ -34,6 +34,8 @@ RSpec.describe UserProfileView do
['1.1.1.1', '2.2.2.2'].each do |ip| ['1.1.1.1', '2.2.2.2'].each do |ip|
add(user_profile_id, ip, other_user.id, time) add(user_profile_id, ip, other_user.id, time)
expect(described_class.count).to eq(1) expect(described_class.count).to eq(1)
# should not actually log IPs
expect(UserProfileView.where(user_id: other_user.id).count(:ip_address)).to eq(0)
end end
end end