From fd72f503acf142fbcdc6c902b87d60fff0ca60eb Mon Sep 17 00:00:00 2001
From: ArtOfCode- <hello@artofcode.co.uk>
Date: Mon, 1 Jun 2020 20:30:35 +0100
Subject: [PATCH] More efficient hot questions selection

---
 app/controllers/application_controller.rb | 6 +++---
 app/views/layouts/_sidebar.html.erb       | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 2ba7c798a..69603f3d6 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -105,9 +105,9 @@ class ApplicationController < ActionController::Base
   end
 
   def pull_hot_questions
-    @hot_questions = Rails.cache.fetch('hot_questions', expires_in: 30.minutes) do
-      Post.undeleted.where(updated_at: (Rails.env.development? ? 365 : 1).days.ago..Time.now)
-          .where(parent_id: nil).includes(:category)
+    @hot_questions = Rails.cache.fetch('hot_questions', expires_in: 4.hours) do
+      Post.undeleted.where(updated_at: (Rails.env.development? ? 365 : 7).days.ago..Time.now)
+          .where(post_type_id: Question.post_type_id).includes(:category)
           .order('score DESC').limit(SiteSetting['HotQuestionsCount'])
     end
   end
diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb
index 190c879a8..a68343b81 100644
--- a/app/views/layouts/_sidebar.html.erb
+++ b/app/views/layouts/_sidebar.html.erb
@@ -12,7 +12,7 @@
     <% end %>
   <% end %>
 
-  <% if Rails.env.development? || @hot_questions.size > 0 %>
+  <% if Rails.env.development? || @hot_questions.to_a.size > 0 %>
     <div class="widget has-margin-4">
       <h4 class="widget--header has-margin-0">Hot Posts</h4>
       <% @hot_questions.each do |hq| %>
-- 
GitLab