FIX: keep unique post checks separate for PMs vs topics (#17272)
This allows for people to use PMs for drafting and then post them on topics. Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
parent
644e05cd4d
commit
6ecfdc8f55
|
@ -248,7 +248,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
|
||||||
"unique-post-#{user_id}:#{raw_hash}"
|
"unique#{topic&.private_message? ? "-pm" : ""}-post-#{user_id}:#{raw_hash}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_unique_post_key
|
def store_unique_post_key
|
||||||
|
|
|
@ -232,16 +232,22 @@ describe PostValidator do
|
||||||
|
|
||||||
describe "unique_post_validator" do
|
describe "unique_post_validator" do
|
||||||
fab!(:user) { Fabricate(:user) }
|
fab!(:user) { Fabricate(:user) }
|
||||||
fab!(:post) { Fabricate(:post, user: user, topic: topic) }
|
fab!(:post) { Fabricate(:post, raw: "Non PM topic body", user: user, topic: topic) }
|
||||||
|
fab!(:pm_post) { Fabricate(:post, raw: "PM topic body", user: user, topic: Fabricate(:private_message_topic)) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
SiteSetting.unique_posts_mins = 5
|
SiteSetting.unique_posts_mins = 5
|
||||||
|
|
||||||
post.store_unique_post_key
|
post.store_unique_post_key
|
||||||
|
pm_post.store_unique_post_key
|
||||||
|
|
||||||
@key = post.unique_post_key
|
@key = post.unique_post_key
|
||||||
|
@pm_key = pm_post.unique_post_key
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
Discourse.redis.del(@key)
|
Discourse.redis.del(@key)
|
||||||
|
Discourse.redis.del(@pm_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "post is unique" do
|
context "post is unique" do
|
||||||
|
@ -263,16 +269,44 @@ describe PostValidator do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "post is not unique" do
|
context "post is not unique" do
|
||||||
let(:new_post) do
|
def build_post(is_pm:, raw:)
|
||||||
Fabricate.build(:post, user: user, raw: post.raw, topic: topic)
|
Fabricate.build(
|
||||||
|
:post,
|
||||||
|
user: user,
|
||||||
|
raw: raw,
|
||||||
|
topic: is_pm ? Fabricate.build(:private_message_topic) : topic
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add an error" do
|
it "should add an error for post dupes" do
|
||||||
|
new_post = build_post(is_pm: false, raw: post.raw)
|
||||||
|
|
||||||
validator.unique_post_validator(new_post)
|
validator.unique_post_validator(new_post)
|
||||||
expect(new_post.errors.to_hash.keys).to contain_exactly(:raw)
|
expect(new_post.errors.to_hash.keys).to contain_exactly(:raw)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should add an error for pm dupes" do
|
||||||
|
new_post = build_post(is_pm: true, raw: pm_post.raw)
|
||||||
|
|
||||||
|
validator.unique_post_validator(new_post)
|
||||||
|
expect(new_post.errors.to_hash.keys).to contain_exactly(:raw)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not add an error for cross PM / topic dupes" do
|
||||||
|
new_post = build_post(is_pm: true, raw: post.raw)
|
||||||
|
|
||||||
|
validator.unique_post_validator(new_post)
|
||||||
|
expect(new_post.errors.count).to eq(0)
|
||||||
|
|
||||||
|
new_post = build_post(is_pm: false, raw: pm_post.raw)
|
||||||
|
|
||||||
|
validator.unique_post_validator(new_post)
|
||||||
|
expect(new_post.errors.count).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
it "should not add an error if post.skip_unique_check is true" do
|
it "should not add an error if post.skip_unique_check is true" do
|
||||||
|
new_post = build_post(is_pm: false, raw: post.raw)
|
||||||
|
|
||||||
new_post.skip_unique_check = true
|
new_post.skip_unique_check = true
|
||||||
validator.unique_post_validator(new_post)
|
validator.unique_post_validator(new_post)
|
||||||
expect(new_post.errors.count).to eq(0)
|
expect(new_post.errors.count).to eq(0)
|
||||||
|
|
Loading…
Reference in New Issue