Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: ensures we don't attempt to create a new PM on an existing topic #9029

Merged
merged 3 commits into from Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/models/composer.js.es6
Expand Up @@ -912,7 +912,7 @@ const Composer = RestModel.extend({
},

createPost(opts) {
if (this.action === CREATE_TOPIC) {
if (CREATE_TOPIC === this.action || PRIVATE_MESSAGE === this.action) {
ZogStriP marked this conversation as resolved.
Show resolved Hide resolved
this.set("topic", null);
}

Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Expand Up @@ -339,6 +339,7 @@ en:
pm_reached_recipients_limit: "Sorry, you can't have more than %{recipients_limit} recipients in a message."
removed_direct_reply_full_quotes: "Automatically removed quote of whole previous post."
secure_upload_not_allowed_in_public_topic: "Sorry, the following secure upload(s) cannot be used in a public topic: %{upload_filenames}."
create_pm_on_existing_topic: "Sorry, you can't create a PM on an existing topic."

just_posted_that: "is too similar to what you recently posted"
invalid_characters: "contains invalid characters"
Expand Down
8 changes: 8 additions & 0 deletions lib/post_creator.rb
Expand Up @@ -136,6 +136,14 @@ def valid?
return false unless skip_validations? || validate_child(topic_creator)
else
@topic = Topic.find_by(id: @opts[:topic_id])

if @topic.present?
jjaffeux marked this conversation as resolved.
Show resolved Hide resolved
if @opts[:archetype] == Archetype.private_message
ZogStriP marked this conversation as resolved.
Show resolved Hide resolved
errors.add(:base, I18n.t(:create_pm_on_existing_topic))
return false
end
end

unless @topic.present? && (@opts[:skip_guardian] || guardian.can_create?(Post, @topic))
errors.add(:base, I18n.t(:topic_not_found))
return false
Expand Down
21 changes: 21 additions & 0 deletions spec/requests/posts_controller_spec.rb
Expand Up @@ -1126,6 +1126,27 @@
end
end

context "when topic_id is set" do
fab!(:topic) { Fabricate(:topic) }

it "errors when creating a private post" do
user_2 = Fabricate(:user)

post "/posts.json", params: {
raw: 'this is the test content',
archetype: 'private_message',
title: "this is some post",
target_recipients: user_2.username,
topic_id: topic.id
}

expect(response.status).to eq(422)
expect(JSON.parse(response.body)["errors"]).to include(
I18n.t("create_pm_on_existing_topic")
)
end
end

context "errors" do
it "does not succeed" do
post "/posts.json", params: { raw: 'test' }
Expand Down