Skip to content

A platform for community discussion. Free, open, simple.

FIX: don't trigger `topic_created` event for reply posts via email. (#15485)

Vinoth Kannanf614b3003252
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index bc6a41af21d..1d181d66a2c 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -1308,7 +1308,7 @@ module Email
import_mode: options[:import_mode],
post_alert_options: options[:post_alert_options]
).enqueue_jobs
- DiscourseEvent.trigger(:topic_created, result.post.topic, options, user)
+ DiscourseEvent.trigger(:topic_created, result.post.topic, options, user) if result.post.is_first_post?
DiscourseEvent.trigger(:post_created, result.post, options, user)
end
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index 235a098ab6a..bf741cf76a8 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -340,7 +340,12 @@ describe Email::Receiver do
expect { process(:like) }.to raise_error(Email::Receiver::InvalidPostAction)
end
- it "works" do
+ it "creates a new reply post" do
+ handler_calls = 0
+ handler = proc { |_| handler_calls += 1 }
+
+ DiscourseEvent.on(:topic_created, &handler)
+
expect { process(:text_reply) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("This is a text reply :)\n\nEmail parsing should not break because of a UTF-8 character: ’")
expect(topic.posts.last.via_email).to eq(true)
@@ -348,6 +353,9 @@ describe Email::Receiver do
expect { process(:html_reply) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("This is a **HTML** reply ;)")
+
+ DiscourseEvent.off(:topic_created, &handler)
+ expect(handler_calls).to eq(0)
end
it "stores the created_via source against the incoming email" do