Use joins for account properties (#3167)

This commit is contained in:
Akihiko Odaki 2017-05-20 22:13:51 +09:00 committed by Eugen Rochko
parent d972845ff6
commit f554807563
6 changed files with 51 additions and 36 deletions

View file

@ -23,12 +23,14 @@ class Api::V1::AccountsController < ApiController
end end
def following def following
results = Follow.where(account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) @accounts = Account.includes(:followers)
accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h .references(:followers)
@accounts = results.map { |f| accounts[f.target_account_id] } .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)
@ -36,12 +38,16 @@ class Api::V1::AccountsController < ApiController
end end
def followers 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.includes(:following)
accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h .references(:following)
@accounts = results.map { |f| accounts[f.account_id] } .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)

View file

@ -7,12 +7,14 @@ class Api::V1::BlocksController < ApiController
respond_to :json respond_to :json
def index 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.includes(:blocked_by)
accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h .references(:blocked_by)
@accounts = results.map { |f| accounts[f.target_account_id] }.compact .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)
end end

View file

@ -5,12 +5,13 @@ class Api::V1::FollowRequestsController < ApiController
before_action :require_user! before_action :require_user!
def index def index
results = FollowRequest.where(target_account: current_account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) @accounts = Account.includes(:follow_requests)
accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h .references(:follow_requests)
@accounts = results.map { |f| accounts[f.account_id] } .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 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)
end end

View file

@ -7,12 +7,14 @@ class Api::V1::MutesController < ApiController
respond_to :json respond_to :json
def index 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.includes(:muting)
accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h .references(:muting)
@accounts = results.map { |f| accounts[f.target_account_id] } .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)
end end

View file

@ -32,12 +32,14 @@ class Api::V1::StatusesController < ApiController
end end
def reblogged_by def reblogged_by
results = @status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) @accounts = Account.includes(statuses: :reblogs)
accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h .references(statuses: :reblogs)
@accounts = results.map { |r| accounts[r.account_id] } .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)
@ -45,12 +47,14 @@ class Api::V1::StatusesController < ApiController
end end
def favourited_by def favourited_by
results = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) @accounts = Account.includes(statuses: :favourites)
accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h .references(statuses: :favourites)
@accounts = results.map { |f| accounts[f.account_id] } .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) 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: results.first.id)) unless results.empty? 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) set_pagination_headers(next_path, prev_path)

View file

@ -60,6 +60,6 @@ class BlockDomainService < BaseService
end end
def media_from_blocked_domain def media_from_blocked_domain
MediaAttachment.where(account: blocked_domain_accounts).reorder(nil) MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
end end
end end