mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 20:36:39 +08:00
DEV: adds event hook when add/remove user to group (#8038)
This commit is contained in:
parent
1cebe7670a
commit
a25869969a
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) }
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user