FIX: post & topic destroyed hooks not triggering with tag filter

FIX: post & topic destroyed hooks not triggering with tag filter

diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
index b2fe649..5d46756 100644
--- a/app/models/web_hook.rb
+++ b/app/models/web_hook.rb
@@ -65,10 +65,12 @@ class WebHook < ActiveRecord::Base
     end
   end
 
-  def self.enqueue_topic_hooks(event, topic)
+  def self.enqueue_topic_hooks(event, topic, payload = nil)
     if active_web_hooks('topic').exists? && topic.present?
-      topic_view = TopicView.new(topic.id, Discourse.system_user)
-      payload = WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer)
+      payload ||= begin
+        topic_view = TopicView.new(topic.id, Discourse.system_user)
+        WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer)
+      end
 
       WebHook.enqueue_hooks(:topic, event,
         id: topic.id,
@@ -79,9 +81,9 @@ class WebHook < ActiveRecord::Base
     end
   end
 
-  def self.enqueue_post_hooks(event, post)
+  def self.enqueue_post_hooks(event, post, payload = nil)
     if active_web_hooks('post').exists? && post.present?
-      payload = WebHook.generate_payload(:post, post)
+      payload ||= WebHook.generate_payload(:post, post)
 
       WebHook.enqueue_hooks(:post, event,
         id: post.id,
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index 54e4451..16e91f4 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -61,19 +61,11 @@ class PostDestroyer
       mark_for_deletion(delete_removed_posts_after)
     end
     DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user)
-    WebHook.enqueue_hooks(:post, :post_destroyed,
-      id: @post.id,
-      category_id: @post&.topic&.category_id,
-      payload: payload
-    ) if WebHook.active_web_hooks(:post).exists?
+    WebHook.enqueue_post_hooks(:post_destroyed, @post, payload)
 
     if @post.is_first_post? && @post.topic
       DiscourseEvent.trigger(:topic_destroyed, @post.topic, @user)
-      WebHook.enqueue_hooks(:topic, :topic_destroyed,
-        id: topic.id,
-        category_id: topic&.category_id,
-        payload: topic_payload
-      ) if WebHook.active_web_hooks(:topic).exists?
+      WebHook.enqueue_topic_hooks(:topic_destroyed, @post.topic, topic_payload)
     end
   end
 
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
index 89beb70..3156c35 100644
--- a/spec/models/web_hook_spec.rb
+++ b/spec/models/web_hook_spec.rb
@@ -257,6 +257,33 @@ describe WebHook do
       expect(payload["id"]).to eq(post.topic.id)
     end
 
+    it 'should enqueue the destroyed hooks with tag filter for post events' do
+      tag = Fabricate(:tag)
+      Fabricate(:web_hook, tags: [tag])
+
+      post = PostCreator.create!(user,
+        raw: 'post',
+        topic_id: topic.id,
+        reply_to_post_number: 1,
+        skip_validations: true
+      )
+
+      topic.tags = [tag]
+      topic.save!
+
+      Jobs::EmitWebHookEvent.jobs.clear
+      PostDestroyer.new(user, post).destroy
+
+      job = Jobs::EmitWebHookEvent.new
+      job.expects(:web_hook_request).times(2)
+
+      args = Jobs::EmitWebHookEvent.jobs[1]["args"].first
+      job.execute(args.with_indifferent_access)
+
+      args = Jobs::EmitWebHookEvent.jobs[2]["args"].first
+      job.execute(args.with_indifferent_access)
+    end
+
     it 'should enqueue the right hooks for user events' do
       Fabricate(:user_web_hook, active: true)

GitHub sha: 167d85c2

This commit has been mentioned on Discourse Meta. There might be relevant details there: