From c463cf63d4a40375519af56dd01d22654c149063 Mon Sep 17 00:00:00 2001 From: Erick Guan Date: Tue, 13 Sep 2016 16:03:17 +0800 Subject: [PATCH] FEATURE: Webhook for user creation and approval --- app/models/user.rb | 13 ++++++++++++- app/models/web_hook.rb | 6 ++++++ app/models/web_hook_event_type.rb | 1 + config/locales/client.en.yml | 7 ++----- db/fixtures/007_web_hook_event_types.rb | 5 +++++ spec/components/post_creator_spec.rb | 2 ++ spec/controllers/user_badges_controller_spec.rb | 3 ++- spec/models/user_spec.rb | 13 ++++++++++++- spec/models/web_hook_spec.rb | 17 +++++++++++++++-- 9 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index d53b52b03..aacae0c97 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -87,6 +87,7 @@ class User < ActiveRecord::Base after_create :ensure_in_trust_level_group after_create :automatic_group_membership after_create :set_default_categories_preferences + after_create :trigger_user_created_event before_save :update_username_lower before_save :ensure_password_is_hashed @@ -266,7 +267,12 @@ class User < ActiveRecord::Base self.approved_at = Time.now - send_approval_email if save and send_mail + if result = save + send_approval_email if send_mail + DiscourseEvent.trigger(:user_approved, self) + end + + result end def self.email_hash(email) @@ -1007,6 +1013,11 @@ class User < ActiveRecord::Base end end + def trigger_user_created_event + DiscourseEvent.trigger(:user_created, self) + true + end + private def previous_visit_at_update_required?(timestamp) diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index 823df4233..a609a6b16 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -68,6 +68,12 @@ class WebHook < ActiveRecord::Base ) end end + + %i(user_created user_approved).each do |event| + DiscourseEvent.on(event) do |user| + WebHook.enqueue_hooks(:user, user_id: user.id, event_name: event.to_s) + end + end end # == Schema Information diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rb index 139dd912f..d3596bfda 100644 --- a/app/models/web_hook_event_type.rb +++ b/app/models/web_hook_event_type.rb @@ -1,6 +1,7 @@ class WebHookEventType < ActiveRecord::Base TOPIC = 1 POST = 2 + USER = 3 has_and_belongs_to_many :web_hooks diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 48a00a3d9..76de26a63 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2467,12 +2467,9 @@ en: post_event: name: "Post Event" details: "When there is a new reply, edit, deleted or recovered." - invitation_event: - name: "Invitation Event" - details: "When a invitation is sent or accepted." user_event: - name: "User Event" - details: "When there is a user is created or changed." + name: "User Creation Event" + details: "When a user is created or approved." delivery_status: title: "Delivery Status" inactive: "Inactive" diff --git a/db/fixtures/007_web_hook_event_types.rb b/db/fixtures/007_web_hook_event_types.rb index afa74b88d..d91b5f09a 100644 --- a/db/fixtures/007_web_hook_event_types.rb +++ b/db/fixtures/007_web_hook_event_types.rb @@ -7,3 +7,8 @@ WebHookEventType.seed do |b| b.id = WebHookEventType::POST b.name = "post" end + +WebHookEventType.seed do |b| + b.id = WebHookEventType::USER + b.name = "user" +end diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 37427b60b..54efd31cc 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -64,6 +64,7 @@ describe PostCreator do end context "success" do + before { creator } it "doesn't return true for spam" do creator.create @@ -71,6 +72,7 @@ describe PostCreator do end it "triggers extensibility events" do + creator # bypass a user_created event, can be removed when there is a UserCreator DiscourseEvent.expects(:trigger).with(:before_create_post, anything).once DiscourseEvent.expects(:trigger).with(:validate_post, anything).once DiscourseEvent.expects(:trigger).with(:topic_created, anything, anything, user).once diff --git a/spec/controllers/user_badges_controller_spec.rb b/spec/controllers/user_badges_controller_spec.rb index 8047d8571..7e2cda7f0 100644 --- a/spec/controllers/user_badges_controller_spec.rb +++ b/spec/controllers/user_badges_controller_spec.rb @@ -102,7 +102,7 @@ describe UserBadgesController do it 'will trigger :user_badge_granted' do log_in :admin - + user DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once xhr :post, :create, badge_id: badge.id, username: user.username end @@ -126,6 +126,7 @@ describe UserBadgesController do it 'will trigger :user_badge_removed' do log_in :admin + DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once xhr :delete, :destroy, id: user_badge.id end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2eae85e49..2552c11f4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -58,6 +58,12 @@ describe User do user.approve(admin) end + it 'triggers a extensibility event' do + user && admin # bypass the user_created event + DiscourseEvent.expects(:trigger).with(:user_approved, user).once + user.approve(admin) + end + context 'after approval' do before do user.approve(admin) @@ -153,8 +159,13 @@ describe User do expect(subject.approved_by_id).to be_blank end + it 'triggers an extensibility event' do + DiscourseEvent.expects(:trigger).with(:user_created, subject).once + subject.save! + end + context 'after_save' do - before { subject.save } + before { subject.save! } it "has correct settings" do expect(subject.email_tokens).to be_present diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 4d5586c90..88eaee6cb 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -103,9 +103,10 @@ describe WebHook do let!(:post_hook) { Fabricate(:web_hook) } let!(:topic_hook) { Fabricate(:topic_web_hook) } let(:user) { Fabricate(:user) } + let(:admin) { Fabricate(:admin) } let(:topic) { Fabricate(:topic, user: user) } - let(:post) { Fabricate(:post, topic: topic) } - let(:post2) { Fabricate(:post, topic: topic) } + let(:post) { Fabricate(:post, topic: topic, user: user) } + let(:post2) { Fabricate(:post, topic: topic, user: user) } it 'should enqueue the right hooks for topic events' do WebHook.expects(:enqueue_topic_hooks).once @@ -119,6 +120,7 @@ describe WebHook do end it 'should enqueue the right hooks for post events' do + user # bypass a user_created event WebHook.expects(:enqueue_hooks).once PostCreator.create(user, { raw: 'post', topic_id: topic.id, reply_to_post_number: 1, skip_validations: true }) @@ -128,5 +130,16 @@ describe WebHook do WebHook.expects(:enqueue_hooks).once PostDestroyer.new(user, post2).recover end + + it 'should enqueue the right hooks for user creation events' do + WebHook.expects(:enqueue_hooks).once + user + + WebHook.expects(:enqueue_hooks).once + admin + + WebHook.expects(:enqueue_hooks).once + user.approve(admin) + end end end