Skip to content

Commit

Permalink
Add GUIDs to comments and trackbacks, and clean up Article GUID code.…
Browse files Browse the repository at this point in the history
… Use GUIDs in Atom 1.0 feeds because our old IDs were nasty and not unique. Still thinking about adding them to RSS 2.0 and Atom 0.3; it'll be user-visible if we change them.

git-svn-id: http://svn.typosphere.org/typo/trunk@636 820eb932-12ee-0310-9ca8-eeb645f39767
  • Loading branch information
scott committed Sep 6, 2005
1 parent 837d01c commit afa5906
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 22 deletions.
9 changes: 3 additions & 6 deletions app/models/article.rb
@@ -1,8 +1,9 @@
require 'uri'
require 'net/http'
require 'md5'

class Article < ActiveRecord::Base
include TypoGuid

has_many :pings, :dependent => true, :order => "created_at ASC"
has_many :comments, :dependent => true, :order => "created_at ASC"
has_many :trackbacks, :dependent => true, :order => "created_at ASC"
Expand Down Expand Up @@ -121,7 +122,7 @@ def keywords_to_tags

protected

before_save :set_defaults
before_save :set_defaults, :create_guid

def set_defaults
begin
Expand All @@ -138,10 +139,6 @@ def set_defaults
self.permalink = self.stripped_title if self.attributes.include?("permalink") and self.permalink.blank?
end

if schema_version >= 9
self.guid = Digest::MD5.new(self.body.to_s+self.extended.to_s+self.title.to_s+self.permalink.to_s+self.author.to_s+Time.now.to_f.to_s).to_s if self.guid.blank?
end

if schema_version >= 10
keywords_to_tags
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/comment.rb
@@ -1,6 +1,8 @@
require_dependency 'transforms'

class Comment < ActiveRecord::Base
include TypoGuid

belongs_to :article
belongs_to :user

Expand All @@ -10,7 +12,7 @@ class Comment < ActiveRecord::Base

protected

before_save :correct_url, :make_nofollow
before_save :correct_url, :make_nofollow, :create_guid

def correct_url
unless url.to_s.empty?
Expand Down
3 changes: 2 additions & 1 deletion app/models/trackback.rb
@@ -1,14 +1,15 @@
require_dependency 'transforms'

class Trackback < ActiveRecord::Base
include TypoGuid
belongs_to :article

validates_age_of :article_id
validates_against_spamdb :title, :excerpt, :ip, :url
validates_presence_of :title, :excerpt, :blog_name, :url

protected
before_save :make_nofollow, :process_trackback
before_save :make_nofollow, :process_trackback, :create_guid

def make_nofollow
self.blog_name = strip_html(blog_name)
Expand Down
2 changes: 1 addition & 1 deletion app/views/xml/_atom10_item_comment.rxml
@@ -1,6 +1,6 @@
xm.entry do
xm.author { xm.name item.author }
xm.id "tag:#{controller.request.host},2005:Typo-#{item.id}"
xm.id "urn:uuid:#{item.guid}"

xm.published item.created_at.xmlschema
xm.updated item.updated_at.xmlschema
Expand Down
2 changes: 1 addition & 1 deletion app/views/xml/_atom10_item_trackback.rxml
@@ -1,5 +1,5 @@
xm.entry do
xm.id "tag:#{controller.request.host},2005:Typo-#{item.id}"
xm.id "urn:uuid:#{item.guid}"

xm.author do
xm.name item.blog_name
Expand Down
1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -97,6 +97,7 @@
require_dependency 'configuration'
require_dependency 'spam_protection'
require_dependency 'xmlrpc_fix'
require_dependency 'guid'

ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(:database_manager => CGI::Session::ActiveRecordStore)
ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::FileStore.new("#{RAILS_ROOT}/cache/fragment/")
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/18_add_guids.rb
@@ -0,0 +1,21 @@
class AddGuids < ActiveRecord::Migration
def self.up
STDERR.puts "Adding GUIDs to Comments and Trackbacks"
Comment.transaction do
add_column :comments, :guid, :string
add_column :trackbacks, :guid, :string

Comment.find(:all).each do |c|
c.save if c.guid.blank?
end
Trackback.find(:all).each do |t|
t.save if t.guid.blank?
end
end
end

def self.down
remove_column :comments, :guid
remove_column :trackbacks, :guid
end
end
8 changes: 5 additions & 3 deletions db/schema.mysql-v3.sql
Expand Up @@ -64,7 +64,8 @@ CREATE TABLE comments (
body_html text,
created_at datetime,
updated_at datetime,
user_id int(11)
user_id int(11),
guid varchar(255)
) ENGINE=MyISAM;

CREATE TABLE page_caches (
Expand Down Expand Up @@ -150,7 +151,8 @@ CREATE TABLE trackbacks (
url varchar(255),
ip varchar(255),
created_at datetime,
updated_at datetime
updated_at datetime,
guid varchar(255)
) ENGINE=MyISAM;

CREATE TABLE users (
Expand Down Expand Up @@ -191,4 +193,4 @@ CREATE TABLE schema_info (
version int(11)
) ENGINE=MyISAM;

insert into schema_info (version) values (17);
insert into schema_info (version) values (18);
8 changes: 5 additions & 3 deletions db/schema.mysql.sql
Expand Up @@ -64,7 +64,8 @@ CREATE TABLE comments (
body_html text,
created_at datetime,
updated_at datetime,
user_id int(11)
user_id int(11),
guid varchar(255)
) ENGINE=InnoDB;

CREATE TABLE page_caches (
Expand Down Expand Up @@ -150,7 +151,8 @@ CREATE TABLE trackbacks (
url varchar(255),
ip varchar(255),
created_at datetime,
updated_at datetime
updated_at datetime,
guid varchar(255)
) ENGINE=InnoDB;

CREATE TABLE users (
Expand Down Expand Up @@ -191,4 +193,4 @@ CREATE TABLE schema_info (
version int(11)
) ENGINE=InnoDB;

insert into schema_info (version) values (17);
insert into schema_info (version) values (18);
8 changes: 5 additions & 3 deletions db/schema.postgresql.sql
Expand Up @@ -64,7 +64,8 @@ CREATE TABLE comments (
body_html text,
created_at timestamp,
updated_at timestamp,
user_id integer
user_id integer,
guid character varying(255)
);

CREATE TABLE page_caches (
Expand Down Expand Up @@ -150,7 +151,8 @@ CREATE TABLE trackbacks (
url character varying(255),
ip character varying(255),
created_at timestamp,
updated_at timestamp
updated_at timestamp,
guid character varying(255)
);

CREATE TABLE users (
Expand Down Expand Up @@ -191,4 +193,4 @@ CREATE TABLE schema_info (
version integer
);

insert into schema_info (version) values (17);
insert into schema_info (version) values (18);
8 changes: 5 additions & 3 deletions db/schema.sqlite.sql
Expand Up @@ -64,7 +64,8 @@ CREATE TABLE comments (
body_html text,
created_at datetime,
updated_at datetime,
user_id integer
user_id integer,
guid varchar(255)
);

CREATE TABLE page_caches (
Expand Down Expand Up @@ -150,7 +151,8 @@ CREATE TABLE trackbacks (
url varchar(255),
ip varchar(255),
created_at datetime,
updated_at datetime
updated_at datetime,
guid varchar(255)
);

CREATE TABLE users (
Expand Down Expand Up @@ -191,4 +193,4 @@ CREATE TABLE schema_info (
version integer
);

insert into schema_info (version) values (17);
insert into schema_info (version) values (18);
16 changes: 16 additions & 0 deletions lib/guid.rb
@@ -0,0 +1,16 @@
require 'md5'

module TypoGuid
def create_guid
self.guid rescue return true
return true unless self.guid.blank?

guid_text = self.inspect+Time.now.to_f.to_s
begin
guid_text += File.open("/dev/urandom",'r').read(16)
rescue => err
end
self.guid = Digest::MD5.new(guid_text).to_s
true
end
end
2 changes: 2 additions & 0 deletions test/fixtures/comments.yml
Expand Up @@ -8,6 +8,7 @@ spam_comment:
body: Test <a href="http://fakeurl.co.uk">body</a>
created_at: 2005-01-01 02:00:00
updated_at: 2005-01-01 02:00:00
guid: 12313123123123123

comment2:
id: 2
Expand All @@ -19,3 +20,4 @@ comment2:
body: Comment body _italic_ *bold*
created_at: 2005-01-01 02:00:01
updated_at: 2005-01-01 02:00:01
guid: 453456456456456
2 changes: 2 additions & 0 deletions test/fixtures/trackbacks.yml
Expand Up @@ -7,6 +7,7 @@ trackback1:
excerpt: This is an excerpt
created_at: 2005-01-01 03:00:00
updated_at: 2005-01-01 03:00:00
guid: dsafsadffsdsf

trackback2:
id: 2
Expand All @@ -17,3 +18,4 @@ trackback2:
excerpt: This is an excerpt
created_at: 2005-01-01 03:00:01
updated_at: 2005-01-01 03:00:01
guid: qweqweqddsfcs
11 changes: 11 additions & 0 deletions test/unit/comment_test.rb
Expand Up @@ -19,6 +19,17 @@ def test_save_spam
assert_equal "http://fakeurl.com", @spam_comment.url
end

def test_create_comment
c = Comment.new
c.author = 'Bob'
c.article_id = 1
c.body = 'nice post'
c.ip = '1.2.3.4'

assert c.save
assert c.guid.size > 15
end

def test_reject_spam_rbl
c = Comment.new
c.author "Spammer"
Expand Down
1 change: 1 addition & 0 deletions test/unit/trackback_test.rb
Expand Up @@ -21,6 +21,7 @@ def test_incomplete
tb.url = "http://foo.com"
assert tb.save
assert tb.errors.empty?
assert tb.guid.size > 15
end

def test_reject_spam_rbl
Expand Down

0 comments on commit afa5906

Please sign in to comment.