From 85d89b472dff2c3d06801dbd42f91c325d21a434 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 8 Sep 2016 20:36:01 +0200 Subject: Optimized n+1 queries in accounts Atom and HTML views Added stack trace for SQL queries in development Removed badly thought out accounts/lookup API --- app/controllers/accounts_controller.rb | 17 ++++++++++++----- app/controllers/api/accounts/lookup_controller.rb | 14 -------------- 2 files changed, 12 insertions(+), 19 deletions(-) delete mode 100644 app/controllers/api/accounts/lookup_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 3c02e0bec..c10a2c680 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -6,14 +6,21 @@ class AccountsController < ApplicationController def show respond_to do |format| - format.html { @statuses = @account.statuses.order('id desc').with_includes.with_counters.paginate(page: params[:page], per_page: 10)} + format.html do + @statuses = @account.statuses.order('id desc').with_includes.with_counters.paginate(page: params[:page], per_page: 10) + + if user_signed_in? + status_ids = @statuses.collect { |s| [s.id, s.reblog_of_id] }.flatten.uniq + @favourited = Favourite.where(status_id: status_ids).where(account_id: current_user.account_id).map { |f| [f.status_id, true] }.to_h + @reblogged = Status.where(reblog_of_id: status_ids).where(account_id: current_user.account_id).map { |s| [s.reblog_of_id, true] }.to_h + else + @favourited = {} + @reblogged = {} + end + end format.atom do @entries = @account.stream_entries.order('id desc').with_includes.paginate_by_max_id(20, params[:max_id] || nil) - - ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Status' }, activity: [:mentions, :media_attachments, reblog: :account, thread: :account]) - ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Favourite' }, activity: [:account, :status]) - ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Follow' }, activity: :target_account) end end end diff --git a/app/controllers/api/accounts/lookup_controller.rb b/app/controllers/api/accounts/lookup_controller.rb deleted file mode 100644 index 319401a2e..000000000 --- a/app/controllers/api/accounts/lookup_controller.rb +++ /dev/null @@ -1,14 +0,0 @@ -class Api::Accounts::LookupController < ApiController - before_action :doorkeeper_authorize! - respond_to :json - - def index - @accounts = Account.where(domain: nil).where(username: lookup_params) - end - - private - - def lookup_params - (params[:usernames] || '').split(',').map(&:strip) - end -end -- cgit