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

This fix attempts to both fix it at UI level and server side. A previous attempt related to this behavior has been made in commit: 49c750ca78
This commit is contained in:
Joffrey JAFFEUX 2020-02-24 15:55:12 +01:00 committed by GitHub
parent 31f3ed8d36
commit 0ea11a9d49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 5 deletions

View File

@ -912,7 +912,7 @@ const Composer = RestModel.extend({
}, },
createPost(opts) { createPost(opts) {
if (this.action === CREATE_TOPIC) { if (CREATE_TOPIC === this.action || PRIVATE_MESSAGE === this.action) {
this.set("topic", null); this.set("topic", null);
} }

View File

@ -339,6 +339,7 @@ en:
pm_reached_recipients_limit: "Sorry, you can't have more than %{recipients_limit} recipients in a message." 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." 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}." 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" just_posted_that: "is too similar to what you recently posted"
invalid_characters: "contains invalid characters" invalid_characters: "contains invalid characters"

View File

@ -136,6 +136,12 @@ class PostCreator
return false unless skip_validations? || validate_child(topic_creator) return false unless skip_validations? || validate_child(topic_creator)
else else
@topic = Topic.find_by(id: @opts[:topic_id]) @topic = Topic.find_by(id: @opts[:topic_id])
if @topic.present? && @opts[:archetype] == Archetype.private_message
errors.add(:base, I18n.t(:create_pm_on_existing_topic))
return false
end
unless @topic.present? && (@opts[:skip_guardian] || guardian.can_create?(Post, @topic)) unless @topic.present? && (@opts[:skip_guardian] || guardian.can_create?(Post, @topic))
errors.add(:base, I18n.t(:topic_not_found)) errors.add(:base, I18n.t(:topic_not_found))
return false return false

View File

@ -176,12 +176,12 @@ module DiscourseNarrativeBot
if @post && if @post &&
@post.topic.private_message? && @post.topic.private_message? &&
@post.topic.topic_allowed_users.pluck(:user_id).include?(@user.id) @post.topic.topic_allowed_users.pluck(:user_id).include?(@user.id)
opts = opts.merge(topic_id: @post.topic_id)
end end
if @data[:topic_id] if @data[:topic_id]
opts = opts.merge(topic_id: @data[:topic_id]) opts = opts
.merge(topic_id: @data[:topic_id])
.except(:title, :target_usernames, :archetype)
end end
post = reply_to(@post, raw, opts) post = reply_to(@post, raw, opts)

View File

@ -208,7 +208,9 @@ module DiscourseNarrativeBot
end end
if @data[:topic_id] if @data[:topic_id]
opts = opts.merge(topic_id: @data[:topic_id]) opts = opts
.merge(topic_id: @data[:topic_id])
.except(:title, :target_usernames, :archetype)
end end
post = reply_to(@post, raw, opts) post = reply_to(@post, raw, opts)

View File

@ -1126,6 +1126,27 @@ describe PostsController do
end end
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 context "errors" do
it "does not succeed" do it "does not succeed" do
post "/posts.json", params: { raw: 'test' } post "/posts.json", params: { raw: 'test' }