A platform for community discussion. Free, open, simple.
FIX: Channel archive N1 when serializing current user (#19820)
Martin Brennan5f4911dae896
diff --git a/plugins/chat/app/serializers/chat_channel_serializer.rb b/plugins/chat/app/serializers/chat_channel_serializer.rbindex b720d691f2f..e6707acfd6b 100644--- a/plugins/chat/app/serializers/chat_channel_serializer.rb+++ b/plugins/chat/app/serializers/chat_channel_serializer.rb@@ -61,7 +61,7 @@ class ChatChannelSerializer < ApplicationSerializerenddef include_archive_status?- scope.is_staff? && (object.archived? || archive&.failed?) && archive.present?+ !object.direct_message_channel? && scope.is_staff? && archive.present?enddef archive_completeddiff --git a/plugins/chat/spec/plugin_spec.rb b/plugins/chat/spec/plugin_spec.rbindex f31c4c1ec92..68cdda55f97 100644--- a/plugins/chat/spec/plugin_spec.rb+++ b/plugins/chat/spec/plugin_spec.rb@@ -345,7 +345,7 @@ describe Chat doendend- context "when followed public channels exist" do+ context "when followed direct message channels exist" dofab!(:user_2) { Fabricate(:user) }fab!(:channel) { Fabricate(:direct_message_channel, users: [user, user_2]) }@@ -356,7 +356,7 @@ describe Chat doendend- context "when followed direct message channels exist" do+ context "when followed public channels exist" dofab!(:channel) { Fabricate(:chat_channel) }before dodiff --git a/plugins/chat/spec/requests/core_ext/latest_performance_spec.rb b/plugins/chat/spec/requests/core_ext/latest_performance_spec.rbnew file mode 100644index 00000000000..185279e1d41--- /dev/null+++ b/plugins/chat/spec/requests/core_ext/latest_performance_spec.rb@@ -0,0 +1,70 @@+# frozen_string_literal: true++describe ListController do+ fab!(:current_user) { Fabricate(:user) }++ before do+ SiteSetting.chat_enabled = true+ Group.refresh_automatic_groups!+ sign_in(current_user)+ end++ describe "#latest" do+ it "does not do N+1 chat_channel_archive queries based on the number of public and DM channels" do+ user_1 = Fabricate(:user)+ Fabricate(:direct_message_channel, users: [current_user, user_1])+ public_channel_1 = Fabricate(:chat_channel)+ public_channel_2 = Fabricate(:chat_channel)++ Fabricate(+ :user_chat_channel_membership,+ user: current_user,+ chat_channel: public_channel_1,+ following: true,+ )++ # warm up+ get "/latest.html"+ expect(response.status).to eq(200)++ initial_sql_queries_count =+ track_sql_queries do+ get "/latest.html"+ expect(response.status).to eq(200)+ expect(response.body).to have_tag("div#data-preloaded") do |element|+ current_user_json =+ JSON.parse(+ JSON.parse(element.current_scope.attribute("data-preloaded").value)["currentUser"],+ )+ expect(current_user_json["chat_channels"]["direct_message_channels"].count).to eq(1)+ expect(current_user_json["chat_channels"]["public_channels"].count).to eq(1)+ end+ end.count++ Fabricate(+ :user_chat_channel_membership,+ user: current_user,+ chat_channel: public_channel_2,+ following: true,+ )+ user_2 = Fabricate(:user)+ Fabricate(:direct_message_channel, users: [current_user, user_2])++ new_sql_queries_count =+ track_sql_queries do+ get "/latest.html"+ expect(response.status).to eq(200)+ expect(response.body).to have_tag("div#data-preloaded") do |element|+ current_user_json =+ JSON.parse(+ JSON.parse(element.current_scope.attribute("data-preloaded").value)["currentUser"],+ )+ expect(current_user_json["chat_channels"]["direct_message_channels"].count).to eq(2)+ expect(current_user_json["chat_channels"]["public_channels"].count).to eq(2)+ end+ end.count++ expect(new_sql_queries_count).to be <= initial_sql_queries_count+ end+ end+end