mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 10:20:58 -06:00
FIX: N+1 query for chat message serializer on mentions (#21767)
Followup to d4a5b79592
,
this introduced an N1 because every message in the list
we had to query users for the mentions and then the user's
status too. Instead we can just include both in Chat::MessagesQuery.
This commit is contained in:
parent
b7229953f7
commit
594636183d
@ -76,10 +76,14 @@ module Chat
|
||||
.includes(:uploads)
|
||||
.includes(chat_channel: :chatable)
|
||||
.includes(:thread)
|
||||
.includes(:chat_mentions)
|
||||
.where(chat_channel_id: channel.id)
|
||||
|
||||
query = query.includes(user: :user_status) if SiteSetting.enable_user_status
|
||||
if SiteSetting.enable_user_status
|
||||
query = query.includes(user: :user_status)
|
||||
query = query.includes(chat_mentions: { user: :user_status })
|
||||
else
|
||||
query = query.includes(chat_mentions: :user)
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
|
@ -27,9 +27,10 @@ module Chat
|
||||
has_many :uploads, serializer: ::UploadSerializer, embed: :objects
|
||||
|
||||
def mentioned_users
|
||||
User
|
||||
.where(id: object.chat_mentions.pluck(:user_id))
|
||||
.order("users.id ASC")
|
||||
object
|
||||
.chat_mentions
|
||||
.map(&:user)
|
||||
.sort_by(&:id)
|
||||
.map { |user| BasicUserWithStatusSerializer.new(user, root: false) }
|
||||
.as_json
|
||||
end
|
||||
|
@ -451,7 +451,7 @@ describe Chat::Message do
|
||||
notification = Fabricate(:notification)
|
||||
mention_1 = Fabricate(:chat_mention, chat_message: message_1, notification: notification)
|
||||
|
||||
message_1.destroy!
|
||||
message_1.reload.destroy!
|
||||
|
||||
expect { mention_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
|
Loading…
Reference in New Issue
Block a user