From f15b0d205f62dad5ffc468cded650677497be148 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 2 Feb 2015 12:44:21 -0500 Subject: [PATCH] FIX: The "too similar" check happened when trying to make a post a wiki --- app/models/post.rb | 4 ++-- lib/discourse_redis.rb | 4 ++++ lib/post_revisor.rb | 1 + lib/rate_limiter.rb | 8 +++++--- spec/components/post_revisor_spec.rb | 18 ++++++++++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b13e2d780..94d63779e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -121,7 +121,7 @@ class Post < ActiveRecord::Base # The key we use in redis to ensure unique posts def unique_post_key - "post-#{user_id}:#{raw_hash}" + "unique-post-#{user_id}:#{raw_hash}" end def store_unique_post_key @@ -132,7 +132,7 @@ class Post < ActiveRecord::Base def matches_recent_post? post_id = $redis.get(unique_post_key) - post_id != nil and post_id != id + post_id != nil and post_id.to_i != id end def raw_hash diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index a41f05c1a..a7a17012b 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -70,6 +70,10 @@ class DiscourseRedis } end + def delete_prefixed(prefix) + keys("#{prefix}*").each { |k| $redis.del(k) } + end + def flushdb keys.each{|k| del(k)} end diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index c445668e7..c612e0453 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -211,6 +211,7 @@ class PostRevisor clear_flags_and_unhide_post @post.extract_quoted_post_numbers + @post_successfully_saved = @post.save(validate: @validate_post) @post.save_reply_relationships end diff --git a/lib/rate_limiter.rb b/lib/rate_limiter.rb index 4a9398271..b3e544912 100644 --- a/lib/rate_limiter.rb +++ b/lib/rate_limiter.rb @@ -4,7 +4,9 @@ require_dependency 'rate_limiter/on_create_record' # A redis backed rate limiter. class RateLimiter - KEY_PREFIX = "l-rate-limit:" + def self.key_prefix + "l-rate-limit:" + end def self.disable @disabled = true @@ -20,12 +22,12 @@ class RateLimiter end def self.clear_all! - $redis.keys("#{KEY_PREFIX}:*").each { |k| $redis.del(k) } + $redis.delete_prefixed(RateLimiter.key_prefix) end def initialize(user, key, max, secs) @user = user - @key = "#{KEY_PREFIX}:#{@user && @user.id}:#{key}" + @key = "#{RateLimiter.key_prefix}:#{@user && @user.id}:#{key}" @max = max @secs = secs end diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index fde755e45..0c16f0f17 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -39,6 +39,24 @@ describe PostRevisor do 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 let(:post) { Fabricate(:post, post_args) } let(:first_version_at) { post.last_version_at }