From a176ce2fd00029ae4d41d3e8531fb5d621b43559 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Tue, 20 Dec 2022 07:52:26 +0800 Subject: [PATCH] PERF: Remove unnecessary SQL UNION query when search public channels (#19515) It is pointless to include DM channels because the query for public channel search restricts chatable type to `Category`. --- plugins/chat/lib/chat_channel_fetcher.rb | 4 +++- plugins/chat/plugin.rb | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/chat/lib/chat_channel_fetcher.rb b/plugins/chat/lib/chat_channel_fetcher.rb index b5f2a5b6254..f0e90fb3dc9 100644 --- a/plugins/chat/lib/chat_channel_fetcher.rb +++ b/plugins/chat/lib/chat_channel_fetcher.rb @@ -63,6 +63,7 @@ module Chat::ChatChannelFetcher def self.secured_public_channel_slug_lookup(guardian, slugs) allowed_channel_ids = generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: true) + ChatChannel .joins( "LEFT JOIN categories ON categories.id = chat_channels.chatable_id AND chat_channels.chatable_type = 'Category'", @@ -75,7 +76,7 @@ module Chat::ChatChannelFetcher def self.secured_public_channel_search(guardian, options = {}) allowed_channel_ids = - generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: options[:exclude_dm_channels]) + generate_allowed_channel_ids_sql(guardian, exclude_dm_channels: true) channels = ChatChannel.includes(chatable: [:topic_only_relative_url]) channels = channels.includes(:chat_channel_archive) if options[:include_archives] @@ -143,6 +144,7 @@ module Chat::ChatChannelFetcher guardian, options.merge(include_archives: true, filter_on_category_name: true), ) + decorate_memberships_with_tracking_data(guardian, channels, memberships) channels = channels.to_a preload_custom_fields_for(channels) diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index f945a295f0e..88204cf734a 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -421,7 +421,6 @@ after_initialize do following: false, limit: 1, status: :open, - exclude_dm_channels: true ).exists? end