Skip to content

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

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

Roman Rizzi8b3d50713d07
diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb
index 809967bbd11..a6aeaf50989 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 73d7c62f690..956d39c1fbf 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