From f55480756337dd4df7513e89673e81e003f1201a Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sat, 20 May 2017 22:13:51 +0900 Subject: Use joins for account properties (#3167) --- app/controllers/api/v1/blocks_controller.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'app/controllers/api/v1/blocks_controller.rb') diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index 742717ba2..37f5ea219 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -7,12 +7,14 @@ class Api::V1::BlocksController < ApiController respond_to :json def index - results = Block.where(account: current_account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h - @accounts = results.map { |f| accounts[f.target_account_id] }.compact + @accounts = Account.includes(:blocked_by) + .references(:blocked_by) + .merge(Block.where(account: current_account) + .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])) + .to_a - next_path = api_v1_blocks_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = api_v1_blocks_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = api_v1_blocks_url(pagination_params(max_id: @accounts.last.blocked_bies.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = api_v1_blocks_url(pagination_params(since_id: @accounts.first.blocked_bies.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) end -- cgit