PERF: Preload users and topics encrypt data (#108)

PERF: Preload users and topics encrypt data (#108)

diff --git a/app/controllers/encrypt_controller.rb b/app/controllers/encrypt_controller.rb
index 95ac69a..5318b24 100644
--- a/app/controllers/encrypt_controller.rb
+++ b/app/controllers/encrypt_controller.rb
@@ -132,7 +132,7 @@ class DiscourseEncrypt::EncryptController < ApplicationController
   # Returns status code 200, topics and posts.
   def posts
     posts = Post
-      .joins(:topic, topic: :encrypted_topics_users)
+      .includes(:topic, topic: [:encrypted_topics_users, :encrypted_topics_data])
       .where(post_number: 1)
       .where(encrypted_topics_users: { user_id: current_user.id })
       .order(created_at: :desc)
diff --git a/lib/encrypted_post_creator.rb b/lib/encrypted_post_creator.rb
index e527414..769aa8b 100644
--- a/lib/encrypted_post_creator.rb
+++ b/lib/encrypted_post_creator.rb
@@ -54,7 +54,10 @@ class EncryptedPostCreator < PostCreator
   private
 
   def users
-    @users ||= User.where(username_lower: @opts[:target_usernames].split(',').map(&:downcase)).to_a << @user
+    @users ||= User
+      .includes(:user_encryption_key)
+      .where(username_lower: (@opts[:target_usernames].split(',') << @user.username).map(&:downcase))
+      .to_a
   end
 
   def self.encrypt(raw, key)

GitHub sha: 559dbb0261ce391bdfbc5471ae9129a995d6fd8d

This commit appears in #108 which was approved by ZogStriP. It was merged by udan11.