2020-07-17 05:48:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserLookup
|
2021-07-14 07:51:33 -04:00
|
|
|
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
|
2021-08-10 07:55:11 -04:00
|
|
|
@group_lookup_columns ||= %i{id name flair_icon flair_upload_id flair_bg_color flair_color}
|
2021-07-14 07:51:33 -04:00
|
|
|
end
|
2020-07-17 05:48:08 -04:00
|
|
|
|
|
|
|
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
|
2021-07-14 07:51:33 -04:00
|
|
|
@primary_groups ||= users.values.each_with_object({}) do |user, hash|
|
|
|
|
if user.primary_group_id
|
|
|
|
hash[user.id] = groups[user.primary_group_id]
|
2021-07-08 03:46:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flair_groups
|
2021-07-14 07:51:33 -04:00
|
|
|
@flair_groups ||= users.values.each_with_object({}) do |user, hash|
|
|
|
|
if user.flair_group_id
|
|
|
|
hash[user.id] = groups[user.flair_group_id]
|
2021-07-08 03:46:21 -04:00
|
|
|
end
|
|
|
|
end
|
2020-07-17 05:48:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def users
|
2021-07-14 07:51:33 -04:00
|
|
|
@users ||= User
|
|
|
|
.where(id: @user_ids)
|
2020-07-17 05:48:08 -04:00
|
|
|
.select(self.class.lookup_columns)
|
2021-07-14 07:51:33 -04:00
|
|
|
.index_by(&:id)
|
2020-07-17 05:48:08 -04:00
|
|
|
end
|
|
|
|
|
2021-07-08 03:46:21 -04:00
|
|
|
def groups
|
2021-07-14 07:51:33 -04:00
|
|
|
@group_lookup ||= begin
|
2021-07-08 03:46:21 -04:00
|
|
|
group_ids = users.values.map { |u| [u.primary_group_id, u.flair_group_id] }
|
|
|
|
group_ids.flatten!
|
|
|
|
group_ids.uniq!
|
|
|
|
group_ids.compact!
|
2020-07-17 05:48:08 -04:00
|
|
|
|
2021-07-14 07:51:33 -04:00
|
|
|
Group
|
|
|
|
.includes(:flair_upload)
|
2021-07-08 03:46:21 -04:00
|
|
|
.where(id: group_ids)
|
|
|
|
.select(self.class.group_lookup_columns)
|
2021-07-14 07:51:33 -04:00
|
|
|
.index_by(&:id)
|
2020-07-17 05:48:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|