Move the unique post key storage code into the Post model

This commit is contained in:
Neil Lalonde 2013-09-09 16:17:31 -04:00
parent 48ee89940e
commit 78c15d5810
4 changed files with 14 additions and 6 deletions

View file

@ -70,6 +70,16 @@ class Post < ActiveRecord::Base
"post-#{user_id}:#{raw_hash}"
end
def store_unique_post_key
if SiteSetting.unique_posts_mins > 0
$redis.setex(unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, "1")
end
end
def matches_recent_post?
$redis.exists(unique_post_key)
end
def raw_hash
return if raw.blank?
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))

View file

@ -220,9 +220,7 @@ class PostCreator
end
def store_unique_post_key
if SiteSetting.unique_posts_mins > 0
$redis.setex(@post.unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, "1")
end
@post.store_unique_post_key
end
def consider_clearing_flags

View file

@ -64,7 +64,7 @@ class Validators::PostValidator < ActiveModel::Validator
# If the post is empty, default to the validates_presence_of
return if post.raw.blank?
if $redis.exists(post.unique_post_key)
if post.matches_recent_post?
post.errors.add(:raw, I18n.t(:just_posted_that))
end
end

View file

@ -38,7 +38,7 @@ describe Validators::PostValidator do
context "post is unique" do
before do
$redis.stubs(:exists).with(post.unique_post_key).returns(nil)
post.stubs(:matches_recent_post?).returns(false)
end
it "should not add an error" do
@ -49,7 +49,7 @@ describe Validators::PostValidator do
context "post is not unique" do
before do
$redis.stubs(:exists).with(post.unique_post_key).returns('1')
post.stubs(:matches_recent_post?).returns(true)
end
it "should add an error" do