diff --git a/app/models/user.rb b/app/models/user.rb index d53b52b03bb..aacae0c9701 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 823df4233bd..a609a6b16cd 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 139dd912f7f..d3596bfdaf5 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 48a00a3d9be..76de26a6372 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 afa74b88d37..d91b5f09a1a 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 37427b60b82..54efd31cc9b 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 8047d857154..7e2cda7f044 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 2eae85e49e6..2552c11f496 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 4d5586c9016..88eaee6cb0d 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