FIX: ensure group's users counters are kept in sync
This commit is contained in:
parent
d677f852c4
commit
79639e2dec
|
@ -66,6 +66,7 @@ class Admin::GroupsController < Admin::AdminController
|
||||||
group.title = group.automatic ? nil : title
|
group.title = group.automatic ? nil : title
|
||||||
|
|
||||||
if group.save
|
if group.save
|
||||||
|
Group.reset_counters(group.id, :group_users)
|
||||||
render_serialized(group, BasicGroupSerializer)
|
render_serialized(group, BasicGroupSerializer)
|
||||||
else
|
else
|
||||||
render_json_error group
|
render_json_error group
|
||||||
|
@ -101,6 +102,8 @@ class Admin::GroupsController < Admin::AdminController
|
||||||
group.group_users.where(user_id: user.id).update_all(owner: true)
|
group.group_users.where(user_id: user.id).update_all(owner: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Group.reset_counters(group.id, :group_users)
|
||||||
|
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -111,6 +114,8 @@ class Admin::GroupsController < Admin::AdminController
|
||||||
user = User.find(params[:user_id].to_i)
|
user = User.find(params[:user_id].to_i)
|
||||||
group.group_users.where(user_id: user.id).update_all(owner: false)
|
group.group_users.where(user_id: user.id).update_all(owner: false)
|
||||||
|
|
||||||
|
Group.reset_counters(group.id, :group_users)
|
||||||
|
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,15 +13,17 @@ module Jobs
|
||||||
|
|
||||||
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
||||||
|
|
||||||
User.where("email ~* '@(#{domains})$' and users.id not in (
|
User.where("email ~* '@(#{domains})$'")
|
||||||
select user_id from group_users where group_users.group_id = ?
|
.where("users.id NOT IN (SELECT user_id FROM group_users WHERE group_users.group_id = ?)", group_id)
|
||||||
)", group_id).find_each do |user|
|
.find_each do |user|
|
||||||
begin
|
begin
|
||||||
group.add(user)
|
group.add(user)
|
||||||
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
|
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
|
||||||
# we don't care about this
|
# we don't care about this
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Group.reset_counters(group.id, :group_users)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Jobs
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
UserVisit.ensure_consistency!
|
UserVisit.ensure_consistency!
|
||||||
Group.refresh_automatic_groups!
|
Group.ensure_consistency!
|
||||||
Notification.ensure_consistency!
|
Notification.ensure_consistency!
|
||||||
UserAction.ensure_consistency!
|
UserAction.ensure_consistency!
|
||||||
TopicFeaturedUsers.ensure_consistency!
|
TopicFeaturedUsers.ensure_consistency!
|
||||||
|
|
|
@ -217,6 +217,17 @@ class Group < ActiveRecord::Base
|
||||||
group
|
group
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.ensure_consistency!
|
||||||
|
reset_all_counters
|
||||||
|
refresh_automatic_groups
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset_all_counters
|
||||||
|
Group.pluck(:id).each do |group_id|
|
||||||
|
Group.reset_counters(group_id, :group_users)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.refresh_automatic_groups!(*args)
|
def self.refresh_automatic_groups!(*args)
|
||||||
if args.length == 0
|
if args.length == 0
|
||||||
args = AUTO_GROUPS.keys
|
args = AUTO_GROUPS.keys
|
||||||
|
|
Loading…
Reference in New Issue