Move the unique post key storage code into the Post model
This commit is contained in:
parent
48ee89940e
commit
78c15d5810
|
@ -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+/, ""))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue