mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-04-30 16:04:36 -04:00
Merge pull request #1088 from ComputerDruid/fix-ipv6
Fix ipv6 addresses in the database
This commit is contained in:
commit
3af12ba7d3
10 changed files with 53 additions and 16 deletions
1
Gemfile
1
Gemfile
|
@ -26,7 +26,6 @@ gem 'fast_xs'
|
||||||
gem 'fast_xor', git: 'https://github.com/CodeMonkeySteve/fast_xor.git'
|
gem 'fast_xor', git: 'https://github.com/CodeMonkeySteve/fast_xor.git'
|
||||||
gem 'fastimage'
|
gem 'fastimage'
|
||||||
gem 'fog', require: false
|
gem 'fog', require: false
|
||||||
gem 'has_ip_address'
|
|
||||||
gem 'hiredis'
|
gem 'hiredis'
|
||||||
|
|
||||||
gem 'email_reply_parser', git: 'https://github.com/lawrencepit/email_reply_parser.git'
|
gem 'email_reply_parser', git: 'https://github.com/lawrencepit/email_reply_parser.git'
|
||||||
|
|
|
@ -212,7 +212,6 @@ GEM
|
||||||
guard (>= 1.1)
|
guard (>= 1.1)
|
||||||
spork (>= 0.8.4)
|
spork (>= 0.8.4)
|
||||||
handlebars-source (1.0.0.rc4)
|
handlebars-source (1.0.0.rc4)
|
||||||
has_ip_address (0.0.1)
|
|
||||||
hashie (2.0.4)
|
hashie (2.0.4)
|
||||||
highline (1.6.18)
|
highline (1.6.18)
|
||||||
hike (1.2.2)
|
hike (1.2.2)
|
||||||
|
@ -489,7 +488,6 @@ DEPENDENCIES
|
||||||
guard-rspec
|
guard-rspec
|
||||||
guard-spork
|
guard-spork
|
||||||
handlebars-source (= 1.0.0.rc4)
|
handlebars-source (= 1.0.0.rc4)
|
||||||
has_ip_address
|
|
||||||
highline
|
highline
|
||||||
hiredis
|
hiredis
|
||||||
image_optim
|
image_optim
|
||||||
|
|
|
@ -5,10 +5,8 @@ class TopicLinkClick < ActiveRecord::Base
|
||||||
belongs_to :topic_link, counter_cache: :clicks
|
belongs_to :topic_link, counter_cache: :clicks
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
has_ip_address :ip
|
|
||||||
|
|
||||||
validates_presence_of :topic_link_id
|
validates_presence_of :topic_link_id
|
||||||
validates_presence_of :ip
|
validates_presence_of :ip_address
|
||||||
|
|
||||||
# Create a click from a URL and post_id
|
# Create a click from a URL and post_id
|
||||||
def self.create_from(args={})
|
def self.create_from(args={})
|
||||||
|
@ -28,7 +26,7 @@ class TopicLinkClick < ActiveRecord::Base
|
||||||
rate_key = "link-clicks:#{link.id}:#{args[:user_id] || args[:ip]}"
|
rate_key = "link-clicks:#{link.id}:#{args[:user_id] || args[:ip]}"
|
||||||
if $redis.setnx(rate_key, "1")
|
if $redis.setnx(rate_key, "1")
|
||||||
$redis.expire(rate_key, 1.day.to_i)
|
$redis.expire(rate_key, 1.day.to_i)
|
||||||
create!(topic_link_id: link.id, user_id: args[:user_id], ip: args[:ip])
|
create!(topic_link_id: link.id, user_id: args[:user_id], ip_address: args[:ip])
|
||||||
end
|
end
|
||||||
|
|
||||||
args[:url]
|
args[:url]
|
||||||
|
@ -43,9 +41,9 @@ end
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# topic_link_id :integer not null
|
# topic_link_id :integer not null
|
||||||
# user_id :integer
|
# user_id :integer
|
||||||
# ip :integer not null
|
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
|
# ip_address :string not null
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
|
|
|
@ -3,7 +3,7 @@ require 'ipaddr'
|
||||||
class View < ActiveRecord::Base
|
class View < ActiveRecord::Base
|
||||||
belongs_to :parent, polymorphic: true
|
belongs_to :parent, polymorphic: true
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
validates_presence_of :parent_type, :parent_id, :ip, :viewed_at
|
validates_presence_of :parent_type, :parent_id, :ip_address, :viewed_at
|
||||||
|
|
||||||
# TODO: This could happen asyncronously
|
# TODO: This could happen asyncronously
|
||||||
def self.create_for(parent, ip, user=nil)
|
def self.create_for(parent, ip, user=nil)
|
||||||
|
@ -20,7 +20,7 @@ class View < ActiveRecord::Base
|
||||||
$redis.expire(redis_key, 1.day.to_i)
|
$redis.expire(redis_key, 1.day.to_i)
|
||||||
|
|
||||||
View.transaction do
|
View.transaction do
|
||||||
View.create(parent: parent, ip: IPAddr.new(ip).to_i, viewed_at: Date.today, user: user)
|
View.create(parent: parent, ip_address: ip, viewed_at: Date.today, user: user)
|
||||||
|
|
||||||
# Update the views count in the parent, if it exists.
|
# Update the views count in the parent, if it exists.
|
||||||
if parent.respond_to?(:views)
|
if parent.respond_to?(:views)
|
||||||
|
@ -37,9 +37,9 @@ end
|
||||||
#
|
#
|
||||||
# parent_id :integer not null
|
# parent_id :integer not null
|
||||||
# parent_type :string(50) not null
|
# parent_type :string(50) not null
|
||||||
# ip :integer not null
|
|
||||||
# viewed_at :date not null
|
# viewed_at :date not null
|
||||||
# user_id :integer
|
# user_id :integer
|
||||||
|
# ip_address :string not null
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
require 'ipaddr'
|
||||||
|
|
||||||
|
class ChangeIpToInetInTopicLinkClicks < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
add_column :topic_link_clicks, :ip_address, :inet
|
||||||
|
|
||||||
|
execute "UPDATE topic_link_clicks SET ip_address = inet(
|
||||||
|
(ip >> 24 & 255) || '.' ||
|
||||||
|
(ip >> 16 & 255) || '.' ||
|
||||||
|
(ip >> 8 & 255) || '.' ||
|
||||||
|
(ip >> 0 & 255)
|
||||||
|
);"
|
||||||
|
|
||||||
|
change_column :topic_link_clicks, :ip_address, :inet, { :null => false }
|
||||||
|
remove_column :topic_link_clicks, :ip
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
raise ActiveRecord::IrreversibleMigration
|
||||||
|
end
|
||||||
|
end
|
22
db/migrate/20130625022454_change_ip_to_inet_in_views.rb
Normal file
22
db/migrate/20130625022454_change_ip_to_inet_in_views.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
require 'ipaddr'
|
||||||
|
|
||||||
|
class ChangeIpToInetInViews < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
table = :views
|
||||||
|
add_column table, :ip_address, :inet
|
||||||
|
|
||||||
|
execute "UPDATE views SET ip_address = inet(
|
||||||
|
(ip >> 24 & 255) || '.' ||
|
||||||
|
(ip >> 16 & 255) || '.' ||
|
||||||
|
(ip >> 8 & 255) || '.' ||
|
||||||
|
(ip >> 0 & 255)
|
||||||
|
);"
|
||||||
|
|
||||||
|
change_column table, :ip_address, :inet, { :null => false }
|
||||||
|
remove_column table, :ip
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
raise ActiveRecord::IrreversibleMigration
|
||||||
|
end
|
||||||
|
end
|
|
@ -27,7 +27,6 @@ The following Ruby Gems are used in Discourse:
|
||||||
* [omniauth-facebook](https://github.com/mkdynamic/omniauth-facebook)
|
* [omniauth-facebook](https://github.com/mkdynamic/omniauth-facebook)
|
||||||
* [omniauth-twitter](https://github.com/arunagw/omniauth-twitter)
|
* [omniauth-twitter](https://github.com/arunagw/omniauth-twitter)
|
||||||
* [omniauth-github](https://github.com/intridea/omniauth-github)
|
* [omniauth-github](https://github.com/intridea/omniauth-github)
|
||||||
* [has_ip_address](https://rubygems.org/gems/has_ip_address)
|
|
||||||
* [vestal_versions](https://rubygems.org/gems/vestal_versions)
|
* [vestal_versions](https://rubygems.org/gems/vestal_versions)
|
||||||
* [uglifier](https://rubygems.org/gems/uglifier)
|
* [uglifier](https://rubygems.org/gems/uglifier)
|
||||||
* [nokogiri](https://rubygems.org/gems/nokogiri)
|
* [nokogiri](https://rubygems.org/gems/nokogiri)
|
||||||
|
|
|
@ -26,7 +26,7 @@ describe TopicLinkClick do
|
||||||
|
|
||||||
context 'create' do
|
context 'create' do
|
||||||
before do
|
before do
|
||||||
TopicLinkClick.create(topic_link: @topic_link, ip: '192.168.1.1')
|
TopicLinkClick.create(topic_link: @topic_link, ip_address: '192.168.1.1')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates the forum topic link click' do
|
it 'creates the forum topic link click' do
|
||||||
|
@ -39,7 +39,7 @@ describe TopicLinkClick do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'serializes and deserializes the IP' do
|
it 'serializes and deserializes the IP' do
|
||||||
TopicLinkClick.first.ip.to_s.should == '192.168.1.1'
|
TopicLinkClick.first.ip_address.to_s.should == '192.168.1.1'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -236,7 +236,7 @@ describe TopicLink do
|
||||||
it 'has the correct results' do
|
it 'has the correct results' do
|
||||||
TopicLink.extract_from(post)
|
TopicLink.extract_from(post)
|
||||||
topic_link = post.topic.topic_links.first
|
topic_link = post.topic.topic_links.first
|
||||||
TopicLinkClick.create(topic_link: topic_link, ip: '192.168.1.1')
|
TopicLinkClick.create(topic_link: topic_link, ip_address: '192.168.1.1')
|
||||||
|
|
||||||
counts_for[post.id].should be_present
|
counts_for[post.id].should be_present
|
||||||
counts_for[post.id].find {|l| l[:url] == 'http://google.com'}[:clicks].should == 0
|
counts_for[post.id].find {|l| l[:url] == 'http://google.com'}[:clicks].should == 0
|
||||||
|
|
|
@ -6,7 +6,7 @@ describe View do
|
||||||
it { should belong_to :user }
|
it { should belong_to :user }
|
||||||
it { should validate_presence_of :parent_type }
|
it { should validate_presence_of :parent_type }
|
||||||
it { should validate_presence_of :parent_id }
|
it { should validate_presence_of :parent_id }
|
||||||
it { should validate_presence_of :ip }
|
it { should validate_presence_of :ip_address }
|
||||||
it { should validate_presence_of :viewed_at }
|
it { should validate_presence_of :viewed_at }
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue