FIX: The "too similar" check happened when trying to make a post a wiki

This commit is contained in:
Robin Ward 2015-02-02 12:44:21 -05:00
parent b56999e984
commit f15b0d205f
5 changed files with 30 additions and 5 deletions

View File

@ -121,7 +121,7 @@ class Post < ActiveRecord::Base
# The key we use in redis to ensure unique posts # The key we use in redis to ensure unique posts
def unique_post_key def unique_post_key
"post-#{user_id}:#{raw_hash}" "unique-post-#{user_id}:#{raw_hash}"
end end
def store_unique_post_key def store_unique_post_key
@ -132,7 +132,7 @@ class Post < ActiveRecord::Base
def matches_recent_post? def matches_recent_post?
post_id = $redis.get(unique_post_key) post_id = $redis.get(unique_post_key)
post_id != nil and post_id != id post_id != nil and post_id.to_i != id
end end
def raw_hash def raw_hash

View File

@ -70,6 +70,10 @@ class DiscourseRedis
} }
end end
def delete_prefixed(prefix)
keys("#{prefix}*").each { |k| $redis.del(k) }
end
def flushdb def flushdb
keys.each{|k| del(k)} keys.each{|k| del(k)}
end end

View File

@ -211,6 +211,7 @@ class PostRevisor
clear_flags_and_unhide_post clear_flags_and_unhide_post
@post.extract_quoted_post_numbers @post.extract_quoted_post_numbers
@post_successfully_saved = @post.save(validate: @validate_post) @post_successfully_saved = @post.save(validate: @validate_post)
@post.save_reply_relationships @post.save_reply_relationships
end end

View File

@ -4,7 +4,9 @@ require_dependency 'rate_limiter/on_create_record'
# A redis backed rate limiter. # A redis backed rate limiter.
class RateLimiter class RateLimiter
KEY_PREFIX = "l-rate-limit:" def self.key_prefix
"l-rate-limit:"
end
def self.disable def self.disable
@disabled = true @disabled = true
@ -20,12 +22,12 @@ class RateLimiter
end end
def self.clear_all! def self.clear_all!
$redis.keys("#{KEY_PREFIX}:*").each { |k| $redis.del(k) } $redis.delete_prefixed(RateLimiter.key_prefix)
end end
def initialize(user, key, max, secs) def initialize(user, key, max, secs)
@user = user @user = user
@key = "#{KEY_PREFIX}:#{@user && @user.id}:#{key}" @key = "#{RateLimiter.key_prefix}:#{@user && @user.id}:#{key}"
@max = max @max = max
@secs = secs @secs = secs
end end

View File

@ -39,6 +39,24 @@ describe PostRevisor do
end end
end end
context 'revise wiki' do
before do
# There used to be a bug where wiki changes were considered posting "too similar"
# so this is enabled and checked
$redis.delete_prefixed('unique-post')
SiteSetting.unique_posts_mins = 10
end
it 'allows the user to change it to a wiki' do
pc = PostCreator.new(newuser, topic_id: topic.id, raw: 'this is a post that will become a wiki')
post = pc.create
post.revise(post.user, wiki: true).should == true
post.reload
post.wiki.should be_true
end
end
context 'revise' do context 'revise' do
let(:post) { Fabricate(:post, post_args) } let(:post) { Fabricate(:post, post_args) }
let(:first_version_at) { post.last_version_at } let(:first_version_at) { post.last_version_at }