From f55480756337dd4df7513e89673e81e003f1201a Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sat, 20 May 2017 22:13:51 +0900 Subject: [PATCH] Use joins for account properties (#3167) --- app/controllers/api/v1/accounts_controller.rb | 26 ++++++++++++------- app/controllers/api/v1/blocks_controller.rb | 12 +++++---- .../api/v1/follow_requests_controller.rb | 11 ++++---- app/controllers/api/v1/mutes_controller.rb | 12 +++++---- app/controllers/api/v1/statuses_controller.rb | 24 ++++++++++------- app/services/block_domain_service.rb | 2 +- 6 files changed, 51 insertions(+), 36 deletions(-) diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 12c7dd2b08c..664ac0ba50d 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -23,12 +23,14 @@ class Api::V1::AccountsController < ApiController end def following - results = Follow.where(account: @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] } + @accounts = Account.includes(:followers) + .references(:followers) + .merge(Follow.where(account: @account) + .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])) + .to_a - next_path = following_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = following_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = following_api_v1_account_url(pagination_params(max_id: @accounts.last.followers.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = following_api_v1_account_url(pagination_params(since_id: @accounts.first.followers.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) @@ -36,12 +38,16 @@ class Api::V1::AccountsController < ApiController end def followers - results = Follow.where(target_account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h - @accounts = results.map { |f| accounts[f.account_id] } + @accounts = Account.includes(:following) + .references(:following) + .merge(Follow.where(target_account: @account) + .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), + params[:max_id], + params[:since_id])) + .to_a - next_path = followers_api_v1_account_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = followers_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = followers_api_v1_account_url(pagination_params(max_id: @accounts.last.following.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = followers_api_v1_account_url(pagination_params(since_id: @accounts.first.following.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index 742717ba2fc..37f5ea219d6 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 diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb index 73cfaf10a60..4716019a8e9 100644 --- a/app/controllers/api/v1/follow_requests_controller.rb +++ b/app/controllers/api/v1/follow_requests_controller.rb @@ -5,12 +5,13 @@ class Api::V1::FollowRequestsController < ApiController before_action :require_user! def index - results = FollowRequest.where(target_account: current_account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h - @accounts = results.map { |f| accounts[f.account_id] } + @accounts = Account.includes(:follow_requests) + .references(:follow_requests) + .merge(FollowRequest.where(target_account: current_account) + .paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])) - next_path = api_v1_follow_requests_url(pagination_params(max_id: results.last.id)) if results.size == DEFAULT_ACCOUNTS_LIMIT - prev_path = api_v1_follow_requests_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = api_v1_follow_requests_url(pagination_params(max_id: @accounts.last.follow_requests.last.id)) if @accounts.size == DEFAULT_ACCOUNTS_LIMIT + prev_path = api_v1_follow_requests_url(pagination_params(since_id: @accounts.first.follow_requests.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) end diff --git a/app/controllers/api/v1/mutes_controller.rb b/app/controllers/api/v1/mutes_controller.rb index cbd98732bb4..baf1fc7efaf 100644 --- a/app/controllers/api/v1/mutes_controller.rb +++ b/app/controllers/api/v1/mutes_controller.rb @@ -7,12 +7,14 @@ class Api::V1::MutesController < ApiController respond_to :json def index - results = Mute.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] } + @accounts = Account.includes(:muting) + .references(:muting) + .merge(Mute.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_mutes_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = api_v1_mutes_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = api_v1_mutes_url(pagination_params(max_id: @accounts.last.mutings.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = api_v1_mutes_url(pagination_params(since_id: @accounts.first.mutings.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) end diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index 9312282ed6a..1fead10e7c8 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -32,12 +32,14 @@ class Api::V1::StatusesController < ApiController end def reblogged_by - results = @status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h - @accounts = results.map { |r| accounts[r.account_id] } + @accounts = Account.includes(statuses: :reblogs) + .references(statuses: :reblogs) + .where(statuses: { id: @status.id }) + .merge(@status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])) + .to_a - next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: @accounts.last.statuses.last.reblogs.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: @accounts.first.statuses.first.reblogs.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) @@ -45,12 +47,14 @@ class Api::V1::StatusesController < ApiController end def favourited_by - results = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) - accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h - @accounts = results.map { |f| accounts[f.account_id] } + @accounts = Account.includes(statuses: :favourites) + .references(statuses: :favourites) + .where(statuses: { id: @status.id }) + .merge(@status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])) + .to_a - next_path = favourited_by_api_v1_status_url(pagination_params(max_id: results.last.id)) if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) - prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty? + next_path = favourited_by_api_v1_status_url(pagination_params(max_id: @accounts.last.statuses.last.favourites.last.id)) if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: @accounts.first.statuses.first.favourites.first.id)) unless @accounts.empty? set_pagination_headers(next_path, prev_path) diff --git a/app/services/block_domain_service.rb b/app/services/block_domain_service.rb index 3f8f5b38563..e8b3a870d37 100644 --- a/app/services/block_domain_service.rb +++ b/app/services/block_domain_service.rb @@ -60,6 +60,6 @@ class BlockDomainService < BaseService end def media_from_blocked_domain - MediaAttachment.where(account: blocked_domain_accounts).reorder(nil) + MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil) end end