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:
Sam 2022-06-29 15:35:07 +10:00 committed by GitHub
parent 644e05cd4d
commit 6ecfdc8f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View File

@ -248,7 +248,7 @@ class Post < ActiveRecord::Base
# The key we use in redis to ensure unique posts
def unique_post_key
"unique-post-#{user_id}:#{raw_hash}"
"unique#{topic&.private_message? ? "-pm" : ""}-post-#{user_id}:#{raw_hash}"
end
def store_unique_post_key

View File

@ -232,16 +232,22 @@ describe PostValidator do
describe "unique_post_validator" do
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
SiteSetting.unique_posts_mins = 5
post.store_unique_post_key
pm_post.store_unique_post_key
@key = post.unique_post_key
@pm_key = pm_post.unique_post_key
end
after do
Discourse.redis.del(@key)
Discourse.redis.del(@pm_key)
end
context "post is unique" do
@ -263,16 +269,44 @@ describe PostValidator do
end
context "post is not unique" do
let(:new_post) do
Fabricate.build(:post, user: user, raw: post.raw, topic: topic)
def build_post(is_pm:, raw:)
Fabricate.build(
:post,
user: user,
raw: raw,
topic: is_pm ? Fabricate.build(:private_message_topic) : topic
)
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)
expect(new_post.errors.to_hash.keys).to contain_exactly(:raw)
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
new_post = build_post(is_pm: false, raw: post.raw)
new_post.skip_unique_check = true
validator.unique_post_validator(new_post)
expect(new_post.errors.count).to eq(0)