mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-08 15:50:08 +09:00
Merge pull request #310: Find a direct room with one query instead of checking every one
Reviewed and merged by GPT on behalf of DHH.
This commit is contained in:
@@ -7,12 +7,15 @@ class Rooms::Direct < Room
|
||||
end
|
||||
|
||||
private
|
||||
# FIXME: Find a more performant algorithm that won't be a problem on accounts with 10K+ direct rooms,
|
||||
# which could be to store the membership id list as a hash on the room, and use that for lookup.
|
||||
# Among the first user's rooms, the one whose members are exactly these users: as many
|
||||
# memberships as users, and all of them theirs.
|
||||
def find_for(users)
|
||||
all.joins(:users).detect do |room|
|
||||
Set.new(room.user_ids) == Set.new(users.pluck(:id))
|
||||
end
|
||||
user_ids = users.pluck(:id).uniq
|
||||
|
||||
where(id: Membership.where(user_id: user_ids.first).select(:room_id))
|
||||
.joins(:memberships).group(:id)
|
||||
.having("COUNT(*) = :size AND COUNT(CASE WHEN memberships.user_id IN (:user_ids) THEN 1 END) = :size", size: user_ids.size, user_ids: user_ids)
|
||||
.first
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user