discourse/lib/user_lookup.rb
Jarek Radosz c750bfb4af
FIX: A memoization bug in UserLookup and refactor (#13692)
`@group_lookup` memoization bug was introduced in #13587.
2021-07-14 13:51:33 +02:00

61 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class UserLookup
def self.lookup_columns
@user_lookup_columns ||= %i{id username name uploaded_avatar_id primary_group_id flair_group_id admin moderator trust_level}
end
def self.group_lookup_columns
@group_lookup_columns ||= %i{id name flair_icon flair_upload_id flair_bg_color flair_color}
end
def initialize(user_ids = [])
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
end
# Lookup a user by id
def [](user_id)
users[user_id]
end
def primary_groups
@primary_groups ||= users.values.each_with_object({}) do |user, hash|
if user.primary_group_id
hash[user.id] = groups[user.primary_group_id]
end
end
end
def flair_groups
@flair_groups ||= users.values.each_with_object({}) do |user, hash|
if user.flair_group_id
hash[user.id] = groups[user.flair_group_id]
end
end
end
private
def users
@users ||= User
.where(id: @user_ids)
.select(self.class.lookup_columns)
.index_by(&:id)
end
def groups
@group_lookup ||= begin
group_ids = users.values.map { |u| [u.primary_group_id, u.flair_group_id] }
group_ids.flatten!
group_ids.uniq!
group_ids.compact!
Group
.includes(:flair_upload)
.where(id: group_ids)
.select(self.class.group_lookup_columns)
.index_by(&:id)
end
end
end