FIX: ensure group's users counters are kept in sync

This commit is contained in:
Régis Hanol 2016-04-04 17:03:18 +02:00
parent d677f852c4
commit 79639e2dec
4 changed files with 22 additions and 4 deletions

View File

@ -66,6 +66,7 @@ class Admin::GroupsController < Admin::AdminController
group.title = group.automatic ? nil : title
if group.save
Group.reset_counters(group.id, :group_users)
render_serialized(group, BasicGroupSerializer)
else
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)
end
Group.reset_counters(group.id, :group_users)
render json: success_json
end
@ -111,6 +114,8 @@ class Admin::GroupsController < Admin::AdminController
user = User.find(params[:user_id].to_i)
group.group_users.where(user_id: user.id).update_all(owner: false)
Group.reset_counters(group.id, :group_users)
render json: success_json
end

View File

@ -13,15 +13,17 @@ module Jobs
domains = group.automatic_membership_email_domains.gsub('.', '\.')
User.where("email ~* '@(#{domains})$' and users.id not in (
select user_id from group_users where group_users.group_id = ?
)", group_id).find_each do |user|
User.where("email ~* '@(#{domains})$'")
.where("users.id NOT IN (SELECT user_id FROM group_users WHERE group_users.group_id = ?)", group_id)
.find_each do |user|
begin
group.add(user)
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
# we don't care about this
end
end
Group.reset_counters(group.id, :group_users)
end
end

View File

@ -5,7 +5,7 @@ module Jobs
def execute(args)
UserVisit.ensure_consistency!
Group.refresh_automatic_groups!
Group.ensure_consistency!
Notification.ensure_consistency!
UserAction.ensure_consistency!
TopicFeaturedUsers.ensure_consistency!

View File

@ -217,6 +217,17 @@ class Group < ActiveRecord::Base
group
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)
if args.length == 0
args = AUTO_GROUPS.keys