FEATURE: adds the user_promoted event to webhooks (#15996)

This commit is contained in:
Joffrey JAFFEUX 2022-02-22 10:57:18 +01:00 committed by GitHub
parent 316206a991
commit f5ec32bc8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 0 deletions

View File

@ -14,6 +14,7 @@ class WebHookEventType < ActiveRecord::Base
USER_BADGE = 13
GROUP_USER = 14
LIKE = 15
USER_PROMOTED = 16
has_and_belongs_to_many :web_hooks

View File

@ -114,6 +114,15 @@ DiscourseEvent.on(:user_added_to_group) do |user, group, options|
WebHook.enqueue_object_hooks(:group_user, group_user, :user_added_to_group, WebHookGroupUserSerializer)
end
DiscourseEvent.on(:user_promoted) do |payload|
user_id, new_trust_level, old_trust_level = payload.values_at(:user_id, :new_trust_level, :old_trust_level)
next if new_trust_level < old_trust_level
user = User.find(user_id)
WebHook.enqueue_object_hooks(:user_promoted, user, :user_promoted, UserSerializer)
end
DiscourseEvent.on(:like_created) do |post_action|
user = post_action.user
group_ids = user.groups.map(&:id)

View File

@ -4300,6 +4300,9 @@ en:
notification_event:
name: "Notification Event"
details: "When a user receives a notification in their feed."
user_promoted_event:
name: "User Promoted Event"
details: "When a user is promoted from one trust level to another."
user_badge_event:
name: "Badge Grant Event"
details: "When a user receives a badge."

View File

@ -64,3 +64,8 @@ WebHookEventType.seed do |b|
b.id = WebHookEventType::LIKE
b.name = "like"
end
WebHookEventType.seed do |b|
b.id = WebHookEventType::USER_PROMOTED
b.name = "user_promoted"
end

View File

@ -110,3 +110,11 @@ Fabricator(:like_web_hook, from: :web_hook) do
web_hook.web_hook_event_types = [transients[:like_hook]]
end
end
Fabricator(:user_promoted_web_hook, from: :web_hook) do
transient user_promoted_hook: WebHookEventType.find_by(name: 'user_promoted')
after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:user_promoted_hook]]
end
end

View File

@ -574,6 +574,26 @@ describe WebHook do
expect(payload["user_id"]).to eq(user.id)
end
context 'user promoted hooks' do
fab!(:user_promoted_web_hook) { Fabricate(:user_promoted_web_hook) }
fab!(:another_user) { Fabricate(:user, trust_level: 2) }
it 'should pass the user to the webhook job when a user is promoted' do
another_user.change_trust_level!(another_user.trust_level + 1)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_promoted")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(another_user.id)
end
it 'shouldnt trigger when the user is demoted' do
expect {
another_user.change_trust_level!(another_user.trust_level - 1)
}.to change { Jobs::EmitWebHookEvent.jobs.length }.by(0)
end
end
context 'like created hooks' do
fab!(:like_web_hook) { Fabricate(:like_web_hook) }
fab!(:another_user) { Fabricate(:user) }