Skip to content

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

FIX: skip post webhook when topic is permanently destroyed (#38653)

Régis Hanol0771253035f4
diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
index caa24b698ee..f52270fc529 100644
--- a/app/models/web_hook.rb
+++ b/app/models/web_hook.rb
@@ -117,14 +117,17 @@ class WebHook < ActiveRecord::Base
def self.enqueue_post_hooks(event, post, payload = nil)
if active_web_hooks(event).exists? && post.present?
+ topic = post.topic || Topic.with_deleted.find_by(id: post.topic_id)
+ return if topic.nil?
+
payload ||= WebHook.generate_payload(:post, post)
WebHook.enqueue_hooks(
:post,
event,
id: post.id,
- category_id: post.topic&.category_id,
- tag_ids: post.topic&.tags&.pluck(:id),
+ category_id: topic.category_id,
+ tag_ids: topic.tags.pluck(:id),
payload: payload,
)
end
diff --git a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb
index 49d7d508b8e..cc19af215df 100644
--- a/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb
+++ b/plugins/discourse-solved/lib/discourse_solved/guardian_extensions.rb
@@ -48,7 +48,7 @@ module DiscourseSolved
end
def can_unaccept_answer?(topic, post)
- can_accept_answer?(topic, post) || (is_staff? && topic.solved.present?)
+ can_accept_answer?(topic, post) || (is_staff? && topic&.solved.present?)
end
end
end
diff --git a/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb b/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb
index f2088da4a65..960e2b4864b 100644
--- a/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb
+++ b/plugins/discourse-solved/spec/lib/guardian_extensions_spec.rb
@@ -10,6 +10,13 @@ describe DiscourseSolved::GuardianExtensions do
before { SiteSetting.allow_solved_on_all_topics = true }
+ describe ".can_unaccept_answer?" do
+ it "returns false when topic is nil" do
+ admin = Fabricate(:admin, refresh_auto_groups: true)
+ expect(Guardian.new(admin).can_unaccept_answer?(nil, post)).to be_falsey
+ end
+ end
+
describe ".can_accept_answer?" do
it "returns false for anon users" do
expect(Guardian.new.can_accept_answer?(topic, post)).to eq(false)
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
index a5a87b9230a..a5896bd3bb8 100644
--- a/spec/models/web_hook_spec.rb
+++ b/spec/models/web_hook_spec.rb
@@ -365,6 +365,21 @@ RSpec.describe WebHook do
expect(payload["id"]).to eq(post.topic.id)
end
+ it "should not enqueue post hooks when the topic is permanently destroyed" do
+ Fabricate(:web_hook)
+
+ Jobs::EmitWebHookEvent.jobs.clear
+
+ post.topic.destroy!
+ post.reload
+
+ WebHook.enqueue_post_hooks(:post_recovered, post)
+
+ expect(
+ Jobs::EmitWebHookEvent.jobs.count { |j| j["args"].first["event_name"] == "post_recovered" },
+ ).to eq(0)
+ end
+
it "should serialize the right topic posts counts when a post is deleted" do
Fabricate(:web_hook)