FIX: remove N+1 queries on front page

FIX: remove N+1 queries on front page

Note … for followup you still get N+1 on topic lists containing PMs
but it is a lot more of an edge case

From cd7faa4f7f9f9662731b4e26986406357c46195d Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 10 Dec 2018 13:49:50 +1100
Subject: [PATCH] FIX: remove N+1 queries on front page

Note .. for followup you still get N+1 on topic lists containing PMs
but it is a lot more of an edge case

diff --git a/plugin.rb b/plugin.rb
index e06b234..aa61f0e 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -193,7 +193,9 @@ after_initialize do
   # paired private key.
 
   add_to_serializer(:topic_view, :topic_key, false) do
-    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.topic.id}_#{scope.user.id}")
+    if object.topic.private_message?
+      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.topic.id}_#{scope.user.id}")
+    end
   end
 
   add_to_serializer(:topic_view, :include_topic_key?) do
@@ -201,7 +203,9 @@ after_initialize do
   end
 
   add_to_serializer(:basic_topic, :topic_key, false) do
-    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    if object.private_message?
+      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    end
   end
 
   add_to_serializer(:basic_topic, :include_topic_key?) do
@@ -209,7 +213,9 @@ after_initialize do
   end
 
   add_to_serializer(:listable_topic, :topic_key, false) do
-    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    if object.private_message?
+      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    end
   end
 
   add_to_serializer(:listable_topic, :include_topic_key?) do
@@ -217,7 +223,9 @@ after_initialize do
   end
 
   add_to_serializer(:topic_list_item, :topic_key, false) do
-    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    if object.private_message?
+      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
+    end
   end
 
   add_to_serializer(:topic_list_item, :include_topic_key?) do

GitHub

1 Like

I am very torn here, I don’t want to burn too much more time here.

Even if we move this to a dedicated model (which we should) we still need to teach something to preload the association which is tricky cause we need to hook in to core.

If we really do want to track this then we got to open a proper TODO for this on dev or meta. On the upside migrating to a dedicated model will be very easy and the data is both isolated and encrypted.

@udan11 feel free to mark this approved and maybe start a topic tracking changes for long term changes to encrypt v2 on dev

2 Likes