mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 19:33:41 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
33 lines
605 B
Ruby
33 lines
605 B
Ruby
# frozen_string_literal: true
|
|
|
|
class AvatarLookup
|
|
|
|
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
|
|
|
|
private
|
|
|
|
def self.lookup_columns
|
|
@lookup_columns ||= %i{id user_emails.email username name uploaded_avatar_id}
|
|
end
|
|
|
|
def users
|
|
@users ||= user_lookup_hash
|
|
end
|
|
|
|
def user_lookup_hash
|
|
hash = {}
|
|
User.joins(:user_emails)
|
|
.where(id: @user_ids)
|
|
.select(AvatarLookup.lookup_columns)
|
|
.each { |user| hash[user.id] = user }
|
|
hash
|
|
end
|
|
end
|