FEATURE: trigger `user_updated` event if email changed.

FEATURE: trigger user_updated event if email changed.

It will trigger user_updated Discourse event if a user_email record is created or updated or destroyed.

diff --git a/app/models/user_email.rb b/app/models/user_email.rb
index a3e76f2..6c6b078 100644
--- a/app/models/user_email.rb
+++ b/app/models/user_email.rb
@@ -19,6 +19,10 @@ class UserEmail < ActiveRecord::Base
 
   scope :secondary, -> { where(primary: false) }
 
+  after_commit(on: [:create, :update, :destroy]) do
+    DiscourseEvent.trigger(:user_updated, user)
+  end
+
   private
 
   def strip_downcase_email
diff --git a/spec/models/user_email_spec.rb b/spec/models/user_email_spec.rb
index ebd08de..ebbc05b 100644
--- a/spec/models/user_email_spec.rb
+++ b/spec/models/user_email_spec.rb
@@ -13,9 +13,17 @@ describe UserEmail do
     end
 
     it "allows multiple secondary emails" do
-      Fabricate(:secondary_email, user: user, primary: false)
-      Fabricate(:secondary_email, user: user, primary: false)
+      events = DiscourseEvent.track_events {
+        Fabricate(:secondary_email, user: user, primary: false)
+        Fabricate(:secondary_email, user: user, primary: false)
+      }
+
       expect(user.user_emails.count).to eq 3
+      expect(events.count).to eq 2
+
+      event = events.first
+      expect(event[:event_name]).to eq(:user_updated)
+      expect(event[:params].first).to eq(user)
     end
 
     it "does not allow an invalid email" do
diff --git a/spec/requests/users_email_controller_spec.rb b/spec/requests/users_email_controller_spec.rb
index 06cc9ce..372b10c 100644
--- a/spec/requests/users_email_controller_spec.rb
+++ b/spec/requests/users_email_controller_spec.rb
@@ -63,9 +63,14 @@ describe UsersEmailController do
       it 'confirms with a correct token' do
         user.user_stat.update_columns(bounce_score: 42, reset_bounce_score_after: 1.week.from_now)
 
-        put "/u/confirm-new-email", params: {
-          token: "#{user.email_tokens.last.token}"
-        }
+        event = DiscourseEvent.track_events {
+          put "/u/confirm-new-email", params: {
+            token: "#{user.email_tokens.last.token}"
+          }
+        }.last
+
+        expect(event[:event_name]).to eq(:user_updated)
+        expect(event[:params].first).to eq(user)
 
         expect(response.status).to eq(302)
         expect(response.redirect_url).to include("done")

GitHub sha: 1460d795

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/webhook-doesnt-send-anything-when-email-username-updated/132623/9