DEV: adds event hook when add/remove user to group (#8038)

DEV: adds event hook when add/remove user to group (#8038)

diff --git a/app/jobs/regular/automatic_group_membership.rb b/app/jobs/regular/automatic_group_membership.rb
index 9335b6e..8695737 100644
--- a/app/jobs/regular/automatic_group_membership.rb
+++ b/app/jobs/regular/automatic_group_membership.rb
@@ -22,7 +22,7 @@ module Jobs
         .where(staged: false)
         .find_each do |user|
         next unless user.email_confirmed?
-        group.add(user)
+        group.add(user, automatic: true)
         GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user)
       end
 
diff --git a/app/models/group.rb b/app/models/group.rb
index a04cdef..c3f632a 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -613,7 +613,7 @@ class Group < ActiveRecord::Base
 
   PUBLISH_CATEGORIES_LIMIT = 10
 
-  def add(user, notify: false)
+  def add(user, notify: false, automatic: false)
     self.users.push(user) unless self.users.include?(user)
 
     if notify
@@ -635,12 +635,15 @@ class Group < ActiveRecord::Base
       Discourse.request_refresh!(user_ids: [user.id])
     end
 
+    DiscourseEvent.trigger(:user_added_to_group, user, self, automatic: automatic)
+
     self
   end
 
   def remove(user)
     self.group_users.where(user: user).each(&:destroy)
     user.update_columns(primary_group_id: nil) if user.primary_group_id == self.id
+    DiscourseEvent.trigger(:user_removed_from_group, user, self)
   end
 
   def add_owner(user)
diff --git a/spec/jobs/automatic_group_membership_spec.rb b/spec/jobs/automatic_group_membership_spec.rb
index cb1544d..697b684 100644
--- a/spec/jobs/automatic_group_membership_spec.rb
+++ b/spec/jobs/automatic_group_membership_spec.rb
@@ -23,7 +23,22 @@ describe Jobs::AutomaticGroupMembership do
 
     group = Fabricate(:group, automatic_membership_email_domains: "wat.com", automatic_membership_retroactive: true)
 
-    Jobs::AutomaticGroupMembership.new.execute(group_id: group.id)
+    begin
+      automatic = nil
+      called = false
+
+      DiscourseEvent.on(:user_added_to_group) do |_u, _g, options|
+        automatic = options[:automatic]
+        called = true
+      end
+
+      Jobs::AutomaticGroupMembership.new.execute(group_id: group.id)
+
+      expect(automatic).to eql(true)
+      expect(called).to eq(true)
+    ensure
+      DiscourseEvent.off(:user_added_to_group)
+    end
 
     group.reload
     expect(group.users.include?(user1)).to eq(false)
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index 4739357..d215a89 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -806,6 +806,11 @@ describe Group do
       user.update(primary_group: group)
       expect { group.remove(user) }.to change { user.reload.primary_group }.from(group).to(nil)
     end
+
+    it 'triggers a user_removed_from_group event' do
+      events = DiscourseEvent.track_events { group.remove(user) }.map { |e| e[:event_name] }
+      expect(events).to include(:user_removed_from_group)
+    end
   end
 
   describe '#add' do
@@ -839,6 +844,25 @@ describe Group do
       expect(notification.user_id).to eq(user.id)
     end
 
+    it 'triggers a user_added_to_group event' do
+      begin
+        automatic = nil
+        called = false
+
+        DiscourseEvent.on(:user_added_to_group) do |_u, _g, options|
+          automatic = options[:automatic]
+          called = true
+        end
+
+        group.add(user)
+
+        expect(automatic).to eql(false)
+        expect(called).to eq(true)
+      ensure
+        DiscourseEvent.off(:user_added_to_group)
+      end
+    end
+
     context 'when adding a user into a public group' do
       fab!(:category) { Fabricate(:category) }

GitHub sha: a2586996

1 Like