about summary refs log tree commit diff
path: root/app/controllers
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-10-16 18:57:54 +0200
committerEugen Rochko <eugen@zeonfederated.com>2016-10-16 19:10:16 +0200
commitde1f3aab868f8d18198438c405d0852c58f34e10 (patch)
tree914ad2285da94197fccaee7b462f3d7013823707 /app/controllers
parent1de2833f3045d48ab6d696a109a7a71f6a469135 (diff)
Fix #16 - Optimize n+1 queries when checking reblogged/favourited values for status lists in API
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/api/v1/accounts_controller.rb3
-rw-r--r--app/controllers/api/v1/statuses_controller.rb4
-rw-r--r--app/controllers/api_controller.rb6
3 files changed, 12 insertions, 1 deletions
diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb
index 7757fd7f8..2669315e2 100644
--- a/app/controllers/api/v1/accounts_controller.rb
+++ b/app/controllers/api/v1/accounts_controller.rb
@@ -25,6 +25,7 @@ class Api::V1::AccountsController < ApiController
 
   def statuses
     @statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(20, params[:max_id], params[:since_id]).to_a
+    set_maps(@statuses)
   end
 
   def follow
@@ -53,7 +54,7 @@ class Api::V1::AccountsController < ApiController
 
   def relationships
     ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
-    @accounts    = Account.find(ids)
+    @accounts    = Account.where(id: ids).select('id')
     @following   = Account.following_map(ids, current_user.account_id)
     @followed_by = Account.followed_by_map(ids, current_user.account_id)
     @blocking    = Account.blocking_map(ids, current_user.account_id)
diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb
index 64dc3f84e..a7305233e 100644
--- a/app/controllers/api/v1/statuses_controller.rb
+++ b/app/controllers/api/v1/statuses_controller.rb
@@ -10,6 +10,7 @@ class Api::V1::StatusesController < ApiController
     @status      = Status.find(params[:id])
     @ancestors   = @status.ancestors
     @descendants = @status.descendants
+    set_maps([@status] + @ancestors + @descendants)
   end
 
   def create
@@ -46,16 +47,19 @@ class Api::V1::StatusesController < ApiController
 
   def home
     @statuses = Feed.new(:home, current_user.account).get(20, params[:max_id], params[:since_id]).to_a
+    set_maps(@statuses)
     render action: :index
   end
 
   def mentions
     @statuses = Feed.new(:mentions, current_user.account).get(20, params[:max_id], params[:since_id]).to_a
+    set_maps(@statuses)
     render action: :index
   end
 
   def public
     @statuses = Status.as_public_timeline(current_user.account).paginate_by_max_id(20, params[:max_id], params[:since_id]).to_a
+    set_maps(@statuses)
     render action: :index
   end
 end
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index 4ccf20bc9..c4ad450cf 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -35,4 +35,10 @@ class ApiController < ApplicationController
   def render_empty
     render json: {}, status: 200
   end
+
+  def set_maps(statuses)
+    status_ids      = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.compact
+    @reblogs_map    = Status.reblogs_map(status_ids, current_user.account)
+    @favourites_map = Status.favourites_map(status_ids, current_user.account)
+  end
 end