discourse/app/jobs/regular/automatic_group_membership.rb
Krzysztof Kotlarek 427d54b2b0 DEV: Upgrading Discourse to Zeitwerk (#8098)
Zeitwerk simplifies working with dependencies in dev and makes it easier reloading class chains. 

We no longer need to use Rails "require_dependency" anywhere and instead can just use standard 
Ruby patterns to require files.

This is a far reaching change and we expect some followups here.
2019-10-02 14:01:53 +10:00

35 lines
998 B
Ruby

# frozen_string_literal: true
module Jobs
class AutomaticGroupMembership < ::Jobs::Base
def execute(args)
group_id = args[:group_id]
raise Discourse::InvalidParameters.new(:group_id) if group_id.blank?
group = Group.find_by(id: group_id)
raise Discourse::InvalidParameters.new(:group_id) if group.nil?
return unless group.automatic_membership_retroactive
domains = group.automatic_membership_email_domains.gsub('.', '\.')
User.joins(:user_emails)
.where("user_emails.email ~* '@(#{domains})$'")
.where("users.id NOT IN (SELECT user_id FROM group_users WHERE group_users.group_id = ?)", group_id)
.activated
.where(staged: false)
.find_each do |user|
next unless user.email_confirmed?
group.add(user, automatic: true)
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user)
end
Group.reset_counters(group.id, :group_users)
end
end
end