From f5ec32bc8c6e55168df30017e04ac5c4b0dce984 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 22 Feb 2022 10:57:18 +0100 Subject: [PATCH] FEATURE: adds the user_promoted event to webhooks (#15996) --- app/models/web_hook_event_type.rb | 1 + config/initializers/012-web_hook_events.rb | 9 +++++++++ config/locales/client.en.yml | 3 +++ db/fixtures/007_web_hook_event_types.rb | 5 +++++ spec/fabricators/web_hook_fabricator.rb | 8 ++++++++ spec/models/web_hook_spec.rb | 20 ++++++++++++++++++++ 6 files changed, 46 insertions(+) diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rb index 70f2144f8a4..a8852aeeb97 100644 --- a/app/models/web_hook_event_type.rb +++ b/app/models/web_hook_event_type.rb @@ -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 diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb index a6aeaf50989..fa08cd3aea0 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -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) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 7e58eac33a6..5ef7ed8fd6b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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." diff --git a/db/fixtures/007_web_hook_event_types.rb b/db/fixtures/007_web_hook_event_types.rb index 42ed0e861b5..7990d1b16ea 100644 --- a/db/fixtures/007_web_hook_event_types.rb +++ b/db/fixtures/007_web_hook_event_types.rb @@ -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 diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb index 554e162fd1c..3e490b3f4cb 100644 --- a/spec/fabricators/web_hook_fabricator.rb +++ b/spec/fabricators/web_hook_fabricator.rb @@ -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 diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 956d39c1fbf..28b81534878 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -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 'shouldn’t 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) }