FIX: Pass category and tag IDs to the emit webhook event job. (#15568)

FIX: Pass category and tag IDs to the emit webhook event job. (#15568)

  • FIX: Pass category and tag IDs to the emit webhook event job.

Like webhooks won’t fire when they’re scoped to specific categories or tags because we’re not passing the data to the job that emits it.

  • Update config/initializers/012-web_hook_events.rb

Co-authored-by: Dan Ungureanu dan@ungureanu.me

Co-authored-by: Dan Ungureanu dan@ungureanu.me

diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb
index 809967b..a6aeaf5 100644
--- a/config/initializers/012-web_hook_events.rb
+++ b/config/initializers/012-web_hook_events.rb
@@ -117,5 +117,12 @@ end
 DiscourseEvent.on(:like_created) do |post_action|
   user = post_action.user
   group_ids = user.groups.map(&:id)
-  WebHook.enqueue_object_hooks(:like, post_action, :post_liked, WebHookLikeSerializer, group_ids: group_ids)
+  topic = Topic.includes(:tags).joins(:posts).find_by(posts: { id: post_action.post_id })
+  category_id = topic&.category_id
+  tag_ids = topic&.tag_ids
+
+  WebHook.enqueue_object_hooks(:like,
+    post_action, :post_liked, WebHookLikeSerializer,
+    group_ids: group_ids, category_id: category_id, tag_ids: tag_ids
+  )
 end
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
index 73d7c62..956d39c 100644
--- a/spec/models/web_hook_spec.rb
+++ b/spec/models/web_hook_spec.rb
@@ -574,24 +574,54 @@ describe WebHook do
       expect(payload["user_id"]).to eq(user.id)
     end
 
-    it 'should enqueue hooks for user likes in a group' do
-      group = Fabricate(:group)
-      Fabricate(:like_web_hook, groups: [group])
-      group_user = Fabricate(:group_user, group: group, user: user)
-      poster = Fabricate(:user)
-      post = Fabricate(:post, user: poster)
-      like = Fabricate(:post_action, post: post, user: user, post_action_type_id: PostActionType.types[:like])
-      now = Time.now
-      freeze_time now
+    context 'like created hooks' do
+      fab!(:like_web_hook) { Fabricate(:like_web_hook) }
+      fab!(:another_user) { Fabricate(:user) }
 
-      DiscourseEvent.trigger(:like_created, like)
+      it 'should pass the group id to the emit webhook job' do
+        group = Fabricate(:group)
+        group_user = Fabricate(:group_user, group: group, user: user)
+        post = Fabricate(:post, user: another_user)
+        like = Fabricate(:post_action, post: post, user: user, post_action_type_id: PostActionType.types[:like])
+        now = Time.now
+        freeze_time now
 
-      job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
-      expect(job_args["event_name"]).to eq("post_liked")
-      expect(job_args["group_ids"]).to eq([group.id])
-      payload = JSON.parse(job_args["payload"])
-      expect(payload["post"]["id"]).to eq(post.id)
-      expect(payload["user"]["id"]).to eq(user.id)
+        DiscourseEvent.trigger(:like_created, like)
+
+        assert_hook_was_queued_with(post, user, group_ids: [group.id])
+      end
+
+      it 'should pass the category id to the emit webhook job' do
+        category = Fabricate(:category)
+        topic.update!(category: category)
+        like = Fabricate(:post_action, post: post, user: another_user, post_action_type_id: PostActionType.types[:like])
+
+        DiscourseEvent.trigger(:like_created, like)
+
+        assert_hook_was_queued_with(post, another_user, category_id: category.id)
+      end
+
+      it 'should pass the tag id to the emit webhook job' do
+        tag = Fabricate(:tag)
+        topic.update!(tags: [tag])
+        like = Fabricate(:post_action, post: post, user: another_user, post_action_type_id: PostActionType.types[:like])
+
+        DiscourseEvent.trigger(:like_created, like)
+
+        assert_hook_was_queued_with(post, another_user, tag_ids: [tag.id])
+      end
+
+      def assert_hook_was_queued_with(post, user, group_ids: nil, category_id: nil, tag_ids: nil)
+        job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
+        expect(job_args["event_name"]).to eq("post_liked")
+        payload = JSON.parse(job_args["payload"])
+        expect(payload["post"]["id"]).to eq(post.id)
+        expect(payload["user"]["id"]).to eq(user.id)
+
+        expect(job_args["category_id"]).to eq(category_id) if category_id
+        expect(job_args["group_ids"]).to contain_exactly(*group_ids) if group_ids
+        expect(job_args["tag_ids"]).to contain_exactly(*tag_ids) if tag_ids
+      end
     end
   end
 end

GitHub sha: 8b3d50713d0751713bd7a3d07a7ec9cb234968bc

This commit appears in #15568 which was approved by udan11 and ZogStriP. It was merged by romanrizzi.