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>
This commit is contained in:
Roman Rizzi 2022-01-14 11:17:38 -03:00 committed by GitHub
parent f56eff2303
commit 8b3d50713d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 17 deletions

View File

@ -117,5 +117,12 @@ end
DiscourseEvent.on(:like_created) do |post_action| DiscourseEvent.on(:like_created) do |post_action|
user = post_action.user user = post_action.user
group_ids = user.groups.map(&:id) 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 end

View File

@ -574,24 +574,54 @@ describe WebHook do
expect(payload["user_id"]).to eq(user.id) expect(payload["user_id"]).to eq(user.id)
end end
it 'should enqueue hooks for user likes in a group' do context 'like created hooks' do
group = Fabricate(:group) fab!(:like_web_hook) { Fabricate(:like_web_hook) }
Fabricate(:like_web_hook, groups: [group]) fab!(:another_user) { Fabricate(:user) }
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
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 DiscourseEvent.trigger(:like_created, like)
expect(job_args["event_name"]).to eq("post_liked")
expect(job_args["group_ids"]).to eq([group.id]) assert_hook_was_queued_with(post, user, group_ids: [group.id])
payload = JSON.parse(job_args["payload"]) end
expect(payload["post"]["id"]).to eq(post.id)
expect(payload["user"]["id"]).to eq(user.id) 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 end
end end