Use joins for account properties (#3167)

pull/1536/merge
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
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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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