discourse/plugins/chat/app/serializers/structured_channel_serializer.rb
Alan Guo Xiang Tan 14d54872f0
PERF: Use MessageBus.last_ids instead of MessageBus.last_id for chat (#19523)
Prior to this change, each request executed 2 Redis calls per chat channel
that was loaded. The number of Redis calls quickly adds up once a user
is following multiple channels.
2022-12-20 08:20:45 +08:00

81 lines
2.9 KiB
Ruby

# frozen_string_literal: true
class StructuredChannelSerializer < ApplicationSerializer
attributes :public_channels, :direct_message_channels, :message_bus_last_ids
def public_channels
object[:public_channels].map do |channel|
ChatChannelSerializer.new(
channel,
root: nil,
scope: scope,
membership: channel_membership(channel.id),
new_messages_message_bus_last_id: chat_message_bus_last_ids[ChatPublisher.new_messages_message_bus_channel(channel.id)],
new_mentions_message_bus_last_id: chat_message_bus_last_ids[ChatPublisher.new_mentions_message_bus_channel(channel.id)]
)
end
end
def direct_message_channels
object[:direct_message_channels].map do |channel|
ChatChannelSerializer.new(
channel,
root: nil,
scope: scope,
membership: channel_membership(channel.id),
new_messages_message_bus_last_id: chat_message_bus_last_ids[ChatPublisher.new_messages_message_bus_channel(channel.id)],
new_mentions_message_bus_last_id: chat_message_bus_last_ids[ChatPublisher.new_mentions_message_bus_channel(channel.id)]
)
end
end
def channel_membership(channel_id)
return if scope.anonymous?
object[:memberships].find { |membership| membership.chat_channel_id == channel_id }
end
def message_bus_last_ids
ids = {
channel_metadata: chat_message_bus_last_ids[ChatPublisher::CHANNEL_METADATA_MESSAGE_BUS_CHANNEL],
channel_edits: chat_message_bus_last_ids[ChatPublisher::CHANNEL_EDITS_MESSAGE_BUS_CHANNEL],
channel_status: chat_message_bus_last_ids[ChatPublisher::CHANNEL_STATUS_MESSAGE_BUS_CHANNEL],
new_channel: chat_message_bus_last_ids[ChatPublisher::NEW_CHANNEL_MESSAGE_BUS_CHANNEL]
}
if id = chat_message_bus_last_ids[ChatPublisher.user_tracking_state_message_bus_channel(scope.user.id)]
ids[:user_tracking_state] = id
end
ids
end
private
def chat_message_bus_last_ids
@chat_message_bus_last_ids ||= begin
message_bus_channels = [
ChatPublisher::CHANNEL_METADATA_MESSAGE_BUS_CHANNEL,
ChatPublisher::CHANNEL_EDITS_MESSAGE_BUS_CHANNEL,
ChatPublisher::CHANNEL_STATUS_MESSAGE_BUS_CHANNEL,
ChatPublisher::NEW_CHANNEL_MESSAGE_BUS_CHANNEL,
]
if !scope.anonymous?
message_bus_channels.push(ChatPublisher.user_tracking_state_message_bus_channel(scope.user.id))
end
object[:public_channels].each do |channel|
message_bus_channels.push(ChatPublisher.new_messages_message_bus_channel(channel.id))
message_bus_channels.push(ChatPublisher.new_mentions_message_bus_channel(channel.id))
end
object[:direct_message_channels].each do |channel|
message_bus_channels.push(ChatPublisher.new_messages_message_bus_channel(channel.id))
message_bus_channels.push(ChatPublisher.new_mentions_message_bus_channel(channel.id))
end
MessageBus.last_ids(*message_bus_channels)
end
end
end