mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Move the unique post key storage code into the Post model
This commit is contained in:
parent
48ee89940e
commit
78c15d5810
4 changed files with 14 additions and 6 deletions
|
@ -70,6 +70,16 @@ class Post < ActiveRecord::Base
|
||||||
"post-#{user_id}:#{raw_hash}"
|
"post-#{user_id}:#{raw_hash}"
|
||||||
end
|
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
|
def raw_hash
|
||||||
return if raw.blank?
|
return if raw.blank?
|
||||||
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
|
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
|
||||||
|
|
|
@ -220,9 +220,7 @@ class PostCreator
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_unique_post_key
|
def store_unique_post_key
|
||||||
if SiteSetting.unique_posts_mins > 0
|
@post.store_unique_post_key
|
||||||
$redis.setex(@post.unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, "1")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def consider_clearing_flags
|
def consider_clearing_flags
|
||||||
|
|
|
@ -64,7 +64,7 @@ class Validators::PostValidator < ActiveModel::Validator
|
||||||
# If the post is empty, default to the validates_presence_of
|
# If the post is empty, default to the validates_presence_of
|
||||||
return if post.raw.blank?
|
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))
|
post.errors.add(:raw, I18n.t(:just_posted_that))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ describe Validators::PostValidator do
|
||||||
|
|
||||||
context "post is unique" do
|
context "post is unique" do
|
||||||
before do
|
before do
|
||||||
$redis.stubs(:exists).with(post.unique_post_key).returns(nil)
|
post.stubs(:matches_recent_post?).returns(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not add an error" do
|
it "should not add an error" do
|
||||||
|
@ -49,7 +49,7 @@ describe Validators::PostValidator do
|
||||||
|
|
||||||
context "post is not unique" do
|
context "post is not unique" do
|
||||||
before do
|
before do
|
||||||
$redis.stubs(:exists).with(post.unique_post_key).returns('1')
|
post.stubs(:matches_recent_post?).returns(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add an error" do
|
it "should add an error" do
|
||||||
|
|
Loading…
Reference in a new issue